-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathascii-to-text.py
180 lines (131 loc) · 4.05 KB
/
ascii-to-text.py
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import re
from typing import Callable, List
"""
# CHANGELOG #
## 2022-01-04
### Added
- Project started because of:
- https://twitter.com/MrRichi94/status/1478367534147645446
- https://twitter.com/SaraaDeop/status/1478340161243074560
- Succesfull execution at, at least, 15:37, most likely sooner
- Implemented reverse parsing (conversion), successfully tested at 15:52
- Implement basic unit test cases and successfully passed them all at 15:58
"""
def translate(values: list, translator: Callable[[str,], str]) -> List[str]:
"""
Translate some values given a translator
values : list
The values to translate
translator : Callable[[str,], str]
The callable lambda that actually translates
returns List[str]
"""
translated = [ translator(value) for value in values ]
return translated
def convert_ascii_to_text(codes: List[str]) -> str:
"""
Converts ascii codes to text
codes : List[str]
The array of ascii codes to covert to text
returns str
"""
translator = lambda code: chr(int(code))
return translate(codes, translator)
def convert_text_to_ascii(codes: List[str]) -> str:
"""
Converts text to ascii codes
codes : List[str]
The array of text to covert to ascii codes
returns str
"""
translator = lambda code: ord(str(code))
result = translate(codes, translator)
# Parse them to string
result = [ str(code) for code in result ]
return result
def convert_binary_to_ascii(binaries: List[str], base: int = 2) -> List[str]:
"""
Converts binary codes to ascii codes
binaries : List[str]
The binaries to parse
base : int
The number base to use, two by default
returns List[str]
"""
translator = lambda code: int(code, base)
return translate(binaries, translator)
def convert_ascii_to_binary(codes: List[str]) -> List[int]:
"""
Converts ascii codes to binary codes
codes : List[str]
The ascii to parse
returns List[str]
"""
translator = lambda code: bin(int(code))
result = translate(codes, translator)
# Clean the input
result = [ str(char).replace('0b', '') for char in result ]
return result
def get_user_input(prompt: str = 'El texto a parsear: ') -> str:
"""
Gets the user input
prompt : str
The input prompt
returns str
"""
text = input(prompt)
text = text.strip()
return text
def parse_input(delimiter: str = '\s') -> str:
"""
Gets the user input and parses it to text
delimiter : str
The delimiter to use
returns str
"""
# Get the user input
text = get_user_input()
# Split the user input, to get the character codes only
splitted = re.compile(delimiter).split(text)
# The raw ASCII codes to text values
parsed = convert_ascii_to_text(splitted)
# The joined parsed ASCII characters
joined = ''.join(parsed)
return joined
def convert_input() -> str:
"""
Gets the user input and converts it to binary
returns str
"""
# Get the user input
text = get_user_input('El texto a convertir: ')
# Split the user input, to get the character codes only
splitted = text
# The raw ASCII codes to text values
parsed = convert_text_to_ascii(splitted)
# parsed = convert_ascii_to_binary(parsed)
# The joined parsed ASCII characters
joined = ' '.join(parsed)
return joined
def main() -> None:
"""
Holds the main workflow execution
returns None
"""
# result = parse_input()
result = convert_input()
print(f'El resultado ha sido: "{result}"')
def test() -> None:
"""
Creates the unit test cases
returns None
"""
assert convert_text_to_ascii('Proximamente') == [
'80', '114', '111', '120', '105', '109', '97', '109', '101', '110', '116', '101'
]
assert convert_ascii_to_text([
'80', '114', '111', '120', '105', '109', '97', '109', '101', '110', '116', '101'
]) == list('Proximamente')
if __name__ == '__main__':
test()
main()