Skip to content

Commit 46eda45

Browse files
committed
release version after restructure
1 parent b087e4d commit 46eda45

File tree

7 files changed

+80
-100
lines changed

7 files changed

+80
-100
lines changed

enigma/__init__.py

Lines changed: 43 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,14 @@ class Enigma:
2626
and simulating either an M3 or M4 variant of the famous WWII cipher
2727
encoding/decoding machine.
2828
"""
29-
def __init__(self, rotor_list: List[int] = None,
30-
user_reflector: str = None,
31-
debug: str = 'ERROR',
32-
enigma_type: str = 'M3') -> None:
29+
30+
def __init__(
31+
self,
32+
rotor_list: List[int] = None,
33+
user_reflector: str = None,
34+
debug: str = "ERROR",
35+
enigma_type: str = "M3",
36+
) -> None:
3337
"""
3438
Enigma Machine Class based on the Enigma Model 3 ciphering machine.
3539
@@ -45,7 +49,7 @@ def __init__(self, rotor_list: List[int] = None,
4549
debug: 'DEBUG' Set debug level, default is 'ERROR'
4650
4751
"""
48-
self.version = 'v1.2.1'
52+
self.version = "v1.2.1"
4953
self.isBeta = False
5054
self.type = enigma_type.upper()
5155

@@ -58,13 +62,13 @@ def __init__(self, rotor_list: List[int] = None,
5862
5: rotor.Rotor5(),
5963
6: rotor.Rotor6(),
6064
7: rotor.Rotor7(),
61-
8: rotor.Rotor8()
65+
8: rotor.Rotor8(),
6266
}
6367

6468
# Initialise all reflectors as objects within member dictionary
6569
self._reflector_types = {
66-
'B': reflector.ReflectorB(),
67-
'C': reflector.ReflectorC()
70+
"B": reflector.ReflectorB(),
71+
"C": reflector.ReflectorC(),
6872
}
6973

7074
# Chosen rotor set stored as an ordered dictionary
@@ -76,7 +80,7 @@ def __init__(self, rotor_list: List[int] = None,
7680
self.logger.setLevel(debug)
7781
logging.basicConfig()
7882

79-
if self.type == 'M3':
83+
if self.type == "M3":
8084
if not rotor_list:
8185
rotor_list = [5, 3, 1]
8286

@@ -85,11 +89,11 @@ def __init__(self, rotor_list: List[int] = None,
8589
except AssertionError:
8690
raise IndexError("Invalid Rotor List Argument for Enigma M3")
8791

88-
self.rotors['left'] = self._rotor_types[rotor_list[0]]
89-
self.rotors['middle'] = self._rotor_types[rotor_list[1]]
90-
self.rotors['right'] = self._rotor_types[rotor_list[2]]
92+
self.rotors["left"] = self._rotor_types[rotor_list[0]]
93+
self.rotors["middle"] = self._rotor_types[rotor_list[1]]
94+
self.rotors["right"] = self._rotor_types[rotor_list[2]]
9195

92-
elif self.type == 'M4':
96+
elif self.type == "M4":
9397
if not rotor_list:
9498
rotor_list = [5, 3, 1, 2]
9599

@@ -98,10 +102,10 @@ def __init__(self, rotor_list: List[int] = None,
98102
except AssertionError:
99103
raise IndexError("Invalid Rotor List Argument for Enigma M4")
100104

101-
self.rotors['left'] = self._rotor_types[rotor_list[0]]
102-
self.rotors['middle left'] = self._rotor_types[rotor_list[1]]
103-
self.rotors['middle right'] = self._rotor_types[rotor_list[2]]
104-
self.rotors['right'] = self._rotor_types[rotor_list[3]]
105+
self.rotors["left"] = self._rotor_types[rotor_list[0]]
106+
self.rotors["middle left"] = self._rotor_types[rotor_list[1]]
107+
self.rotors["middle right"] = self._rotor_types[rotor_list[2]]
108+
self.rotors["right"] = self._rotor_types[rotor_list[3]]
105109

106110
else:
107111
TypeError("Unrecognised Enigma type '{}'".format(self.type))
@@ -110,7 +114,7 @@ def __init__(self, rotor_list: List[int] = None,
110114
self._rotor_dict_keys = tuple(self.rotors.keys())
111115

112116
# Setup the reflector choice
113-
user_reflector = user_reflector if user_reflector else 'B'
117+
user_reflector = user_reflector if user_reflector else "B"
114118
self.reflector = self._reflector_types[user_reflector]
115119

116120
# Setup plugboard
@@ -150,7 +154,7 @@ def ringstellung(self, name: str, amount: int) -> None:
150154
j,
151155
name,
152156
letter,
153-
self.rotors[name].get_rotor_conversion(letter)
157+
self.rotors[name].get_rotor_conversion(letter),
154158
)
155159

156160
self.rotors[name].rotate_inner_ring()
@@ -160,7 +164,7 @@ def ringstellung(self, name: str, amount: int) -> None:
160164
j,
161165
name,
162166
letter,
163-
self.rotors[name].get_rotor_conversion(letter)
167+
self.rotors[name].get_rotor_conversion(letter),
164168
)
165169

166170
def _set_rotor(self, name: str, letter: str) -> None:
@@ -198,7 +202,7 @@ def _get_rotor_conv(self, name: str, letter: str) -> str:
198202
"Rotor %s conversion: %s to %s",
199203
name,
200204
letter,
201-
self.rotors[name].get_rotor_conversion(letter)
205+
self.rotors[name].get_rotor_conversion(letter),
202206
)
203207

204208
return self.rotors[name].get_rotor_conversion(letter)
@@ -222,13 +226,12 @@ def _get_rotor_conv_inv(self, name: str, letter: str) -> str:
222226
"Rotor %s conversion: %s to %s",
223227
name,
224228
letter,
225-
self.rotors[name].get_rotor_conversion_inv(letter)
229+
self.rotors[name].get_rotor_conversion_inv(letter),
226230
)
227231

228232
return self.rotors[name].get_rotor_conversion_inv(letter)
229233

230-
def _get_inter_rotor_conv(self, name1: str,
231-
name2: str, letter: str) -> str:
234+
def _get_inter_rotor_conv(self, name1: str, name2: str, letter: str) -> str:
232235
"""
233236
Find encoding of a given letter when passed between two rotors.
234237
@@ -256,23 +259,23 @@ def _get_inter_rotor_conv(self, name1: str,
256259
zero_point_2 = self.rotors[name2].alpha.index(self.rotors[name2].face)
257260

258261
# Offset between the two letter positions of the two rotors
259-
interval = zero_point_2-zero_point_1
262+
interval = zero_point_2 - zero_point_1
260263

261264
# Find the subsequent letter from the second rotor given the index on
262265
# the first taking into account the maximum index of 25
263266
if interval > 0:
264267
i = list(range(26))
265-
n = i[(terminal+interval) % len(i)]
268+
n = i[(terminal + interval) % len(i)]
266269
else:
267270
i = list(range(26))
268-
n = i[(26+terminal+interval) % len(i)]
271+
n = i[(26 + terminal + interval) % len(i)]
269272

270273
self.logger.debug(
271274
"Rotor %s rotor to %s rotor conversion: %s to %s",
272275
name1,
273276
name2,
274277
letter,
275-
self.rotors[name2].alpha[n]
278+
self.rotors[name2].alpha[n],
276279
)
277280

278281
if not self.rotors[name2].alpha[n]:
@@ -283,8 +286,7 @@ def _get_inter_rotor_conv(self, name1: str,
283286

284287
return self.rotors[name2].alpha[n]
285288

286-
def _get_inter_rotor_conv_inv(self, name1: str, name2: str,
287-
letter: str) -> str:
289+
def _get_inter_rotor_conv_inv(self, name1: str, name2: str, letter: str) -> str:
288290
"""
289291
Find inverse conversion between rotors (same as forward conversion).
290292
@@ -326,8 +328,8 @@ def type_letter(self, letter: str) -> str:
326328

327329
# TODO not sure what to call this action...
328330
for i, j in zip(
329-
reversed(self._rotor_dict_keys[1:]),
330-
reversed(self._rotor_dict_keys[:-1]),
331+
reversed(self._rotor_dict_keys[1:]),
332+
reversed(self._rotor_dict_keys[:-1]),
331333
):
332334
if self.rotors[i].face in self.rotors[i].notches:
333335
self._move_rotor(j, 1)
@@ -337,16 +339,12 @@ def type_letter(self, letter: str) -> str:
337339
# Get the rotor conversion for given key
338340
cipher = self._get_rotor_conv(rotor_key, cipher)
339341
# Get the inter rotor conversion for the key and the key-1
340-
adj_rotor_key_index = self._rotor_dict_keys.index(rotor_key)-1
342+
adj_rotor_key_index = self._rotor_dict_keys.index(rotor_key) - 1
341343
# At this point we should be ready for reflection
342344
if adj_rotor_key_index < 0:
343345
break
344346
adj_rotor_key = self._rotor_dict_keys[adj_rotor_key_index]
345-
cipher = self._get_inter_rotor_conv(
346-
rotor_key,
347-
adj_rotor_key,
348-
cipher
349-
)
347+
cipher = self._get_inter_rotor_conv(rotor_key, adj_rotor_key, cipher)
350348
else:
351349
assert False, "Shouldn't get here!"
352350

@@ -358,12 +356,10 @@ def type_letter(self, letter: str) -> str:
358356
cipher = self._get_rotor_conv_inv(rotor_key, cipher)
359357
try:
360358
# Get the inter rotor conv_inversion for the key and the key-1
361-
adj_rotor_key_index = self._rotor_dict_keys.index(rotor_key)+1
359+
adj_rotor_key_index = self._rotor_dict_keys.index(rotor_key) + 1
362360
adj_rotor_key = self._rotor_dict_keys[adj_rotor_key_index]
363361
cipher = self._get_inter_rotor_conv_inv(
364-
rotor_key,
365-
adj_rotor_key,
366-
cipher
362+
rotor_key, adj_rotor_key, cipher
367363
)
368364
except IndexError:
369365
# At this point we should be ready for reflection
@@ -410,7 +406,7 @@ def type_phrase(self, phrase: str) -> str:
410406
411407
"""
412408
# Remove spaces from the phrase string
413-
phrase = phrase.replace(' ', '')
409+
phrase = phrase.replace(" ", "")
414410

415411
# Determine how many extra characters are required in order to assemble
416412
# cipher into the expected groups of 5 letters
@@ -420,15 +416,15 @@ def type_phrase(self, phrase: str) -> str:
420416
for i in range(remainder):
421417
phrase += random.choice(string.ascii_letters.upper())
422418

423-
out_str = ''
419+
out_str = ""
424420

425421
# Encode the phrase one letter at a time assembling the results into
426422
# an output string
427423
for letter in list(phrase):
428424
out_str += self.type_letter(letter)
429425

430426
# Format the output to be groups five letters in size
431-
out_str = ' '.join(out_str[i:i+5] for i in range(0, len(out_str), 5))
427+
out_str = " ".join(out_str[i : i + 5] for i in range(0, len(out_str), 5))
432428

433429
return out_str
434430

@@ -467,9 +463,9 @@ def rewire_plugboard(self, letter_1: str, letter_2: str) -> None:
467463
assert isinstance(letter_1, str) and isinstance(letter_2, str)
468464
except AssertionError as e:
469465
self.logger.error(
470-
"Invalid Characters for Plugboard Rewiring '%s' and '%s'",
466+
"Invalid Characters for Plugboard Rewiring '%s' and '%s'",
471467
letter_1,
472-
letter_2
468+
letter_2,
473469
)
474470
raise e
475471
try:

enigma/plugboard.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class Plugboard:
1717
Class representing the Enigma plugboard component for manual letter
1818
encoding by connecting two letters via a dictionary
1919
"""
20+
2021
def __init__(self) -> None:
2122
"""Initialise a plugboard instance as a Python dictionary."""
2223

@@ -81,7 +82,7 @@ def plugboard_conversion_inv(self, letter: str) -> str:
8182
for key in self._plug_board_dict:
8283
if self._plug_board_dict[key] == letter:
8384
return key
84-
85+
8586
raise KeyError("Could not find conversion for '{}'".format(letter))
8687

8788
def rewire(self, letter_1: str, letter_2: str) -> None:

enigma/reflector.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class Reflector:
1919
Base class for construction of a reflector which sends forward encoded
2020
cipher back through enigma machine after translation.
2121
"""
22+
2223
def __init__(self) -> None:
2324
"""Initialise an instance of the reflector class."""
2425
self._reflector_dict: Dict[str, str] = {}
@@ -40,13 +41,12 @@ def reflector_conversion(self, letter: str) -> str:
4041
assert self._reflector_dict[letter]
4142
return self._reflector_dict[letter]
4243
except (KeyError, AssertionError):
43-
raise KeyError(
44-
"Could not find '{}' in Reflector Dictionary".format(letter)
45-
)
44+
raise KeyError("Could not find '{}' in Reflector Dictionary".format(letter))
4645

4746

4847
class ReflectorB(Reflector):
4948
"""Enigma Reflector Type B"""
49+
5050
def __init__(self) -> None:
5151
"""
5252
Initialise a Type B Enigma Reflector.
@@ -91,6 +91,7 @@ def __init__(self) -> None:
9191

9292
class ReflectorC(Reflector):
9393
"""Enigma Reflector Type C"""
94+
9495
def __init__(self) -> None:
9596
"""
9697
Initialise a Type C Enigma Reflector.

0 commit comments

Comments
 (0)