Hacking Secret Ciphers with Python

(Ann) #1
Chapter 12 – Detecting English Programmatically 171

directory as detectEnglish.py then this module will not work. If the user doesn’t have this file, the
comments tell them they can download it from http://invpy.com/dictionary.txt.


detectEnglish.py


  1. UPPERLETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

  2. LETTERS_AND_SPACE = UPPERLETTERS + UPPERLETTERS.lower() + ' \t\n'


Lines 10 and 11 set up a few variables that are constants, which is why they have uppercase
names. UPPERLETTERS is a variable containing the 26 uppercase letters, and
LETTERS_AND_SPACE contain these letters (and the lowercase letters returned from
UPPERLETTERS.lower()) but also the space character, the tab character, and the newline
character. The tab and newline characters are represented with escape characters \t and \n.


detectEnglish.py


  1. def loadDictionary():

  2. dictionaryFile = open('dictionary.txt')


The dictionary file sits on the user’s hard drive, but we need to load the text in this file as a string
value so our Python code can use it. First, we get a file object by calling open() and passing the
string of the filename 'dictionary.txt'. Before we continue with the
loadDictionary() code, let’s learn about the dictionary data type.


Dictionaries and the Dictionary Data Type


The dictionary data type has values which can contain multiple other values, just like lists do. In
list values, you use an integer index value to retrieve items in the list, like spam[42]. For each
item in the dictionary value, there is a key used to retrieve it. (Values stored inside lists and
dictionaries are also sometimes called items.) The key can be an integer or a string value, like
spam['hello'] or spam[42]. Dictionaries let us organize our program’s data with even
more flexibility than lists.


Instead of typing square brackets like list values, dictionary values (or simply, dictionaries) use
curly braces. Try typing the following into the interactive shell:





emptyList = []
emptyDictionary = {}





A dictionary’s values are typed out as key-value pairs, which are separated by colons. Multiple
key-value pairs are separated by commas. To retrieve values from a dictionary, just use square

Free download pdf