I have created a functional Caesar cipher in Python 3 and I have implemented a GUI using Tkinter.
def MOST_COMMON_LETTER():
return("e")
def circle(l,n):
if n < (len(l) - 1):
return(l[n])
else:
return(l[n % (len(l))])
def most_frequent(l): # not whitespace
l = l.replace(' ','')
all_frequencies = [l.count(i) for i in l]
for i in l:
if l.count(i) == max(all_frequencies):
return(i)
def my_formatting(string):
for i in string:
if i in ".,1234567890?!;:_;-/()[]{}+-*\"'":
string = string.replace(i,'')
string = string.lower()
return(string)
def en_de_code(string,key,en_or_de): #calling with en is encode, calling with de is decode
alphabet = "abcdefghijklmnopqrstuvwxyz"
new_string = ""
string = my_formatting(string)
for i in string:
if i == " ":
new_string += " "
else:
position = alphabet.index(i)
if en_or_de == 'en':
new_string += circle(alphabet,position + key)
elif en_or_de == 'de':
new_string += circle(alphabet,position - key)
return(new_string)
def encode(string,key):
return(en_de_code(string,key,"en"))
def decode(string,key):
return(en_de_code(string,key,"de"))
def randomly_encode(string):
import random
key = random.randint(1,25)
return(encode(string,key))
def brute_force_decode(string):
new_string = ""
for key in range(1,26):
new_string += decode(string,key)
new_string += '-'
return(new_string)
def meaningful_decode(string):
alphabet = "abcdefghijklmnopqrstuvwxyz"
the_most_frequent = most_frequent(string)
key = -(alphabet.index(the_most_frequent) - alphabet.index(MOST_COMMON_LETTER()))
return(decode(string,key))
def GUI():
try:
import Tkinter as tk
except:
import tkinter as tk
master = tk.Tk()
master.wm_title("Caesar cipher")
key_label = tk.Label(master, text="Key")
key_label.pack()
key_slider = tk.Scale(master, from_=1, to=25, orient=tk.HORIZONTAL,length=300)
key_slider.pack()
text_label = tk.Label(master, text="Enter your text down here")
text_label.pack()
text_field = tk.Text(master)
text_field.pack()
def allow_to_paste(string):
master.clipboard_clear()
master.clipboard_append(string)
def encode_and_show():
temp1 = encode(text_field.get("1.0","end-1c"),key_slider.get())
result.set(temp1)
allow_to_paste(temp1)
b = tk.Button(master, text="Encode", command=encode_and_show)
b.pack()
def randomly_encode_and_show():
temp2 = randomly_encode(text_field.get("1.0","end-1c"))
result.set(temp2)
allow_to_paste(temp2)
b = tk.Button(master, text="Randomly encode", command=randomly_encode_and_show)
b.pack()
def decode_and_show():
temp3 = decode(text_field.get("1.0","end-1c"),key_slider.get())
result.set(temp3)
allow_to_paste(temp3)
b = tk.Button(master, text="Decode", command=decode_and_show)
b.pack()
def brute_force_decode_and_show():
temp4= brute_force_decode(text_field.get("1.0","end-1c"))
result.set(temp4)
allow_to_paste(temp4)
b = tk.Button(master, text="Brute force decode", command=brute_force_decode_and_show)
b.pack()
def meaningful_decode_and_show():
temp5 = meaningful_decode(text_field.get("1.0","end-1c"))
result.set(temp5)
allow_to_paste(temp5)
b = tk.Button(master, text="Meanigful decode", command=meaningful_decode_and_show)
b.pack()
result = tk.StringVar()
result.set("Encoded text")
paste_information = tk.Label(master, text="Just paste to en/de-coded text where you want it.")
paste_information.pack()
result_label = tk.Label(master, textvariable=result)
result_label.pack()
tk.mainloop()
if __name__ == "__main__":
GUI()
- Is it understandable the first time you read it?
- Does it make use of "black magic" (absurd expressions that only who used them in the first place knows)?
- Should I explain briefly what my function do with a doc-string?
- Should I use a Class for the Tkinter GUI part?
- Is there something that is just bug-prone or considered bad style?