-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpluglead.py
More file actions
78 lines (65 loc) · 2.88 KB
/
pluglead.py
File metadata and controls
78 lines (65 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
class PlugLead:
# Available contacts: shared between all instances
contacts = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S",
"T", "U", "V", "W", "X", "Y", "Z"}
def __init__(self, mapping):
# The input mapping must be a string
if not isinstance(mapping, str):
raise TypeError("Plug lead input must be of string type")
# And must have length 2
if len(mapping) != 2:
raise ValueError("Plug leads must be specified by two UPPERCASE characters between A and Z included")
# Make sure that each element is a valid plug board contact
if mapping[0] not in self.contacts or mapping[1] not in self.contacts:
raise ValueError(f"Plug lead contacts ({mapping[0] + mapping[1] }) are not valid. "
f"Plug leads must be specified by two UPPERCASE characters between A and Z included")
if mapping[0] == mapping[1]:
raise ValueError(f"Plug lead {mapping[0] + mapping[1]} is not valid. Contacts cannot be duplicated")
from_char = mapping[0]
to_char = mapping[1]
# Create a dictionary with back and forth mapping
self.letter_encode = {from_char: to_char, to_char: from_char}
def encode(self, char_in):
# Input validation
if not isinstance(char_in, str) or char_in not in self.contacts:
raise ValueError(f"{char_in} is not a valid input. Input must be an UPPERCASE character between A and Z included")
char_out = char_in
# If the plug lead connects the input char then map it to the other end of the lead.
if char_in in self.letter_encode:
char_out = self.letter_encode[char_in]
return char_out
def __eq__(self, other):
return any(x == y for x, y in zip(self.letter_encode.keys(), other.letter_encode.keys())) or\
any(x == y for x, y in zip(self.letter_encode.keys(), other.letter_encode.values()))
if __name__ == "__main__":
# Test correct encoding
lead = PlugLead("AG")
assert (lead.encode("A") == "G")
assert (lead.encode("D") == "D")
lead = PlugLead("DA")
assert (lead.encode("A") == "D")
assert (lead.encode("D") == "A")
# Test wrong inputs
try:
lead = PlugLead(2343)
print("Test1 failed")
except TypeError:
print("Test1 passed")
try:
lead = PlugLead("2343")
print("Test2 failed")
except ValueError:
print("Test2 passed")
try:
lead = PlugLead("&*")
print("Test3 failed")
except ValueError:
print("Test3 passed")
lead = PlugLead("AF")
testSet = ["i", "?", 12, [], ""]
for i, test in enumerate(testSet):
try:
lead.encode(test)
print(f"Test{i} encode failed")
except ValueError:
print(f"Test{i} encode passed")