Skip to content

Commit 96fe4e6

Browse files
committed
feat: Format code, Add doc strings, Add custom exception
Added InvalidInput exception that is raised when input is invalid Used string libary to make the conversion table a bit cleaner Added doc strings and type hints for functions so easy ide support Formatted code with black
1 parent 8b57af6 commit 96fe4e6

File tree

1 file changed

+63
-17
lines changed

1 file changed

+63
-17
lines changed

base69/base69.py

Lines changed: 63 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,71 @@
1+
import string
12

2-
convertion_table = {0:'0',1:'1',2:'2',3:'3',4:'4',5:'5',6:'6',7:'7',8:'8',9:'9',10:'A',11:'B',12:'C',13:'D',14:'E',15:'F',16:'G',17:'H',18:'I',19:'J',20:'K',21:'L',22:'M',23:'N',24:'O',25:'P',26:'Q',27:'R',28:'S',29:'T',30:'U',31:'V',32:'W',33:'X',34:'Y',35:'Z',36:'a',37:'b',38:'c',39:'d',40:'e',41:'f',42:'g',43:'h',44:'i',45:'j',46:'k',47:'l',48:'m',49:'n',50:'o',51:'p',52:'q',53:'r',54:'s',55:'t',56:'u',57:'v',58:'w',59:'x',60:'y',61:'z',62:'+',63:'/',64:'=',65:'@',66:'*',67:'-',68:'!',69:'#'}
3+
__all__ = ["encode_base69", "decode_base69", "InvalidInput"]
34

4-
def encode_base69(input):
5-
if type(input) == int:
6-
base69 = ''
7-
while input > 0:
8-
remainder = input % 70
9-
base69 = convertion_table[remainder] + base69
10-
input = input // 70
11-
base69 = '69*|' + base69
12-
return base69
13-
elif type(input) == str:
14-
return "We only support integers for now"
5+
values = (
6+
list(string.digits)
7+
+ list(string.ascii_uppercase)
8+
+ list(string.ascii_lowercase)
9+
+ ["+", "/", "=", "@", "*", "-", "!", "#"]
10+
)
11+
CONVERSION_TABLE = dict(zip(range(len(values)), values))
1512

1613

17-
def decode_base69(input):
14+
class InvalidInput(Exception):
15+
def __init__(self, *args: object) -> None:
16+
super().__init__(*args)
17+
18+
19+
def encode_base69(input: int) -> str:
20+
"""
21+
Encode an integer to Base69
22+
23+
Parameters:
24+
input (`int`): The number to convert
25+
26+
Returns:
27+
`str`: The encoded Base69 result
28+
29+
Raises:
30+
`InvalidInput`: If the input is invalid
31+
"""
32+
33+
if not type(input) == int:
34+
raise InvalidInput(
35+
"Input must be an integer as we only support integers for now"
36+
)
37+
38+
base69 = ""
39+
while input > 0:
40+
remainder = input % 70
41+
base69 = CONVERSION_TABLE[remainder] + base69
42+
input = input // 70
43+
base69 = "69*|" + base69
44+
return base69
45+
46+
47+
def decode_base69(input: str) -> int:
48+
"""
49+
Decode Base69 text to an integer
50+
51+
Parameters:
52+
input (`str`): the Base69 to decode
53+
54+
Returns:
55+
`int`: The decoded integer result
56+
57+
Raises:
58+
`InvalidInput`: If the input is invalid
59+
"""
60+
61+
if not input.startswith("69*|"):
62+
raise InvalidInput("Invalid Base69 input! Base69 must begin '69*|'")
63+
1864
input = input[4:]
65+
1966
base = 0
2067
for i in range(len(input)):
21-
base += list(convertion_table.keys())[list(convertion_table.values()).index(input[i])] * (70 ** (len(input) - i - 1))
68+
base += list(CONVERSION_TABLE.keys())[
69+
list(CONVERSION_TABLE.values()).index(input[i])
70+
] * (70 ** (len(input) - i - 1))
2271
return base
23-
24-
25-

0 commit comments

Comments
 (0)