class Keyword_wrap_alphabet(Enum):
- from_a = 0
- from_last = 1
- from_largest = 2
+ from_a = 1
+ from_last = 2
+ from_largest = 3
def keyword_cipher_alphabet_of(keyword, wrap_alphabet=Keyword_wrap_alphabet.from_a):
>>> keyword_break(keyword_encipher('this is a test message for the ' \
'keyword decipherment', 'elephant', Keyword_wrap_alphabet.from_last), \
wordlist=['cat', 'elephant', 'kangaroo']) # doctest: +ELLIPSIS
- (('elephant', <Keyword_wrap_alphabet.from_last: 1>), -52.834575011...)
+ (('elephant', <Keyword_wrap_alphabet.from_last: 2>), -52.834575011...)
"""
best_keyword = ''
best_wrap_alphabet = True
>>> keyword_break_mp(keyword_encipher('this is a test message for the ' \
'keyword decipherment', 'elephant', Keyword_wrap_alphabet.from_last), \
wordlist=['cat', 'elephant', 'kangaroo']) # doctest: +ELLIPSIS
- (('elephant', <Keyword_wrap_alphabet.from_last: 1>), -52.834575011...)
+ (('elephant', <Keyword_wrap_alphabet.from_last: 2>), -52.834575011...)
"""
with Pool() as pool:
helper_args = [(message, word, wrap, fitness)
<!DOCTYPE html>
<html>
<head>
- <title>Affine ciphers</title>
+ <title>Breaking keyword ciphers</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<style type="text/css">
/* Slideshow styles */
Python's Global Interpreter Lock prevents shooting yourself in the foot.
Where you want true parallelism, need different threads (Python processes).
+
* Thread-safe shared-memory code is hard.
The `multiprocessing` library makes this easier.
## Positional, keyword
* Common or garden parameters, as you're used to.
-* `def keyword_encipher(message, keyword, wrap_alphabet=0):`
+* `def keyword_encipher(message, keyword, Keyword_wrap_alphabet.from_a):`
## Excess positional
* `def mean(x, *xs):`
<!DOCTYPE html>
<html>
<head>
- <title>Affine ciphers</title>
+ <title>Keyword ciphers</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<style type="text/css">
/* Slideshow styles */
# Keyword arguments
Used to:
+
1. give a default value for a parameter
2. allow parameters to be named (not just positional)
Pass this parameter to the `keyword_alphabet` procedure.
-## Note: `Enum` introduced in Python 3.4. This is a better solution.
+## wrap_alphabet has no inherent meaning
+Use Python 3.4's `Enum`
+```python
+from enum import Enum
+
+class Keyword_wrap_alphabet(Enum):
+ from_a = 1
+ from_last = 2
+ from_largest = 3
+```
+(Use integers in earlier Pythons)
---
# Deduplicating a sequence
* Santitise the keyword before we use it
+---
+# Making the keyword alphabet
+
## Cases
1. As we're deduplicating anyway, just add the entire alphabet to the end of the keyword, then deduplicate.
`deduplicate(keyword + string.ascii_lowercase)`
2. and 3. How to find the appropriate letter of the keyword.
Indexing pulls out letters. `'keyword'[0]` = 'k' ; `'keyword'[3]` = 'w' ; `'keyword'[-1]` = 'd'
+
Slices pulls out substrings. `'keyword'[1:4]` = 'eyw' ; `'keyword'[:3]` = 'key' ; `'keyword'[5:]` = 'rd'
`deduplicate(keyword + string_ascii_lowercase[from:] + string.ascii_lowercase)`