I know there are plenty of Caesar Cipher reviews on here, but I especially wanted to see how other programmers felt about my use of the cmd module and the efficiency of my functions.
Though this program in its current state is fully functional, I feel the design may be gridlocked and lacking flexibility, due to my lack of knowledge regarding the process of creating commands for the user to utilize.
The current design makes it difficult to implement the varying command attributes I hoped to add later on. How should I go about doing this? A format I see in other cmd programs goes something like command --numbers --property=number --filename
import cmd
def sym_extract(text,syms):
text = text[text.find(syms[0])+1:text.find(syms[1])]
return text
def del_punc(string):
string = ''.join(ch if ch.isalnum() else '' if ch!=' ' else ' ' for ch in string)
return(string.lower())
def cipher(text, shift):
text = del_punc(text)
secret = ''
for c in text:
if c in 'abcdefghijklmnopqrstuvwxyz':
num = ord(c)
num += shift
if num > ord('z'):
num -= 26
elif num < ord('a'):
num += 26
secret = secret + chr(num)
else:
secret = secret + c
return secret
class CryptoPromptCmd(cmd.Cmd):
prompt = '\n> '
def default(self, arg):
print('invalid command, try typing "help"')
def do_quit(self, arg):
'''exits the program'''
return True
def do_encrypt(self, arg):
'''usage: encrypt (provided string or /filename)[shift number]'''
if arg != '':
text = del_punc(sym_extract(arg,'()'))
shift = int(sym_extract(arg,'[]'))
encrypted = cipher(text,shift)
print('\n ',encrypted.upper())
else:
print('invalid command\nusage: encrypt(provided string or /filename)[shift number]')
return
def do_decrypt(self, arg):
'''usage:\noutput to file: decrypt(provided string or /filename)[file]\nattempt manually: decrypt(provided string or /filename)[shift number]'''
if arg != '':
text = del_punc(sym_extract(arg,'()'))
shift = -1*int(sym_extract(arg,'[]'))
decrypted = cipher(text,shift)
print('\n ',decrypted.upper())
else:
print('invalid command\nusage:')
print('output to file: decrypt(provided string or /filename)[output /filename]')
print('attempt manually: decrypt(provided string or /filename)[shift number]')
return
if __name__ == '__main__':
print('\n'* 3)
print("Caesar Cipher Tool")
print('==================')
print()
print('type "help" for commands')
print()
CryptoPromptCmd().cmdloop()
print('\nexiting...\n')
tr d-za-c a-z
? \$\endgroup\$tr
program translates characters according to the specification you give it, sotr a-z d-za-c
encodes the Caesar cipher (with a shift of 3) andtr d-za-c a-z
decodes it. For example,echo hello | tr a-z d-za-c
outputskhoor
. \$\endgroup\$