-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsevenseg.py
executable file
·74 lines (61 loc) · 1.66 KB
/
sevenseg.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
#!/usr/bin/env python3
from itertools import zip_longest
def ssplit(s, *inds):
prev = 0
for ind in inds:
yield s[prev:ind]
prev = ind
yield s[prev:]
def hsplit(string, *inds):
return list(map('\n'.join,
zip(*(ssplit(s, *inds) for s in string.splitlines()))))
DIGS = '''\
_ _ _ _ _ _ _ _ _ _ _ _
| | | _| _||_||_ |_ ||_||_||_||_ | _||_ |_
|_| ||_ _| | _||_| ||_| _|| ||_||_ |_||_ |
'''
DIGS = hsplit(DIGS, *range(3, 3*16, 3))
PATTERN = '''\
_
|_|
|_|
'''
TEMPLATE = '''\
0
561
432
'''
def hconcat(*strings):
line_lists = [s.splitlines() for s in strings]
widths = [max(map(len, lines)) for lines in line_lists]
return '\n'.join(
''.join(line.ljust(w) for line, w in zip(row, widths))
for row in zip_longest(*line_lists, fillvalue='')
)
def decode(x):
if hasattr(x, '__iter__') and not isinstance(x, str):
return hconcat(*(decode(y) for y in x))
return ''.join(
c if (not d.isdigit()) or (x >> int(d) & 1) else ' '
for c, d in zip(PATTERN, TEMPLATE)
)
def sevenseg_char(c):
if c == ' ':
return '\n'.join(' ')
if c == ':':
return '\n'.join(' ..')
if c == '.':
return '\n'.join(' .')
return DIGS[int(c, 16)]
def sevenseg_str(s):
return hconcat(*map(sevenseg_char, s))
def sevenseg_num(n, hex=False):
return sevenseg_str(format(n, 'x' if hex else 'd'))
if __name__ == '__main__':
import sys
if len(sys.argv) > 1:
for arg in sys.argv[1:]:
print(sevenseg_str(arg))
else:
for line in sys.stdin:
print(sevenseg_str(line.rstrip('\n')))