282 http://inventwithpython.com/hacking
Email questions to the author: [email protected]
variable with all non-letter and non-space characters replaced by a blank string. This effectively
returns a string that has all punctuation and number characters removed from message.
This string then has the upper() method called on it to return an uppercase version of the
string, and that string has the split() method called on it to return the individual words in the
string in a list. To see what each part of line 1 15 does, type the following into the interactive
shell:
import re
nonLettersOrSpacePattern = re.compile('[^A-Z\s]')
message = 'Hello, this is my 1st test message.'
message = nonLettersOrSpacePattern.sub('', message.upper())
message
'HELLO THIS IS MY ST TEST MESSAGE'
cipherwordList = message.split()
cipherwordList
['HELLO', 'THIS', 'IS', 'MY', 'ST', 'TEST', 'MESSAGE']
After line 115 executes, the cipherwordList variable will contain a list of uppercase strings
of the individual words that were previously in message.
simpleSubHacker.py
116. for cipherword in cipherwordList:
117. # Get a new cipherletter mapping for each ciphertext word.
118. newMap = getBlankCipherletterMapping()
The for loop on line 1 16 will assign each string in the message list to the cipherword
variable. Inside this loop we will get the cipherword’s candidates, add the candidates to a
cipherletter mapping, and then intersect this mapping with intersectedMap.
First, line 118 will get a new, blank cipherletter mapping from
getBlankCipherletterMapping() and store it in the newMap variable.
simpleSubHacker.py
120. wordPattern = makeWordPatterns.getWordPattern(cipherword)
121. if wordPattern not in wordPatterns.allPatterns:
122. continue # This word was not in our dictionary, so continue.
To find the candidates for the current cipherword, we call getWordPattern() in the
makeWordPatterns module on line 1 20. If the word pattern of the cipherword does not exist
in the keys of the wordPatterns.allPatterns dictionary, then whatever the cipherword