-
Notifications
You must be signed in to change notification settings - Fork 1
/
uniutils.py
204 lines (179 loc) · 5.35 KB
/
uniutils.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!coding:utf-8
import unicodedata
import sys
__all__ = ('uniname', 'hexord', 'hexaord', 'uord', 'uordname', 'uniline', 'unicontext', 'unibasecat', 'unilinecat', 'unibasecatverbose', 'unicatverbose', 'unilineverbose')
lmap = map if sys.version_info[0] < 3 else lambda *a, **b: list(map(*a, **b))
COLOR_TERMINAL = True
def greenize(s):
return "\033[32m{}\033[0m".format(s) if COLOR_TERMINAL else s
def greenizebold(s):
return "\033[32;1m{}\033[0m".format(s) if COLOR_TERMINAL else s
# decorator
def multiple_makes_lmap(f):
# return lambda s: f(s) if len(s) == 1 else lmap(f, s)
# return lambda s, *args: f(s, *args) if len(s) == 1 else [f(x, *args) for x in s]
from functools import wraps
@wraps(f)
def new(s, *args):
if not hasattr(s, '__iter__'):
return f(s, *args)
R = [f(x, *args) for x in s]
if len(R) == 1:
return R[0]
return R
return new
@multiple_makes_lmap
def uniname(s):
"""
>>> uniname('é')
'LATIN SMALL LETTER E WITH ACUTE'
>>> uniname('αβγ')
['GREEK SMALL LETTER ALPHA', 'GREEK SMALL LETTER BETA', 'GREEK SMALL LETTER GAMMA']
"""
return (unicodedata.name(s, '?') if unicodedata.category(s) != 'Cc' else
unicodedata.name(chr(0x2400 + ord(s))).replace('SYMBOL FOR', '<CONTROL>') if ord(s) < 32 else
'<CONTROL> DELETE' if ord(s) == 127 else
'<CONTROL>')
@multiple_makes_lmap
def hexord(s):
"""
>>> hexord('é')
'0xe9'
>>> hexord('αβγ')
['0x3b1', '0x3b2', '0x3b3']
"""
return hex(ord(s))
@multiple_makes_lmap
def hexaord(s):
"""
>>> hexaord('é')
'e9'
>>> hexaord('αβγ')
['3b1', '3b2', '3b3']
"""
return format(ord(s), 'x') # = hex(ord(s))[2:]
@multiple_makes_lmap
def uord(s):
"""
>>> uord('é')
'U+00E9'
>>> uord('αβγ')
['U+03B1', 'U+03B2', 'U+03B3']
"""
return 'U+{:04X}'.format(ord(s)) # = 'U+' + hex(ord(s))[2:].zfill(4).upper()
@multiple_makes_lmap
def uordname(s):
"""
>>> uordname('é')
'U+00E9 LATIN SMALL LETTER E WITH ACUTE'
>>> uordname('αβγ')
['U+03B1 GREEK SMALL LETTER ALPHA', 'U+03B2 GREEK SMALL LETTER BETA', 'U+03B3 GREEK SMALL LETTER GAMMA']
"""
return '{} {}'.format(uord(s), uniname(s))
@multiple_makes_lmap
def uniline(s):
"""
>>> uniline('é')
'U+00E9 é LATIN SMALL LETTER E WITH ACUTE'
>>> uniline('αβγ')
['U+03B1 α GREEK SMALL LETTER ALPHA', 'U+03B2 β GREEK SMALL LETTER BETA', 'U+03B3 γ GREEK SMALL LETTER GAMMA']
"""
return '{} {} {}'.format(uord(s), s, uniname(s))
@multiple_makes_lmap
def unilinecat(s):
"""
>>> unilinecat('é')
'Ll U+00E9 é LATIN SMALL LETTER E WITH ACUTE'
"""
return '{} {}'.format(unicodedata.category(s), uniline(s))
@multiple_makes_lmap
def unibasecat(s):
"""
>>> unibasecat('é')
'L'
"""
return unicodedata.category(s)[0]
CATEGORY_BASE_VERBOSE = {
'L': 'Letter',
'M': 'Mark',
'N': 'Number',
'P': 'Punctuation',
'S': 'Symbol',
'Z': 'Separator',
'C': 'Other',
}
@multiple_makes_lmap
def unibasecatverbose(s):
"""
>>> unibasecatverbose('é')
'L: Letter'
"""
cat = unicodedata.category(s)
return '{}: {}'.format(cat[0], CATEGORY_BASE_VERBOSE[cat[0]])
CATEGORY_SECOND_VERBOSE = {
'Lu': 'uppercase',
'Ll': 'lowercase',
'Lt': 'titlecase',
'Lm': 'modifier',
'Lo': 'other',
'Mn': 'nonspacing',
'Mc': 'spacing combining',
'Me': 'enclosing',
'Nd': 'decimal digit',
'Nl': 'letter',
'No': 'other',
'Pc': 'connector',
'Pd': 'dash',
'Ps': 'open',
'Pe': 'close',
'Pi': 'initial quote',
'Pf': 'final quote',
'Po': 'other',
'Sm': 'math',
'Sc': 'currency',
'Sk': 'modifier',
'So': 'other',
'Zs': 'space',
'Zl': 'line',
'Zp': 'paragraph',
'Cc': 'control',
'Cf': 'format',
'Cs': 'surrogate',
'Co': 'private use',
'Cn': 'not assigned',
}
@multiple_makes_lmap
def unicatverbose(s):
"""
>>> unicatverbose('é')
'Ll: Letter, lowercase'
"""
cat = unicodedata.category(s)
return '{}: {}, {}'.format(cat, CATEGORY_BASE_VERBOSE[cat[0]], CATEGORY_SECOND_VERBOSE[cat])
# won't raise a KeyError
@multiple_makes_lmap
def unilineverbose(s):
"""
>>> unilineverbose('é')
'U+00E9 é LATIN SMALL LETTER E WITH ACUTE (Ll: Letter, lowercase)'
"""
return '{} ({})'.format(uniline(s), unicatverbose(s))
# because of extra parameter "height", multiple_makes_lmap is ambiguous
#@multiple_makes_lmap
def unicontext(s, height=5):
name = unicodedata.name(s, '?')
print('U+' + hex(ord(s))[2:].zfill(4).upper(), s, name)
b = ord(s) >> 4 << 4
print(' ' + ' '.join('↓' if i == ord(s) % 16 else ' ' for i in range(16)))
print(' ' + ' '.join(hex(i)[2:].upper() for i in range(16)))
for n in (b + i * 16 for i in range(-height,+height+1)):
if n < 0x10: # contains \n and \t annoying to print
if n >= 0:
print(('U+' if n != b else ' →') + '0000' + 32 * ' ')
else:
print(('U+' if n != b else ' →') + hex(n)[2:].zfill(4).upper() + ' ' +
' '.join((greenizebold if n + i == ord(s) else lambda x:x)(chr(n + i))
for i in range(16)))
if __name__ == '__main__':
import doctest
doctest.testmod()