-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.py
101 lines (69 loc) · 2.29 KB
/
common.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
MAX_NUMBER = 300
MIN_NUMBER = 1
MAX_LIST_LENGTH = 1000
TEST_FOLDER = 'TEST'
SERIALIZE_POS0_TABLE = {}
SERIALIZE_POS1_TABLE = {}
DESERIALIZE_POS0_TABLE = {}
DESERIALIZE_POS1_TABLE = {}
SERIALIZE_POS0_TABLE_SIZE = 0
SERIALIZE_POS1_TABLE_SIZE = 0
SERIALIZE_ITEM_DELIMETER = ' '
class CommandLineError(Exception):
pass
class ParseError(Exception):
def __init__(self, msg):
self.msg = msg
def __str__(self):
return self.msg
def create_serialize_pos0_table():
chars_list = concat_lists(rangeChars('A', 'Z'),
rangeChars('a', 'z'))
return create_serialize_table(chars_list)
def create_serialize_pos1_table():
chars_list = rangeChars('0', '9')
return create_serialize_table(chars_list)
def create_deserialize_pos0_table():
return create_deserialize_table(SERIALIZE_POS0_TABLE)
def create_deserialize_pos1_table():
return create_deserialize_table(SERIALIZE_POS1_TABLE)
def create_deserialize_table(serialize_table):
deserialize_table = {}
for key in serialize_table:
value = serialize_table[key]
deserialize_table[value] = key
return deserialize_table
def rangeChars(fromChar, toChar):
result = []
for charOrd in xrange(ord(fromChar), ord(toChar) + 1):
result.append(chr(charOrd))
return result
def concat_lists(*lists):
result = []
for curr_list in lists:
for element in curr_list:
result.append(element)
return result
def create_serialize_table(chars_list):
table = {}
index = 0
for curr_char in chars_list:
table[index] = curr_char
index += 1
return table
def is_numeric(text):
return text.isdigit()
def init():
global SERIALIZE_POS0_TABLE, \
SERIALIZE_POS1_TABLE, \
DESERIALIZE_POS0_TABLE, \
DESERIALIZE_POS1_TABLE, \
SERIALIZE_POS0_TABLE_SIZE, \
SERIALIZE_POS1_TABLE_SIZE
SERIALIZE_POS0_TABLE = create_serialize_pos0_table()
SERIALIZE_POS1_TABLE = create_serialize_pos1_table()
DESERIALIZE_POS0_TABLE = create_deserialize_pos0_table()
DESERIALIZE_POS1_TABLE = create_deserialize_pos1_table()
SERIALIZE_POS0_TABLE_SIZE = len(SERIALIZE_POS0_TABLE)
SERIALIZE_POS1_TABLE_SIZE = len(SERIALIZE_POS1_TABLE)
init()