Started on documentation
[szyfrow.git] / docs / szyfrow / pocket_enigma.html
1 <!doctype html>
2 <html lang="en">
3 <head>
4 <meta charset="utf-8">
5 <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1" />
6 <meta name="generator" content="pdoc 0.9.2" />
7 <title>szyfrow.pocket_enigma API documentation</title>
8 <meta name="description" content="The Pocket Enigma machine, a simple example that illustrates the mechanisms
9 of the Enigma machine. See [a …" />
10 <link rel="preload stylesheet" as="style" href="https://cdnjs.cloudflare.com/ajax/libs/10up-sanitize.css/11.0.1/sanitize.min.css" integrity="sha256-PK9q560IAAa6WVRRh76LtCaI8pjTJ2z11v0miyNNjrs=" crossorigin>
11 <link rel="preload stylesheet" as="style" href="https://cdnjs.cloudflare.com/ajax/libs/10up-sanitize.css/11.0.1/typography.min.css" integrity="sha256-7l/o7C8jubJiy74VsKTidCy1yBkRtiUGbVkYBylBqUg=" crossorigin>
12 <link rel="stylesheet preload" as="style" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.1.1/styles/github.min.css" crossorigin>
13 <style>:root{--highlight-color:#fe9}.flex{display:flex !important}body{line-height:1.5em}#content{padding:20px}#sidebar{padding:30px;overflow:hidden}#sidebar > *:last-child{margin-bottom:2cm}.http-server-breadcrumbs{font-size:130%;margin:0 0 15px 0}#footer{font-size:.75em;padding:5px 30px;border-top:1px solid #ddd;text-align:right}#footer p{margin:0 0 0 1em;display:inline-block}#footer p:last-child{margin-right:30px}h1,h2,h3,h4,h5{font-weight:300}h1{font-size:2.5em;line-height:1.1em}h2{font-size:1.75em;margin:1em 0 .50em 0}h3{font-size:1.4em;margin:25px 0 10px 0}h4{margin:0;font-size:105%}h1:target,h2:target,h3:target,h4:target,h5:target,h6:target{background:var(--highlight-color);padding:.2em 0}a{color:#058;text-decoration:none;transition:color .3s ease-in-out}a:hover{color:#e82}.title code{font-weight:bold}h2[id^="header-"]{margin-top:2em}.ident{color:#900}pre code{background:#f8f8f8;font-size:.8em;line-height:1.4em}code{background:#f2f2f1;padding:1px 4px;overflow-wrap:break-word}h1 code{background:transparent}pre{background:#f8f8f8;border:0;border-top:1px solid #ccc;border-bottom:1px solid #ccc;margin:1em 0;padding:1ex}#http-server-module-list{display:flex;flex-flow:column}#http-server-module-list div{display:flex}#http-server-module-list dt{min-width:10%}#http-server-module-list p{margin-top:0}.toc ul,#index{list-style-type:none;margin:0;padding:0}#index code{background:transparent}#index h3{border-bottom:1px solid #ddd}#index ul{padding:0}#index h4{margin-top:.6em;font-weight:bold}@media (min-width:200ex){#index .two-column{column-count:2}}@media (min-width:300ex){#index .two-column{column-count:3}}dl{margin-bottom:2em}dl dl:last-child{margin-bottom:4em}dd{margin:0 0 1em 3em}#header-classes + dl > dd{margin-bottom:3em}dd dd{margin-left:2em}dd p{margin:10px 0}.name{background:#eee;font-weight:bold;font-size:.85em;padding:5px 10px;display:inline-block;min-width:40%}.name:hover{background:#e0e0e0}dt:target .name{background:var(--highlight-color)}.name > span:first-child{white-space:nowrap}.name.class > span:nth-child(2){margin-left:.4em}.inherited{color:#999;border-left:5px solid #eee;padding-left:1em}.inheritance em{font-style:normal;font-weight:bold}.desc h2{font-weight:400;font-size:1.25em}.desc h3{font-size:1em}.desc dt code{background:inherit}.source summary,.git-link-div{color:#666;text-align:right;font-weight:400;font-size:.8em;text-transform:uppercase}.source summary > *{white-space:nowrap;cursor:pointer}.git-link{color:inherit;margin-left:1em}.source pre{max-height:500px;overflow:auto;margin:0}.source pre code{font-size:12px;overflow:visible}.hlist{list-style:none}.hlist li{display:inline}.hlist li:after{content:',\2002'}.hlist li:last-child:after{content:none}.hlist .hlist{display:inline;padding-left:1em}img{max-width:100%}td{padding:0 .5em}.admonition{padding:.1em .5em;margin-bottom:1em}.admonition-title{font-weight:bold}.admonition.note,.admonition.info,.admonition.important{background:#aef}.admonition.todo,.admonition.versionadded,.admonition.tip,.admonition.hint{background:#dfd}.admonition.warning,.admonition.versionchanged,.admonition.deprecated{background:#fd4}.admonition.error,.admonition.danger,.admonition.caution{background:lightpink}</style>
14 <style media="screen and (min-width: 700px)">@media screen and (min-width:700px){#sidebar{width:30%;height:100vh;overflow:auto;position:sticky;top:0}#content{width:70%;max-width:100ch;padding:3em 4em;border-left:1px solid #ddd}pre code{font-size:1em}.item .name{font-size:1em}main{display:flex;flex-direction:row-reverse;justify-content:flex-end}.toc ul ul,#index ul{padding-left:1.5em}.toc > ul > li{margin-top:.5em}}</style>
15 <style media="print">@media print{#sidebar h1{page-break-before:always}.source{display:none}}@media print{*{background:transparent !important;color:#000 !important;box-shadow:none !important;text-shadow:none !important}a[href]:after{content:" (" attr(href) ")";font-size:90%}a[href][title]:after{content:none}abbr[title]:after{content:" (" attr(title) ")"}.ir a:after,a[href^="javascript:"]:after,a[href^="#"]:after{content:""}pre,blockquote{border:1px solid #999;page-break-inside:avoid}thead{display:table-header-group}tr,img{page-break-inside:avoid}img{max-width:100% !important}@page{margin:0.5cm}p,h2,h3{orphans:3;widows:3}h1,h2,h3,h4,h5,h6{page-break-after:avoid}}</style>
16 <script defer src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.1.1/highlight.min.js" integrity="sha256-Uv3H6lx7dJmRfRvH8TH6kJD1TSK1aFcwgx+mdg3epi8=" crossorigin></script>
17 <script>window.addEventListener('DOMContentLoaded', () => hljs.initHighlighting())</script>
18 </head>
19 <body>
20 <main>
21 <article id="content">
22 <header>
23 <h1 class="title">Module <code>szyfrow.pocket_enigma</code></h1>
24 </header>
25 <section id="section-intro">
26 <p>The Pocket Enigma machine, a simple example that illustrates the mechanisms
27 of the Enigma machine. See <a href="http://www.savory.de/pocket_enigma.htm">a review</a>
28 for more information about the machine.</p>
29 <details class="source">
30 <summary>
31 <span>Expand source code</span>
32 </summary>
33 <pre><code class="python">&#34;&#34;&#34;The Pocket Enigma machine, a simple example that illustrates the mechanisms
34 of the Enigma machine. See [a review](http://www.savory.de/pocket_enigma.htm)
35 for more information about the machine.
36 &#34;&#34;&#34;
37
38 from szyfrow.support.utilities import *
39 from szyfrow.support.language_models import *
40
41
42 class PocketEnigma(object):
43 &#34;&#34;&#34;A pocket enigma machine
44 The wheel is internally represented as a 26-element list self.wheel_map,
45 where wheel_map[i] == j shows that the position i places on from the arrow
46 maps to the position j places on.
47 &#34;&#34;&#34;
48 def __init__(self, wheel=1, position=&#39;a&#39;):
49 &#34;&#34;&#34;initialise the pocket enigma, including which wheel to use and the
50 starting position of the wheel.
51
52 The wheel is either 1 or 2 (the predefined wheels) or a list of letter
53 pairs.
54
55 The position is the letter pointed to by the arrow on the wheel.
56
57 &gt;&gt;&gt; pe.wheel_map
58 [25, 4, 23, 10, 1, 7, 9, 5, 12, 6, 3, 17, 8, 14, 13, 21, 19, 11, 20, 16, 18, 15, 24, 2, 22, 0]
59 &gt;&gt;&gt; pe.position
60 0
61 &#34;&#34;&#34;
62 self.wheel1 = [(&#39;a&#39;, &#39;z&#39;), (&#39;b&#39;, &#39;e&#39;), (&#39;c&#39;, &#39;x&#39;), (&#39;d&#39;, &#39;k&#39;),
63 (&#39;f&#39;, &#39;h&#39;), (&#39;g&#39;, &#39;j&#39;), (&#39;i&#39;, &#39;m&#39;), (&#39;l&#39;, &#39;r&#39;), (&#39;n&#39;, &#39;o&#39;),
64 (&#39;p&#39;, &#39;v&#39;), (&#39;q&#39;, &#39;t&#39;), (&#39;s&#39;, &#39;u&#39;), (&#39;w&#39;, &#39;y&#39;)]
65 self.wheel2 = [(&#39;a&#39;, &#39;c&#39;), (&#39;b&#39;, &#39;d&#39;), (&#39;e&#39;, &#39;w&#39;), (&#39;f&#39;, &#39;i&#39;),
66 (&#39;g&#39;, &#39;p&#39;), (&#39;h&#39;, &#39;m&#39;), (&#39;j&#39;, &#39;k&#39;), (&#39;l&#39;, &#39;n&#39;), (&#39;o&#39;, &#39;q&#39;),
67 (&#39;r&#39;, &#39;z&#39;), (&#39;s&#39;, &#39;u&#39;), (&#39;t&#39;, &#39;v&#39;), (&#39;x&#39;, &#39;y&#39;)]
68 if wheel == 1:
69 self.make_wheel_map(self.wheel1)
70 elif wheel == 2:
71 self.make_wheel_map(self.wheel2)
72 else:
73 self.validate_wheel_spec(wheel)
74 self.make_wheel_map(wheel)
75 if position in string.ascii_lowercase:
76 self.position = pos(position)
77 else:
78 self.position = position
79
80 def make_wheel_map(self, wheel_spec):
81 &#34;&#34;&#34;Expands a wheel specification from a list of letter-letter pairs
82 into a full wheel_map.
83
84 &gt;&gt;&gt; pe.make_wheel_map(pe.wheel2)
85 [2, 3, 0, 1, 22, 8, 15, 12, 5, 10, 9, 13, 7, 11, 16, 6, 14, 25, 20, 21, 18, 19, 4, 24, 23, 17]
86 &#34;&#34;&#34;
87 self.validate_wheel_spec(wheel_spec)
88 self.wheel_map = [0] * 26
89 for p in wheel_spec:
90 self.wheel_map[pos(p[0])] = pos(p[1])
91 self.wheel_map[pos(p[1])] = pos(p[0])
92 return self.wheel_map
93
94 def validate_wheel_spec(self, wheel_spec):
95 &#34;&#34;&#34;Validates that a wheel specificaiton will turn into a valid wheel
96 map.
97
98 &gt;&gt;&gt; pe.validate_wheel_spec([])
99 Traceback (most recent call last):
100 ...
101 ValueError: Wheel specification has 0 pairs, requires 13
102 &gt;&gt;&gt; pe.validate_wheel_spec([(&#39;a&#39;, &#39;b&#39;, &#39;c&#39;)]*13)
103 Traceback (most recent call last):
104 ...
105 ValueError: Not all mappings in wheel specificationhave two elements
106 &gt;&gt;&gt; pe.validate_wheel_spec([(&#39;a&#39;, &#39;b&#39;)]*13)
107 Traceback (most recent call last):
108 ...
109 ValueError: Wheel specification does not contain 26 letters
110 &#34;&#34;&#34;
111 if len(wheel_spec) != 13:
112 raise ValueError(&#34;Wheel specification has {} pairs, requires 13&#34;.
113 format(len(wheel_spec)))
114 for p in wheel_spec:
115 if len(p) != 2:
116 raise ValueError(&#34;Not all mappings in wheel specification&#34;
117 &#34;have two elements&#34;)
118 if len(set([p[0] for p in wheel_spec] +
119 [p[1] for p in wheel_spec])) != 26:
120 raise ValueError(&#34;Wheel specification does not contain 26 letters&#34;)
121
122 def encipher_letter(self, letter):
123 &#34;&#34;&#34;Enciphers a single letter, by advancing the wheel before looking up
124 the letter on the wheel.
125
126 &gt;&gt;&gt; pe.set_position(&#39;f&#39;)
127 5
128 &gt;&gt;&gt; pe.encipher_letter(&#39;k&#39;)
129 &#39;h&#39;
130 &#34;&#34;&#34;
131 self.advance()
132 return self.lookup(letter)
133 decipher_letter = encipher_letter
134
135 def lookup(self, letter):
136 &#34;&#34;&#34;Look up what a letter enciphers to, without turning the wheel.
137
138 &gt;&gt;&gt; pe.set_position(&#39;f&#39;)
139 5
140 &gt;&gt;&gt; cat([pe.lookup(l) for l in string.ascii_lowercase])
141 &#39;udhbfejcpgmokrliwntsayqzvx&#39;
142 &gt;&gt;&gt; pe.lookup(&#39;A&#39;)
143 &#39;&#39;
144 &#34;&#34;&#34;
145 if letter in string.ascii_lowercase:
146 return unpos(
147 (self.wheel_map[(pos(letter) - self.position) % 26] +
148 self.position))
149 else:
150 return &#39;&#39;
151
152 def advance(self):
153 &#34;&#34;&#34;Advances the wheel one position.
154
155 &gt;&gt;&gt; pe.set_position(&#39;f&#39;)
156 5
157 &gt;&gt;&gt; pe.advance()
158 6
159 &#34;&#34;&#34;
160 self.position = (self.position + 1) % 26
161 return self.position
162
163 def encipher(self, message, starting_position=None):
164 &#34;&#34;&#34;Enciphers a whole message.
165
166 &gt;&gt;&gt; pe.set_position(&#39;f&#39;)
167 5
168 &gt;&gt;&gt; pe.encipher(&#39;helloworld&#39;)
169 &#39;kjsglcjoqc&#39;
170 &gt;&gt;&gt; pe.set_position(&#39;f&#39;)
171 5
172 &gt;&gt;&gt; pe.encipher(&#39;kjsglcjoqc&#39;)
173 &#39;helloworld&#39;
174 &gt;&gt;&gt; pe.encipher(&#39;helloworld&#39;, starting_position = &#39;x&#39;)
175 &#39;egrekthnnf&#39;
176 &#34;&#34;&#34;
177 if starting_position:
178 self.set_position(starting_position)
179 transformed = &#39;&#39;
180 for l in message:
181 transformed += self.encipher_letter(l)
182 return transformed
183 decipher = encipher
184
185 def set_position(self, position):
186 &#34;&#34;&#34;Sets the position of the wheel, by specifying the letter the arrow
187 points to.
188
189 &gt;&gt;&gt; pe.set_position(&#39;a&#39;)
190 0
191 &gt;&gt;&gt; pe.set_position(&#39;m&#39;)
192 12
193 &gt;&gt;&gt; pe.set_position(&#39;z&#39;)
194 25
195 &#34;&#34;&#34;
196 self.position = pos(position)
197 return self.position
198
199
200 def pocket_enigma_break_by_crib(message, wheel_spec, crib, crib_position):
201 &#34;&#34;&#34;Break a pocket enigma using a crib (some plaintext that&#39;s expected to
202 be in a certain position). Returns a list of possible starting wheel
203 positions that could produce the crib.
204
205 &gt;&gt;&gt; pocket_enigma_break_by_crib(&#39;kzpjlzmoga&#39;, 1, &#39;h&#39;, 0)
206 [&#39;a&#39;, &#39;f&#39;, &#39;q&#39;]
207 &gt;&gt;&gt; pocket_enigma_break_by_crib(&#39;kzpjlzmoga&#39;, 1, &#39;he&#39;, 0)
208 [&#39;a&#39;]
209 &gt;&gt;&gt; pocket_enigma_break_by_crib(&#39;kzpjlzmoga&#39;, 1, &#39;ll&#39;, 2)
210 [&#39;a&#39;]
211 &gt;&gt;&gt; pocket_enigma_break_by_crib(&#39;kzpjlzmoga&#39;, 1, &#39;l&#39;, 2)
212 [&#39;a&#39;]
213 &gt;&gt;&gt; pocket_enigma_break_by_crib(&#39;kzpjlzmoga&#39;, 1, &#39;l&#39;, 3)
214 [&#39;a&#39;, &#39;j&#39;, &#39;n&#39;]
215 &gt;&gt;&gt; pocket_enigma_break_by_crib(&#39;aaaaa&#39;, 1, &#39;l&#39;, 3)
216 []
217 &#34;&#34;&#34;
218 pe = PocketEnigma(wheel=wheel_spec)
219 possible_positions = []
220 for p in string.ascii_lowercase:
221 pe.set_position(p)
222 plaintext = pe.decipher(message)
223 if plaintext[crib_position:crib_position+len(crib)] == crib:
224 possible_positions += [p]
225 return possible_positions
226
227 if __name__ == &#34;__main__&#34;:
228 import doctest
229 doctest.testmod(extraglobs={&#39;pe&#39;: PocketEnigma(1, &#39;a&#39;)})</code></pre>
230 </details>
231 </section>
232 <section>
233 </section>
234 <section>
235 </section>
236 <section>
237 <h2 class="section-title" id="header-functions">Functions</h2>
238 <dl>
239 <dt id="szyfrow.pocket_enigma.cat"><code class="name flex">
240 <span>def <span class="ident">cat</span></span>(<span>iterable, /)</span>
241 </code></dt>
242 <dd>
243 <div class="desc"><p>Concatenate any number of strings.</p>
244 <p>The string whose method is called is inserted in between each given string.
245 The result is returned as a new string.</p>
246 <p>Example: '.'.join(['ab', 'pq', 'rs']) -&gt; 'ab.pq.rs'</p></div>
247 </dd>
248 <dt id="szyfrow.pocket_enigma.lcat"><code class="name flex">
249 <span>def <span class="ident">lcat</span></span>(<span>iterable, /)</span>
250 </code></dt>
251 <dd>
252 <div class="desc"><p>Concatenate any number of strings.</p>
253 <p>The string whose method is called is inserted in between each given string.
254 The result is returned as a new string.</p>
255 <p>Example: '.'.join(['ab', 'pq', 'rs']) -&gt; 'ab.pq.rs'</p></div>
256 </dd>
257 <dt id="szyfrow.pocket_enigma.pocket_enigma_break_by_crib"><code class="name flex">
258 <span>def <span class="ident">pocket_enigma_break_by_crib</span></span>(<span>message, wheel_spec, crib, crib_position)</span>
259 </code></dt>
260 <dd>
261 <div class="desc"><p>Break a pocket enigma using a crib (some plaintext that's expected to
262 be in a certain position). Returns a list of possible starting wheel
263 positions that could produce the crib.</p>
264 <pre><code class="language-python-repl">&gt;&gt;&gt; pocket_enigma_break_by_crib('kzpjlzmoga', 1, 'h', 0)
265 ['a', 'f', 'q']
266 &gt;&gt;&gt; pocket_enigma_break_by_crib('kzpjlzmoga', 1, 'he', 0)
267 ['a']
268 &gt;&gt;&gt; pocket_enigma_break_by_crib('kzpjlzmoga', 1, 'll', 2)
269 ['a']
270 &gt;&gt;&gt; pocket_enigma_break_by_crib('kzpjlzmoga', 1, 'l', 2)
271 ['a']
272 &gt;&gt;&gt; pocket_enigma_break_by_crib('kzpjlzmoga', 1, 'l', 3)
273 ['a', 'j', 'n']
274 &gt;&gt;&gt; pocket_enigma_break_by_crib('aaaaa', 1, 'l', 3)
275 []
276 </code></pre></div>
277 <details class="source">
278 <summary>
279 <span>Expand source code</span>
280 </summary>
281 <pre><code class="python">def pocket_enigma_break_by_crib(message, wheel_spec, crib, crib_position):
282 &#34;&#34;&#34;Break a pocket enigma using a crib (some plaintext that&#39;s expected to
283 be in a certain position). Returns a list of possible starting wheel
284 positions that could produce the crib.
285
286 &gt;&gt;&gt; pocket_enigma_break_by_crib(&#39;kzpjlzmoga&#39;, 1, &#39;h&#39;, 0)
287 [&#39;a&#39;, &#39;f&#39;, &#39;q&#39;]
288 &gt;&gt;&gt; pocket_enigma_break_by_crib(&#39;kzpjlzmoga&#39;, 1, &#39;he&#39;, 0)
289 [&#39;a&#39;]
290 &gt;&gt;&gt; pocket_enigma_break_by_crib(&#39;kzpjlzmoga&#39;, 1, &#39;ll&#39;, 2)
291 [&#39;a&#39;]
292 &gt;&gt;&gt; pocket_enigma_break_by_crib(&#39;kzpjlzmoga&#39;, 1, &#39;l&#39;, 2)
293 [&#39;a&#39;]
294 &gt;&gt;&gt; pocket_enigma_break_by_crib(&#39;kzpjlzmoga&#39;, 1, &#39;l&#39;, 3)
295 [&#39;a&#39;, &#39;j&#39;, &#39;n&#39;]
296 &gt;&gt;&gt; pocket_enigma_break_by_crib(&#39;aaaaa&#39;, 1, &#39;l&#39;, 3)
297 []
298 &#34;&#34;&#34;
299 pe = PocketEnigma(wheel=wheel_spec)
300 possible_positions = []
301 for p in string.ascii_lowercase:
302 pe.set_position(p)
303 plaintext = pe.decipher(message)
304 if plaintext[crib_position:crib_position+len(crib)] == crib:
305 possible_positions += [p]
306 return possible_positions</code></pre>
307 </details>
308 </dd>
309 <dt id="szyfrow.pocket_enigma.wcat"><code class="name flex">
310 <span>def <span class="ident">wcat</span></span>(<span>iterable, /)</span>
311 </code></dt>
312 <dd>
313 <div class="desc"><p>Concatenate any number of strings.</p>
314 <p>The string whose method is called is inserted in between each given string.
315 The result is returned as a new string.</p>
316 <p>Example: '.'.join(['ab', 'pq', 'rs']) -&gt; 'ab.pq.rs'</p></div>
317 </dd>
318 </dl>
319 </section>
320 <section>
321 <h2 class="section-title" id="header-classes">Classes</h2>
322 <dl>
323 <dt id="szyfrow.pocket_enigma.PocketEnigma"><code class="flex name class">
324 <span>class <span class="ident">PocketEnigma</span></span>
325 <span>(</span><span>wheel=1, position='a')</span>
326 </code></dt>
327 <dd>
328 <div class="desc"><p>A pocket enigma machine
329 The wheel is internally represented as a 26-element list self.wheel_map,
330 where wheel_map[i] == j shows that the position i places on from the arrow
331 maps to the position j places on.</p>
332 <p>initialise the pocket enigma, including which wheel to use and the
333 starting position of the wheel.</p>
334 <p>The wheel is either 1 or 2 (the predefined wheels) or a list of letter
335 pairs.</p>
336 <p>The position is the letter pointed to by the arrow on the wheel.</p>
337 <pre><code class="language-python-repl">&gt;&gt;&gt; pe.wheel_map
338 [25, 4, 23, 10, 1, 7, 9, 5, 12, 6, 3, 17, 8, 14, 13, 21, 19, 11, 20, 16, 18, 15, 24, 2, 22, 0]
339 &gt;&gt;&gt; pe.position
340 0
341 </code></pre></div>
342 <details class="source">
343 <summary>
344 <span>Expand source code</span>
345 </summary>
346 <pre><code class="python">class PocketEnigma(object):
347 &#34;&#34;&#34;A pocket enigma machine
348 The wheel is internally represented as a 26-element list self.wheel_map,
349 where wheel_map[i] == j shows that the position i places on from the arrow
350 maps to the position j places on.
351 &#34;&#34;&#34;
352 def __init__(self, wheel=1, position=&#39;a&#39;):
353 &#34;&#34;&#34;initialise the pocket enigma, including which wheel to use and the
354 starting position of the wheel.
355
356 The wheel is either 1 or 2 (the predefined wheels) or a list of letter
357 pairs.
358
359 The position is the letter pointed to by the arrow on the wheel.
360
361 &gt;&gt;&gt; pe.wheel_map
362 [25, 4, 23, 10, 1, 7, 9, 5, 12, 6, 3, 17, 8, 14, 13, 21, 19, 11, 20, 16, 18, 15, 24, 2, 22, 0]
363 &gt;&gt;&gt; pe.position
364 0
365 &#34;&#34;&#34;
366 self.wheel1 = [(&#39;a&#39;, &#39;z&#39;), (&#39;b&#39;, &#39;e&#39;), (&#39;c&#39;, &#39;x&#39;), (&#39;d&#39;, &#39;k&#39;),
367 (&#39;f&#39;, &#39;h&#39;), (&#39;g&#39;, &#39;j&#39;), (&#39;i&#39;, &#39;m&#39;), (&#39;l&#39;, &#39;r&#39;), (&#39;n&#39;, &#39;o&#39;),
368 (&#39;p&#39;, &#39;v&#39;), (&#39;q&#39;, &#39;t&#39;), (&#39;s&#39;, &#39;u&#39;), (&#39;w&#39;, &#39;y&#39;)]
369 self.wheel2 = [(&#39;a&#39;, &#39;c&#39;), (&#39;b&#39;, &#39;d&#39;), (&#39;e&#39;, &#39;w&#39;), (&#39;f&#39;, &#39;i&#39;),
370 (&#39;g&#39;, &#39;p&#39;), (&#39;h&#39;, &#39;m&#39;), (&#39;j&#39;, &#39;k&#39;), (&#39;l&#39;, &#39;n&#39;), (&#39;o&#39;, &#39;q&#39;),
371 (&#39;r&#39;, &#39;z&#39;), (&#39;s&#39;, &#39;u&#39;), (&#39;t&#39;, &#39;v&#39;), (&#39;x&#39;, &#39;y&#39;)]
372 if wheel == 1:
373 self.make_wheel_map(self.wheel1)
374 elif wheel == 2:
375 self.make_wheel_map(self.wheel2)
376 else:
377 self.validate_wheel_spec(wheel)
378 self.make_wheel_map(wheel)
379 if position in string.ascii_lowercase:
380 self.position = pos(position)
381 else:
382 self.position = position
383
384 def make_wheel_map(self, wheel_spec):
385 &#34;&#34;&#34;Expands a wheel specification from a list of letter-letter pairs
386 into a full wheel_map.
387
388 &gt;&gt;&gt; pe.make_wheel_map(pe.wheel2)
389 [2, 3, 0, 1, 22, 8, 15, 12, 5, 10, 9, 13, 7, 11, 16, 6, 14, 25, 20, 21, 18, 19, 4, 24, 23, 17]
390 &#34;&#34;&#34;
391 self.validate_wheel_spec(wheel_spec)
392 self.wheel_map = [0] * 26
393 for p in wheel_spec:
394 self.wheel_map[pos(p[0])] = pos(p[1])
395 self.wheel_map[pos(p[1])] = pos(p[0])
396 return self.wheel_map
397
398 def validate_wheel_spec(self, wheel_spec):
399 &#34;&#34;&#34;Validates that a wheel specificaiton will turn into a valid wheel
400 map.
401
402 &gt;&gt;&gt; pe.validate_wheel_spec([])
403 Traceback (most recent call last):
404 ...
405 ValueError: Wheel specification has 0 pairs, requires 13
406 &gt;&gt;&gt; pe.validate_wheel_spec([(&#39;a&#39;, &#39;b&#39;, &#39;c&#39;)]*13)
407 Traceback (most recent call last):
408 ...
409 ValueError: Not all mappings in wheel specificationhave two elements
410 &gt;&gt;&gt; pe.validate_wheel_spec([(&#39;a&#39;, &#39;b&#39;)]*13)
411 Traceback (most recent call last):
412 ...
413 ValueError: Wheel specification does not contain 26 letters
414 &#34;&#34;&#34;
415 if len(wheel_spec) != 13:
416 raise ValueError(&#34;Wheel specification has {} pairs, requires 13&#34;.
417 format(len(wheel_spec)))
418 for p in wheel_spec:
419 if len(p) != 2:
420 raise ValueError(&#34;Not all mappings in wheel specification&#34;
421 &#34;have two elements&#34;)
422 if len(set([p[0] for p in wheel_spec] +
423 [p[1] for p in wheel_spec])) != 26:
424 raise ValueError(&#34;Wheel specification does not contain 26 letters&#34;)
425
426 def encipher_letter(self, letter):
427 &#34;&#34;&#34;Enciphers a single letter, by advancing the wheel before looking up
428 the letter on the wheel.
429
430 &gt;&gt;&gt; pe.set_position(&#39;f&#39;)
431 5
432 &gt;&gt;&gt; pe.encipher_letter(&#39;k&#39;)
433 &#39;h&#39;
434 &#34;&#34;&#34;
435 self.advance()
436 return self.lookup(letter)
437 decipher_letter = encipher_letter
438
439 def lookup(self, letter):
440 &#34;&#34;&#34;Look up what a letter enciphers to, without turning the wheel.
441
442 &gt;&gt;&gt; pe.set_position(&#39;f&#39;)
443 5
444 &gt;&gt;&gt; cat([pe.lookup(l) for l in string.ascii_lowercase])
445 &#39;udhbfejcpgmokrliwntsayqzvx&#39;
446 &gt;&gt;&gt; pe.lookup(&#39;A&#39;)
447 &#39;&#39;
448 &#34;&#34;&#34;
449 if letter in string.ascii_lowercase:
450 return unpos(
451 (self.wheel_map[(pos(letter) - self.position) % 26] +
452 self.position))
453 else:
454 return &#39;&#39;
455
456 def advance(self):
457 &#34;&#34;&#34;Advances the wheel one position.
458
459 &gt;&gt;&gt; pe.set_position(&#39;f&#39;)
460 5
461 &gt;&gt;&gt; pe.advance()
462 6
463 &#34;&#34;&#34;
464 self.position = (self.position + 1) % 26
465 return self.position
466
467 def encipher(self, message, starting_position=None):
468 &#34;&#34;&#34;Enciphers a whole message.
469
470 &gt;&gt;&gt; pe.set_position(&#39;f&#39;)
471 5
472 &gt;&gt;&gt; pe.encipher(&#39;helloworld&#39;)
473 &#39;kjsglcjoqc&#39;
474 &gt;&gt;&gt; pe.set_position(&#39;f&#39;)
475 5
476 &gt;&gt;&gt; pe.encipher(&#39;kjsglcjoqc&#39;)
477 &#39;helloworld&#39;
478 &gt;&gt;&gt; pe.encipher(&#39;helloworld&#39;, starting_position = &#39;x&#39;)
479 &#39;egrekthnnf&#39;
480 &#34;&#34;&#34;
481 if starting_position:
482 self.set_position(starting_position)
483 transformed = &#39;&#39;
484 for l in message:
485 transformed += self.encipher_letter(l)
486 return transformed
487 decipher = encipher
488
489 def set_position(self, position):
490 &#34;&#34;&#34;Sets the position of the wheel, by specifying the letter the arrow
491 points to.
492
493 &gt;&gt;&gt; pe.set_position(&#39;a&#39;)
494 0
495 &gt;&gt;&gt; pe.set_position(&#39;m&#39;)
496 12
497 &gt;&gt;&gt; pe.set_position(&#39;z&#39;)
498 25
499 &#34;&#34;&#34;
500 self.position = pos(position)
501 return self.position</code></pre>
502 </details>
503 <h3>Methods</h3>
504 <dl>
505 <dt id="szyfrow.pocket_enigma.PocketEnigma.advance"><code class="name flex">
506 <span>def <span class="ident">advance</span></span>(<span>self)</span>
507 </code></dt>
508 <dd>
509 <div class="desc"><p>Advances the wheel one position.</p>
510 <pre><code class="language-python-repl">&gt;&gt;&gt; pe.set_position('f')
511 5
512 &gt;&gt;&gt; pe.advance()
513 6
514 </code></pre></div>
515 <details class="source">
516 <summary>
517 <span>Expand source code</span>
518 </summary>
519 <pre><code class="python">def advance(self):
520 &#34;&#34;&#34;Advances the wheel one position.
521
522 &gt;&gt;&gt; pe.set_position(&#39;f&#39;)
523 5
524 &gt;&gt;&gt; pe.advance()
525 6
526 &#34;&#34;&#34;
527 self.position = (self.position + 1) % 26
528 return self.position</code></pre>
529 </details>
530 </dd>
531 <dt id="szyfrow.pocket_enigma.PocketEnigma.decipher"><code class="name flex">
532 <span>def <span class="ident">decipher</span></span>(<span>self, message, starting_position=None)</span>
533 </code></dt>
534 <dd>
535 <div class="desc"><p>Enciphers a whole message.</p>
536 <pre><code class="language-python-repl">&gt;&gt;&gt; pe.set_position('f')
537 5
538 &gt;&gt;&gt; pe.encipher('helloworld')
539 'kjsglcjoqc'
540 &gt;&gt;&gt; pe.set_position('f')
541 5
542 &gt;&gt;&gt; pe.encipher('kjsglcjoqc')
543 'helloworld'
544 &gt;&gt;&gt; pe.encipher('helloworld', starting_position = 'x')
545 'egrekthnnf'
546 </code></pre></div>
547 <details class="source">
548 <summary>
549 <span>Expand source code</span>
550 </summary>
551 <pre><code class="python">def encipher(self, message, starting_position=None):
552 &#34;&#34;&#34;Enciphers a whole message.
553
554 &gt;&gt;&gt; pe.set_position(&#39;f&#39;)
555 5
556 &gt;&gt;&gt; pe.encipher(&#39;helloworld&#39;)
557 &#39;kjsglcjoqc&#39;
558 &gt;&gt;&gt; pe.set_position(&#39;f&#39;)
559 5
560 &gt;&gt;&gt; pe.encipher(&#39;kjsglcjoqc&#39;)
561 &#39;helloworld&#39;
562 &gt;&gt;&gt; pe.encipher(&#39;helloworld&#39;, starting_position = &#39;x&#39;)
563 &#39;egrekthnnf&#39;
564 &#34;&#34;&#34;
565 if starting_position:
566 self.set_position(starting_position)
567 transformed = &#39;&#39;
568 for l in message:
569 transformed += self.encipher_letter(l)
570 return transformed</code></pre>
571 </details>
572 </dd>
573 <dt id="szyfrow.pocket_enigma.PocketEnigma.decipher_letter"><code class="name flex">
574 <span>def <span class="ident">decipher_letter</span></span>(<span>self, letter)</span>
575 </code></dt>
576 <dd>
577 <div class="desc"><p>Enciphers a single letter, by advancing the wheel before looking up
578 the letter on the wheel.</p>
579 <pre><code class="language-python-repl">&gt;&gt;&gt; pe.set_position('f')
580 5
581 &gt;&gt;&gt; pe.encipher_letter('k')
582 'h'
583 </code></pre></div>
584 <details class="source">
585 <summary>
586 <span>Expand source code</span>
587 </summary>
588 <pre><code class="python">def encipher_letter(self, letter):
589 &#34;&#34;&#34;Enciphers a single letter, by advancing the wheel before looking up
590 the letter on the wheel.
591
592 &gt;&gt;&gt; pe.set_position(&#39;f&#39;)
593 5
594 &gt;&gt;&gt; pe.encipher_letter(&#39;k&#39;)
595 &#39;h&#39;
596 &#34;&#34;&#34;
597 self.advance()
598 return self.lookup(letter)</code></pre>
599 </details>
600 </dd>
601 <dt id="szyfrow.pocket_enigma.PocketEnigma.encipher"><code class="name flex">
602 <span>def <span class="ident">encipher</span></span>(<span>self, message, starting_position=None)</span>
603 </code></dt>
604 <dd>
605 <div class="desc"><p>Enciphers a whole message.</p>
606 <pre><code class="language-python-repl">&gt;&gt;&gt; pe.set_position('f')
607 5
608 &gt;&gt;&gt; pe.encipher('helloworld')
609 'kjsglcjoqc'
610 &gt;&gt;&gt; pe.set_position('f')
611 5
612 &gt;&gt;&gt; pe.encipher('kjsglcjoqc')
613 'helloworld'
614 &gt;&gt;&gt; pe.encipher('helloworld', starting_position = 'x')
615 'egrekthnnf'
616 </code></pre></div>
617 <details class="source">
618 <summary>
619 <span>Expand source code</span>
620 </summary>
621 <pre><code class="python">def encipher(self, message, starting_position=None):
622 &#34;&#34;&#34;Enciphers a whole message.
623
624 &gt;&gt;&gt; pe.set_position(&#39;f&#39;)
625 5
626 &gt;&gt;&gt; pe.encipher(&#39;helloworld&#39;)
627 &#39;kjsglcjoqc&#39;
628 &gt;&gt;&gt; pe.set_position(&#39;f&#39;)
629 5
630 &gt;&gt;&gt; pe.encipher(&#39;kjsglcjoqc&#39;)
631 &#39;helloworld&#39;
632 &gt;&gt;&gt; pe.encipher(&#39;helloworld&#39;, starting_position = &#39;x&#39;)
633 &#39;egrekthnnf&#39;
634 &#34;&#34;&#34;
635 if starting_position:
636 self.set_position(starting_position)
637 transformed = &#39;&#39;
638 for l in message:
639 transformed += self.encipher_letter(l)
640 return transformed</code></pre>
641 </details>
642 </dd>
643 <dt id="szyfrow.pocket_enigma.PocketEnigma.encipher_letter"><code class="name flex">
644 <span>def <span class="ident">encipher_letter</span></span>(<span>self, letter)</span>
645 </code></dt>
646 <dd>
647 <div class="desc"><p>Enciphers a single letter, by advancing the wheel before looking up
648 the letter on the wheel.</p>
649 <pre><code class="language-python-repl">&gt;&gt;&gt; pe.set_position('f')
650 5
651 &gt;&gt;&gt; pe.encipher_letter('k')
652 'h'
653 </code></pre></div>
654 <details class="source">
655 <summary>
656 <span>Expand source code</span>
657 </summary>
658 <pre><code class="python">def encipher_letter(self, letter):
659 &#34;&#34;&#34;Enciphers a single letter, by advancing the wheel before looking up
660 the letter on the wheel.
661
662 &gt;&gt;&gt; pe.set_position(&#39;f&#39;)
663 5
664 &gt;&gt;&gt; pe.encipher_letter(&#39;k&#39;)
665 &#39;h&#39;
666 &#34;&#34;&#34;
667 self.advance()
668 return self.lookup(letter)</code></pre>
669 </details>
670 </dd>
671 <dt id="szyfrow.pocket_enigma.PocketEnigma.lookup"><code class="name flex">
672 <span>def <span class="ident">lookup</span></span>(<span>self, letter)</span>
673 </code></dt>
674 <dd>
675 <div class="desc"><p>Look up what a letter enciphers to, without turning the wheel.</p>
676 <pre><code class="language-python-repl">&gt;&gt;&gt; pe.set_position('f')
677 5
678 &gt;&gt;&gt; cat([pe.lookup(l) for l in string.ascii_lowercase])
679 'udhbfejcpgmokrliwntsayqzvx'
680 &gt;&gt;&gt; pe.lookup('A')
681 ''
682 </code></pre></div>
683 <details class="source">
684 <summary>
685 <span>Expand source code</span>
686 </summary>
687 <pre><code class="python">def lookup(self, letter):
688 &#34;&#34;&#34;Look up what a letter enciphers to, without turning the wheel.
689
690 &gt;&gt;&gt; pe.set_position(&#39;f&#39;)
691 5
692 &gt;&gt;&gt; cat([pe.lookup(l) for l in string.ascii_lowercase])
693 &#39;udhbfejcpgmokrliwntsayqzvx&#39;
694 &gt;&gt;&gt; pe.lookup(&#39;A&#39;)
695 &#39;&#39;
696 &#34;&#34;&#34;
697 if letter in string.ascii_lowercase:
698 return unpos(
699 (self.wheel_map[(pos(letter) - self.position) % 26] +
700 self.position))
701 else:
702 return &#39;&#39;</code></pre>
703 </details>
704 </dd>
705 <dt id="szyfrow.pocket_enigma.PocketEnigma.make_wheel_map"><code class="name flex">
706 <span>def <span class="ident">make_wheel_map</span></span>(<span>self, wheel_spec)</span>
707 </code></dt>
708 <dd>
709 <div class="desc"><p>Expands a wheel specification from a list of letter-letter pairs
710 into a full wheel_map.</p>
711 <pre><code class="language-python-repl">&gt;&gt;&gt; pe.make_wheel_map(pe.wheel2)
712 [2, 3, 0, 1, 22, 8, 15, 12, 5, 10, 9, 13, 7, 11, 16, 6, 14, 25, 20, 21, 18, 19, 4, 24, 23, 17]
713 </code></pre></div>
714 <details class="source">
715 <summary>
716 <span>Expand source code</span>
717 </summary>
718 <pre><code class="python">def make_wheel_map(self, wheel_spec):
719 &#34;&#34;&#34;Expands a wheel specification from a list of letter-letter pairs
720 into a full wheel_map.
721
722 &gt;&gt;&gt; pe.make_wheel_map(pe.wheel2)
723 [2, 3, 0, 1, 22, 8, 15, 12, 5, 10, 9, 13, 7, 11, 16, 6, 14, 25, 20, 21, 18, 19, 4, 24, 23, 17]
724 &#34;&#34;&#34;
725 self.validate_wheel_spec(wheel_spec)
726 self.wheel_map = [0] * 26
727 for p in wheel_spec:
728 self.wheel_map[pos(p[0])] = pos(p[1])
729 self.wheel_map[pos(p[1])] = pos(p[0])
730 return self.wheel_map</code></pre>
731 </details>
732 </dd>
733 <dt id="szyfrow.pocket_enigma.PocketEnigma.set_position"><code class="name flex">
734 <span>def <span class="ident">set_position</span></span>(<span>self, position)</span>
735 </code></dt>
736 <dd>
737 <div class="desc"><p>Sets the position of the wheel, by specifying the letter the arrow
738 points to.</p>
739 <pre><code class="language-python-repl">&gt;&gt;&gt; pe.set_position('a')
740 0
741 &gt;&gt;&gt; pe.set_position('m')
742 12
743 &gt;&gt;&gt; pe.set_position('z')
744 25
745 </code></pre></div>
746 <details class="source">
747 <summary>
748 <span>Expand source code</span>
749 </summary>
750 <pre><code class="python">def set_position(self, position):
751 &#34;&#34;&#34;Sets the position of the wheel, by specifying the letter the arrow
752 points to.
753
754 &gt;&gt;&gt; pe.set_position(&#39;a&#39;)
755 0
756 &gt;&gt;&gt; pe.set_position(&#39;m&#39;)
757 12
758 &gt;&gt;&gt; pe.set_position(&#39;z&#39;)
759 25
760 &#34;&#34;&#34;
761 self.position = pos(position)
762 return self.position</code></pre>
763 </details>
764 </dd>
765 <dt id="szyfrow.pocket_enigma.PocketEnigma.validate_wheel_spec"><code class="name flex">
766 <span>def <span class="ident">validate_wheel_spec</span></span>(<span>self, wheel_spec)</span>
767 </code></dt>
768 <dd>
769 <div class="desc"><p>Validates that a wheel specificaiton will turn into a valid wheel
770 map.</p>
771 <pre><code class="language-python-repl">&gt;&gt;&gt; pe.validate_wheel_spec([])
772 Traceback (most recent call last):
773 ...
774 ValueError: Wheel specification has 0 pairs, requires 13
775 &gt;&gt;&gt; pe.validate_wheel_spec([('a', 'b', 'c')]*13)
776 Traceback (most recent call last):
777 ...
778 ValueError: Not all mappings in wheel specificationhave two elements
779 &gt;&gt;&gt; pe.validate_wheel_spec([('a', 'b')]*13)
780 Traceback (most recent call last):
781 ...
782 ValueError: Wheel specification does not contain 26 letters
783 </code></pre></div>
784 <details class="source">
785 <summary>
786 <span>Expand source code</span>
787 </summary>
788 <pre><code class="python">def validate_wheel_spec(self, wheel_spec):
789 &#34;&#34;&#34;Validates that a wheel specificaiton will turn into a valid wheel
790 map.
791
792 &gt;&gt;&gt; pe.validate_wheel_spec([])
793 Traceback (most recent call last):
794 ...
795 ValueError: Wheel specification has 0 pairs, requires 13
796 &gt;&gt;&gt; pe.validate_wheel_spec([(&#39;a&#39;, &#39;b&#39;, &#39;c&#39;)]*13)
797 Traceback (most recent call last):
798 ...
799 ValueError: Not all mappings in wheel specificationhave two elements
800 &gt;&gt;&gt; pe.validate_wheel_spec([(&#39;a&#39;, &#39;b&#39;)]*13)
801 Traceback (most recent call last):
802 ...
803 ValueError: Wheel specification does not contain 26 letters
804 &#34;&#34;&#34;
805 if len(wheel_spec) != 13:
806 raise ValueError(&#34;Wheel specification has {} pairs, requires 13&#34;.
807 format(len(wheel_spec)))
808 for p in wheel_spec:
809 if len(p) != 2:
810 raise ValueError(&#34;Not all mappings in wheel specification&#34;
811 &#34;have two elements&#34;)
812 if len(set([p[0] for p in wheel_spec] +
813 [p[1] for p in wheel_spec])) != 26:
814 raise ValueError(&#34;Wheel specification does not contain 26 letters&#34;)</code></pre>
815 </details>
816 </dd>
817 </dl>
818 </dd>
819 </dl>
820 </section>
821 </article>
822 <nav id="sidebar">
823 <h1>Index</h1>
824 <div class="toc">
825 <ul></ul>
826 </div>
827 <ul id="index">
828 <li><h3>Super-module</h3>
829 <ul>
830 <li><code><a title="szyfrow" href="index.html">szyfrow</a></code></li>
831 </ul>
832 </li>
833 <li><h3><a href="#header-functions">Functions</a></h3>
834 <ul class="">
835 <li><code><a title="szyfrow.pocket_enigma.cat" href="#szyfrow.pocket_enigma.cat">cat</a></code></li>
836 <li><code><a title="szyfrow.pocket_enigma.lcat" href="#szyfrow.pocket_enigma.lcat">lcat</a></code></li>
837 <li><code><a title="szyfrow.pocket_enigma.pocket_enigma_break_by_crib" href="#szyfrow.pocket_enigma.pocket_enigma_break_by_crib">pocket_enigma_break_by_crib</a></code></li>
838 <li><code><a title="szyfrow.pocket_enigma.wcat" href="#szyfrow.pocket_enigma.wcat">wcat</a></code></li>
839 </ul>
840 </li>
841 <li><h3><a href="#header-classes">Classes</a></h3>
842 <ul>
843 <li>
844 <h4><code><a title="szyfrow.pocket_enigma.PocketEnigma" href="#szyfrow.pocket_enigma.PocketEnigma">PocketEnigma</a></code></h4>
845 <ul class="two-column">
846 <li><code><a title="szyfrow.pocket_enigma.PocketEnigma.advance" href="#szyfrow.pocket_enigma.PocketEnigma.advance">advance</a></code></li>
847 <li><code><a title="szyfrow.pocket_enigma.PocketEnigma.decipher" href="#szyfrow.pocket_enigma.PocketEnigma.decipher">decipher</a></code></li>
848 <li><code><a title="szyfrow.pocket_enigma.PocketEnigma.decipher_letter" href="#szyfrow.pocket_enigma.PocketEnigma.decipher_letter">decipher_letter</a></code></li>
849 <li><code><a title="szyfrow.pocket_enigma.PocketEnigma.encipher" href="#szyfrow.pocket_enigma.PocketEnigma.encipher">encipher</a></code></li>
850 <li><code><a title="szyfrow.pocket_enigma.PocketEnigma.encipher_letter" href="#szyfrow.pocket_enigma.PocketEnigma.encipher_letter">encipher_letter</a></code></li>
851 <li><code><a title="szyfrow.pocket_enigma.PocketEnigma.lookup" href="#szyfrow.pocket_enigma.PocketEnigma.lookup">lookup</a></code></li>
852 <li><code><a title="szyfrow.pocket_enigma.PocketEnigma.make_wheel_map" href="#szyfrow.pocket_enigma.PocketEnigma.make_wheel_map">make_wheel_map</a></code></li>
853 <li><code><a title="szyfrow.pocket_enigma.PocketEnigma.set_position" href="#szyfrow.pocket_enigma.PocketEnigma.set_position">set_position</a></code></li>
854 <li><code><a title="szyfrow.pocket_enigma.PocketEnigma.validate_wheel_spec" href="#szyfrow.pocket_enigma.PocketEnigma.validate_wheel_spec">validate_wheel_spec</a></code></li>
855 </ul>
856 </li>
857 </ul>
858 </li>
859 </ul>
860 </nav>
861 </main>
862 <footer id="footer">
863 <p>Generated by <a href="https://pdoc3.github.io/pdoc"><cite>pdoc</cite> 0.9.2</a>.</p>
864 </footer>
865 </body>
866 </html>