This repository has been archived by the owner on Jun 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscEncoder.py
61 lines (53 loc) · 1.61 KB
/
scEncoder.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
"""
The scratchconnect Encoder File
"""
import string
ALL_CHARS = list(string.ascii_uppercase + string.ascii_lowercase +
string.digits + string.punctuation + ' ')
class Encoder:
"""
DON'T USE THIS
"""
def __init__(self):
pass
def encode(self, text):
text = str(text)
number = ""
for i in range(0, len(text)):
char = text[i]
index = str(ALL_CHARS.index(char) + 1)
if int(index) < 10:
index = '0' + index
number += index
return number
def decode(self, encoded_code):
encoded_code = str(encoded_code)
i = 0
text = ""
while i < int(len(encoded_code) - 1 / 2):
index = int(encoded_code[i] + encoded_code[i + 1]) - 1
text += ALL_CHARS[index]
i += 2
return text
def encode_list(self, data):
if type(data) != list:
raise TypeError(
"To encode a list, the data should be in list form. To encode a text use the encode() function")
encoded = ""
for i in data:
encoded += f"{self.encode(i)}00"
return encoded
def decode_list(self, encoded_list_data):
decoded = []
i = 0
text = ""
while i < int(len(encoded_list_data) - 1 / 2):
code = encoded_list_data[i] + encoded_list_data[i + 1]
index = int(code) - 1
if code == "00":
decoded.append(text)
text = ""
else:
text += ALL_CHARS[index]
i += 2
return decoded