forked from fluentpython/example-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstr_repr2.py
More file actions
50 lines (44 loc) · 1.66 KB
/
str_repr2.py
File metadata and controls
50 lines (44 loc) · 1.66 KB
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
from itertools import groupby
def bare_repr(codepoint):
return repr(chr(codepoint))[1:-1]
def display(codepoint):
repstr = repr(chr(codepoint))[1:-1]
print('U+{:04x} {:{max_len}}'.format(
codepoint, repstr, max_len=max(lengths)))
def repr_shape(codepoint):
brepr = bare_repr(codepoint)
if len(brepr) == 1:
shape = 'GLYPH'
else:
shape = brepr[:2]
escapes.add(shape)
return len(brepr), shape
escapes = set()
group_gen = groupby((codepoint for codepoint in range(0x110000)), repr_shape)
for len_shape, group in group_gen:
len_brepr, shape = len_shape
group = list(group)
cp_first = group[0]
cp_last = group[-1]
cp_mid = group[len(group)//2]
if len(group) == 1:
glyph_sample = bare_repr(cp_first) if shape == 'GLYPH' else ''
print('{:6d} U+{:04X} {:5} {}'.format(
len(group), cp_first, shape, glyph_sample))
else:
if len(group) == 2:
if shape == 'GLYPH':
glyph_sample = bare_repr(cp_first) + ' ' + bare_repr(cp_last)
else:
glyph_sample = ''
print('{:6d} U+{:04X} , U+{:04X} {:5} {}'.format(
len(group), cp_first, cp_last, shape, glyph_sample))
else:
if shape == 'GLYPH':
glyph_sample = ' '.join([bare_repr(cp_first),
bare_repr(cp_mid), bare_repr(cp_last)])
else:
glyph_sample = ''
print('{:6d} U+{:04X}...U+{:04X} {:5} {}'.format(
len(group), cp_first, cp_last, shape, glyph_sample))
print('escapes:', ' '.join(sorted(escapes, key=str.upper)))