-
Notifications
You must be signed in to change notification settings - Fork 2
/
util.py
161 lines (134 loc) · 3.84 KB
/
util.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
# coding: utf-8
"""
something useful but ..
"""
import time
def unions(sets):
"""
:param sets: 集合的列表
:return: 一些set的union
"""
return reduce(lambda x, y: x.union(y), sets, set())
def char_range(l, r):
"""
:param l: 字符左边界
:param r: 字符右边界
:return:[l, r]区间的字符
"""
return [chr(i) for i in range(ord(l), ord(r)+1)]
def parse_convert(s):
"""
:param s: 字符串
:return: 转换转义符之后的字符串
"""
s = s.replace(r"\n", "\n")
s = s.replace(r"\r", "\r")
s = s.replace(r"\t", "\t")
return s
def bnf_reader(filename='test.txt'):
"""
:param filename: 文件名
:return: 返回一个每次迭代得到一行分割之后的二元组的生成器
"""
with open(filename) as reader:
comment = False
for line in reader:
line = line[:line.find('#')] # 删除注释
line = parse_convert(line).strip()
if line == '"""' or line == "'''":
comment = not comment
if comment:
continue
if line.startswith('#') or line.find("::=") <= 0:
continue
yield tuple(line.split(" ::= "))
def frozen_item(item):
"""
:param item: 项目
:return:可hash项目
"""
return tuple([item[0], item[1], item[2], frozenset(item[3])])
def frozen_items(items):
"""
:param items: 项目集
:return: 可hash项目集
"""
return frozenset([frozen_item(item) for item in items])
def print_with_color(msg, color="Red", new_line=True):
ref = {
'Default': '\033[0m',
'Black': '\033[30m',
'Red': '\033[31m',
'Green': '\033[32m',
'Yellow': '\033[33m',
'Blue': '\033[34m',
'Magenta': '\033[35m',
'Cyan': '\033[36m',
'White': '\033[37m',
'Bold': '\033[1m',
'Reverse': '\033[2m',
'Blackbg': '\033[40m',
'Redbg': '\033[41m',
'Greenbg': '\033[42m',
'Yellowbg': '\033[43m',
'Bluebg': '\033[44m',
'Magentabg': '\033[45m',
'Cyanbg': '\033[46m',
'Whitebg': '\033[47m',
'Reset': '\033[0;0m',
}
if new_line:
print ref[color] + msg + ref['Reset']
else:
print ref[color] + msg + ref['Reset'],
def colorful(msg, fore_color="Red", back_color="Default"):
ref = {
'Default': '\033[0m',
'Black': '\033[30m',
'Red': '\033[31m',
'Green': '\033[32m',
'Yellow': '\033[33m',
'Blue': '\033[34m',
'Magenta': '\033[35m',
'Cyan': '\033[36m',
'White': '\033[37m',
'Bold': '\033[1m',
'Reverse': '\033[2m',
'Blackbg': '\033[40m',
'Redbg': '\033[41m',
'Greenbg': '\033[42m',
'Yellowbg': '\033[43m',
'Bluebg': '\033[44m',
'Magentabg': '\033[45m',
'Cyanbg': '\033[46m',
'Whitebg': '\033[47m',
'Reset': '\033[0;0m',
}
return ref.get(back_color+'bg', '\033[0m') + ref.get(fore_color, '\033[0m') + msg + ref['Reset']
def args_restore(args):
"""
:param args: 消除lexp-seq规约时产生的伪列表
:return:
"""
if type(args) == list and len(args) == 1 and type(args[0]) == tuple:
in_args = args[0]
res = []
for a in in_args:
res.append(args_restore(a))
return res
else:
return args
import sys
def cli_spinner(msg=""):
# frames = ["◐", "◓", "◑", "◒", ]
frames = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"]
x = 0
print '\33[?25l\r',
while True:
# sys.stdout.write(frames[x])
print colorful(frames[x], "Magenta"), msg,
x = (x + 1) % len(frames)
time.sleep(0.1)
sys.stdout.write('\r')
sys.stdout.flush()
# cli_spinner("foo")