Hacking Secret Ciphers with Python

(Ann) #1

256 http://inventwithpython.com/hacking


Email questions to the author: [email protected]


The pattern '0.1.0.1.0.1' does not exist in the dictionary. This is why the expression
wordPatterns.allPatterns['0.1.0.1.0.1'] causes an error (because there is no
'0.1.0.1.0.1' key in allPatterns) and why '0.1.0.1.0.1' in
wordPatterns.allPatterns evaluates to False.


How the Program Works..........................................................................................................................................


makeWordPatterns.py



  1. Makes the wordPatterns.py File




  2. http://inventwithpython.com/hacking (BSD Licensed)






  3. Creates wordPatterns.py based on the words in our dictionary




  4. text file, dictionary.txt. (Download this file from




  5. http://invpy.com/dictionary.txt))




The top part of this file has the usual comments describing what the program is.


The pprint.pprint() and pprint.pformat() Functions


makeWordPatterns.py


  1. import pprint


The pprint module has functions for pretty printing values, which is useful for printing
dictionary and list values on the screen. The print() function simply prints these values going
left to right:





print(someListOfListsVar))
[['ant'], ['baboon', 'badger', 'bat', 'bear', 'beaver'], ['camel', 'cat',
'clam', 'cobra', 'cougar', 'coyote', 'crow'], ['deer', 'dog', 'donkey',
'duck'], ['eagle'], ['ferret', 'fox', 'frog'], ['goat']]





The pprint module has a function named pprint(). The value passed to
pprint.pprint() will be “pretty printed” to the screen so that it is easier to read:





import pprint
pprint.pprint(someListOfListsVar))
[['ant'],
['baboon', 'badger', 'bat', 'bear', 'beaver'],
['camel', 'cat', 'clam', 'cobra', 'cougar', 'coyote', 'crow'],
['deer', 'dog', 'donkey', 'duck'],
['eagle'],
['ferret', 'fox', 'frog'],
['goat']]




Free download pdf