-
Notifications
You must be signed in to change notification settings - Fork 0
/
unicode.py
147 lines (89 loc) · 2.72 KB
/
unicode.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
#!/usr/bin/env python3
# Unicode information
from sys import argv, exit
import unicodedata
# common output
def unicode_info(number, hexnum, char):
categories = {
'Lu': 'Uppercase Letter',
'Ll': 'Lowercase Letter',
'Lt': 'Titlecase Letter',
'LC': 'Cased Letter',
'Lm': 'Modifier Letter',
'Lo': 'Other Letter',
'L' : 'Letter',
'Mn': 'Nonspacing Mark',
'Mc': 'Spacing Mark',
'Me': 'Enclosing Mark',
'M' : 'Mark',
'Nd': 'Decimal Number',
'Nl': 'Letter Number',
'No': 'Other Number',
'N' : 'Number',
'Pc': 'Connector Punctuation',
'Pd': 'Dash Punctuation',
'Ps': 'Open Punctuation',
'Pe': 'Close Punctuation',
'Pi': 'Initial Punctuation',
'Pf': 'Final Punctuation',
'Po': 'Other Punctuation',
'P' : 'Punctuation',
'Sm': 'Math Symbol',
'Sc': 'Currency Symbol',
'Sk': 'Modifier Symbol',
'So': 'Other Symbol',
'S' : 'Symbol',
'Zs': 'Space Separator',
'Zl': 'Line Separator',
'Zp': 'Paragraph Separator',
'Z' : 'Separator',
'Cc': 'Control',
'Cf': 'Format',
'Cs': 'Surrogate',
'Co': 'Private Use',
'Cn': 'Unassigned',
'C' : 'Other'
}
try: ucname = unicodedata.name(char)
except Exception: ucname = 'unknown'
try: category = categories[unicodedata.category(char)]
except Exception: category = 'unknown'
print('Unicode', number, '[hex:', hexnum, ']', 'is:', char,
'(' + category, '›', ucname + ')')
# cli arg
myname = argv.pop(0).split('/')[-1]
syntax = f'syntax: {myname} <type> <input>\n'
syntax += 'types: s (string), d (decimal), h (hex) or r (range)'
if len(argv) < 2: exit(syntax)
typ, inp = argv[0].lower(), argv[1]
if not typ in 'sdhr': exit(syntax)
match typ:
# string
case 's':
for ch in inp:
nm = ord(ch)
hx = hex(nm)
unicode_info(nm, hx, ch)
# decimal
case 'd':
try: nm = int(inp)
except Exception: exit('not a decimal number')
hx = hex(nm)
ch = chr(nm)
unicode_info(nm, hx, ch)
# hexadecimal
case 'h':
try: nm = int(inp, 16)
except Exception: exit('not a hex number')
hx = hex(nm)
ch = chr(nm)
unicode_info(nm, hx, ch)
# range
case 'r':
smin, _, smax = inp.partition('-')
try: imin, imax = int(smin,16), int(smax,16)
except Exception: exit('syntax: unicode r <hexmin>-<hexmax>')
for nm in range(imin, imax+1):
ch = chr(nm)
if ch: print(ch, end=' ')
print()