-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprettierprint.py
141 lines (127 loc) · 5.9 KB
/
prettierprint.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
from multiprocessing import parent_process
from typing import Any
OUTLIST = []
def print(data: Any, ind_char: str=' ', ind_incr: int=6, item_sep: str='\n', top_level_sep: str='', list_item_char: str=' - ', depth: int=None, print_func=print):
"""
Print data structures in a clean, easy to read format.
:param data: Any The object to be clean-printed
:param ind_char: str The string character for showing hierarchy depth. Default: '\t'
:param item_sep: str The string character for separating individual items. Default: '\n'
:param top_level_sep: str The string to use for visually separating items in a list, set, or tuple.
:param depth: int The maximum level of hierarchy depth to display. Default: None (all levels)
:param print_func: func The print function to use. Default is Python's built-in 'print'
"""
output = format_output(data,
ind_char=ind_char,
ind_incr=ind_incr,
item_sep=item_sep,
top_level_sep=top_level_sep,
list_item_char=list_item_char,
depth=depth)
print_func(output)
def format_output(data: Any, ind_char: str=' ', ind_incr: int=6, item_sep: str='\n', top_level_sep: str='', list_item_char: str=' - ', depth: int=None) -> str:
"""
Print data structures in a clean, easy to read format.
:param data: Any The object to be clean-printed
:param ind_char: str The string character for showing hierarchy depth. Default: '\t'
:param item_sep: str The string character for separating individual items. Default: '\n'
:param top_level_sep: str The string to use for visually separating items in a list, set, or tuple.
:param depth: int The maximum level of hierarchy depth to display. Default: None (all levels)
:return str Returns a formatted string
"""
output = ''
_format(data,
ind_char=ind_char,
ind_incr=ind_incr,
top_level_sep=top_level_sep,
list_item_char=list_item_char,
depth=depth)
for o in OUTLIST:
if o == list_item_char:
output += o
else:
output += (o + item_sep)
OUTLIST.clear()
return output
def _format(data: Any, _indent: int=0, ind_char: str=' ', ind_incr: int=6, top_level_sep: str='', list_item_char: str='- ', _parent_is_list: bool=None, depth: int=None):
"""Recursive function to process an input object for printing"""
if depth:
if depth < _indent:
OUTLIST.append(f'{ind_char * _indent * ind_incr} [...]')
return
if _isstr(data) or _isnum(data):
if _parent_is_list:
OUTLIST.append(f'{ind_char * ((_indent * ind_incr) - len(list_item_char))}{list_item_char}{data}')
else:
OUTLIST.append(f'{ind_char * _indent * ind_incr}{data}')
elif type(data) is dict:
is_first = True
for k, v in data.items():
if is_first and _parent_is_list:
if _isstr(v) or _isnum(v):
OUTLIST.append(f'{ind_char * ((_indent * ind_incr) - len(list_item_char))}{list_item_char}{k}: {v}')
else:
OUTLIST.append(f'{ind_char * ((_indent * ind_incr) - len(list_item_char))}{list_item_char}{k}:')
_format(v,
_indent=(_indent+1),
ind_char=ind_char,
ind_incr=ind_incr,
top_level_sep=top_level_sep,
list_item_char=list_item_char,
_parent_is_list=_parent_is_list,
depth=depth)
is_first = False
else:
if _isstr(v) or _isnum(v):
OUTLIST.append(f'{ind_char * _indent * ind_incr}{k}: {v}')
else:
OUTLIST.append(f'{ind_char * _indent * ind_incr}{k}:')
_format(v,
_indent=(_indent+1),
ind_char=ind_char,
ind_incr=ind_incr,
top_level_sep=top_level_sep,
list_item_char=list_item_char,
_parent_is_list=_parent_is_list,
depth=depth)
elif hasattr(data, '__dict__'):
if _parent_is_list:
OUTLIST.append(f'{ind_char * ((_indent * ind_incr) - len(list_item_char))}{list_item_char}{type(data).__name__}:')
_parent_is_list = False
else:
OUTLIST.append(f'{ind_char * _indent * ind_incr}{type(data).__name__}:')
_format(data.__dict__,
_indent=(_indent+1),
ind_char=ind_char,
ind_incr=ind_incr,
top_level_sep=top_level_sep,
list_item_char=list_item_char,
_parent_is_list=_parent_is_list,
depth=depth)
elif type(data) in (list, set, tuple):
_parent_is_list = True
for i in data:
if _indent == 0:
OUTLIST.append(top_level_sep)
_parent_is_list = False
_format(i,
_indent,
ind_char=ind_char,
ind_incr=ind_incr,
top_level_sep=top_level_sep,
list_item_char=list_item_char,
_parent_is_list=_parent_is_list,
depth=depth)
else:
OUTLIST.append(f'{ind_char * _indent * ind_incr}{data}')
def _isstr(data: Any) -> bool:
return type(data) is str
def _isnum(data: Any) -> bool:
if not _isstr(data):
try:
int(data)
return True
except TypeError:
return False
else:
return False