From c51b4c30edcc2f0947f44df7126b67b8d1cbe044 Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Thu, 7 May 2020 15:05:59 +0200 Subject: [PATCH] Added input library --- caesar.py | 18 ++++++++++-------- lib/__pycache__/input.cpython-38.pyc | Bin 0 -> 616 bytes lib/__pycache__/vars.cpython-38.pyc | Bin 427 -> 583 bytes lib/input.py | 10 ++++++++++ lib/vars.py | 7 +++++++ 5 files changed, 27 insertions(+), 8 deletions(-) create mode 100644 lib/__pycache__/input.cpython-38.pyc create mode 100644 lib/input.py diff --git a/caesar.py b/caesar.py index 9b1e6d4..b78c77e 100644 --- a/caesar.py +++ b/caesar.py @@ -1,12 +1,13 @@ -import sys +from lib.input import * from lib.vars import alphabet +from lib.vars import listToString -if( len(sys.argv) >= 4 ): - in_key = int(sys.argv[1]) - in_txt = sys.argv[2] - in_alphabet = sys.argv[3] +if( inputHasKeys(["-k", "-i", "-a"]) ): + in_key = int(getValueOfKey("-k")) + in_txt = getValueOfKey("-i") + in_alphabet = getValueOfKey("-a") else: - print("file.py {int KEY} {string TXT} {string ALPHABET_TYPE}") + print("file.py -k {int KEY} -i {string TXT} -a {string ALPHABET_TYPE}") exit() alen = len(alphabet[in_alphabet]) @@ -16,7 +17,8 @@ decryp_list = [""] * len(in_txt) for char in txt_list: index = alphabet[in_alphabet].index(char) - + print("Decrypting char-index: " + str(txt_list.index(char)) + " (" + char + ")") + index = index + in_key # shift the alphabet while( index >= alen ): #cycle through the alphabet diff = (index + in_key) - (alen - 1) @@ -24,4 +26,4 @@ for char in txt_list: decryp_list[txt_list.index(char)] = alphabet[in_alphabet][index] -print(decryp_list) +print( "Output: " + listToString(decryp_list) ) diff --git a/lib/__pycache__/input.cpython-38.pyc b/lib/__pycache__/input.cpython-38.pyc new file mode 100644 index 0000000000000000000000000000000000000000..599bc6f80ff9cf256ffbc3f96e0b2759bdaef297 GIT binary patch literal 616 zcmZWmO-sW-5S`g=)A&&oFDewgDFJE3b18xz1f>c^4+VwTU2E*xkKI(P#FPFIJ$m=o zqfIXeH+!$f#2*Rw>2 zgd9=g2~yaDM|@--R8lxwWce(*>z$I+p-?0@z(yc!fhH_lMzTb8%a*uj zRo{glE1RixbWh(6Y@J)jD+TiGek~WaA2|}^sx{MZN5<8wKhptbGBPH>R*4Kdj9u~< rd43v!|LXV!0qp|SKz_4hSMrOg?VMpw&ad6{)yU{NdzEwZh3|ar#TIFV0=al%CHi7deknhFz7bF$a*aV6tOjW8z>00FoUOCIA2c diff --git a/lib/input.py b/lib/input.py new file mode 100644 index 0000000..d5690f7 --- /dev/null +++ b/lib/input.py @@ -0,0 +1,10 @@ +import sys + +def getValueOfKey(key): # get the value of an input key, example: -c {VALUE} + for i in sys.argv: + if( i == key ): + return sys.argv[sys.argv.index(key) + 1] + return -1 # if no value was found return -1 + +def inputHasKeys(keyList): + return all(key in sys.argv for key in keyList) diff --git a/lib/vars.py b/lib/vars.py index 995c07c..0739f7f 100644 --- a/lib/vars.py +++ b/lib/vars.py @@ -10,3 +10,10 @@ alphabet["ENG"] = eng_alphabet alphabet["SWE"] = swe_alphabet # Functions +def listToString( l ): + returnStr = "" + + for char in l: + returnStr += char + + return returnStr