-
Notifications
You must be signed in to change notification settings - Fork 0
/
tattoos
executable file
·113 lines (103 loc) · 4.24 KB
/
tattoos
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys, ctypes, collections, argparse
from extras import ColoredArgParser
from extras import PrettyTable
from extras import humanizedbin
from extras import humanizedoct
from extras import humanizedhex
from extras import humanizeddec
from extras import humanizedtwoscompbin
from extras import ColoredSetting
from extras import colorize
from extras import RingLooper
def parse_args():
parser = ColoredArgParser(description = 'Tattooing the magic numbers.')
parser.add_argument('--color',
choices = ['auto', 'always', 'never'],
default = 'auto',
dest = 'colored',
help = 'When to colored, the default is auto.')
parser.add_argument('-z', '--zen-of-python',
action = 'store_true',
help = 'The Zen of Python Colorful version.')
return parser.parse_args()
integer_limits = (
('INT8_MIN', -128),
('INT8_MAX', 127),
('UINT8_MAX', 255),
('INT16_MIN', -32768),
('INT16_MAX', 32767),
('UINT16_MAX', 65535),
('INT32_MIN', -2147483648),
('INT32_MAX', 2147483647),
('UINT32_MAX', 4294967295),
('INT64_MIN', -9223372036854775808),
('INT64_MAX', 9223372036854775807),
('UINT64_MAX', 18446744073709551615),
)
def list_limits_for_integer():
pt = PrettyTable(enable_painting = ColoredSetting().is_colorize(sys.stdout))
pt.add_field('Constant', alignment = '>')
pt.add_field('Decimal', alignment = '>')
pt.add_field('Binary', alignment = '>')
pt.add_field('Octal', alignment = '>')
pt.add_field('Hexadecimal', alignment = '>')
for k, v in integer_limits:
pt.add_record(k, humanizeddec(v), humanizedbin(v), humanizedoct(v), humanizedhex(v))
print(pt)
def list_pow2n(base = 2, maxexponent = 64):
pt = PrettyTable(enable_painting = ColoredSetting().is_colorize(sys.stdout))
pt.add_field('Power of 2', alignment = '>')
pt.add_field('Decimal', alignment = '>')
pt.add_field('Binary', alignment = '>')
pt.add_field('Octal', alignment = '>')
pt.add_field('Hexadecimal', alignment = '>')
for i in range(maxexponent + 1):
res = base ** i
pt.add_record('{:d} ** {:d}'.format(base, i), humanizeddec(res), humanizedbin(res), humanizedoct(res), humanizedhex(res))
print(pt)
def list_twos_complement(ranger = range(15, -16, -1), bits = 4):
pt = PrettyTable(enable_painting = ColoredSetting().is_colorize(sys.stdout))
pt.add_field('Decimal', alignment = '>')
pt.add_field("Two's complement", alignment = '>')
pt.add_field('Octal', alignment = '>')
pt.add_field('Hexadecimal', alignment = '>')
for i in ranger:
pt.add_record(i, humanizedtwoscompbin(i, bits = bits), humanizedoct(i), humanizedhex(i))
print(pt)
_palette_paints = ( { 'fgcolor' : 'red', 'set' : ( 'bold', ) },
{ 'fgcolor' : 'green', 'set' : ( 'bold', ) },
{ 'fgcolor' : 'yellow', 'set' : ( 'bold', ) },
{ 'fgcolor' : 'blue', 'set' : ( 'bold', ) },
{ 'fgcolor' : 'magenta', 'set' : ( 'bold', ) },
{ 'fgcolor' : 'cyan', 'set' : ( 'bold', ) },
{ 'fgcolor' : 'light red', 'set' : ( 'bold', ) },
{ 'fgcolor' : 'light green', 'set' : ( 'bold', ) },
{ 'fgcolor' : 'light yellow', 'set' : ( 'bold', ) },
{ 'fgcolor' : 'light blue', 'set' : ( 'bold', ) },
{ 'fgcolor' : 'light magenta', 'set' : ( 'bold', ) },
{ 'fgcolor' : 'light cyan', 'set' : ( 'bold', ) }, )
def zop():
stdoutbak, sys.stdout = sys.stdout, None
from this import s
sys.stdout = stdoutbak
paletteiter = iter(RingLooper(*_palette_paints))
d = {}
for c in (65, 97):
for i in range(26):
d[chr(i+c)] = chr((i+13) % 26 + c)
for l in ''.join([d.get(c, c) for c in s]).splitlines():
print(colorize(l, enabling = ColoredSetting().is_colorize(sys.stdout), **next(paletteiter)))
def main(args = None):
if args is None:
return 0
ColoredSetting(args.colored)
if args.zen_of_python:
zop()
else:
list_limits_for_integer()
list_pow2n()
list_twos_complement()
if __name__ == '__main__':
sys.exit(main(parse_args()))