hprint 2.0.3__py3-none-any.whl → 2.0.5__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- hprint/__init__.py +46 -18
- {hprint-2.0.3.dist-info → hprint-2.0.5.dist-info}/METADATA +1 -1
- hprint-2.0.5.dist-info/RECORD +6 -0
- {hprint-2.0.3.dist-info → hprint-2.0.5.dist-info}/WHEEL +1 -1
- hprint-2.0.3.dist-info/RECORD +0 -6
- {hprint-2.0.3.dist-info → hprint-2.0.5.dist-info}/LICENSE +0 -0
- {hprint-2.0.3.dist-info → hprint-2.0.5.dist-info}/top_level.txt +0 -0
hprint/__init__.py
CHANGED
@@ -4,13 +4,16 @@ import json
|
|
4
4
|
from pprint import pformat
|
5
5
|
import os
|
6
6
|
import traceback
|
7
|
+
import logging
|
8
|
+
import textwrap
|
7
9
|
|
8
10
|
_print = partial(print, flush=True)
|
9
11
|
|
10
|
-
HPRINT_WRAP = os.getenv("HPRINT_WRAP", 50)
|
12
|
+
HPRINT_WRAP = int(os.getenv("HPRINT_WRAP", 50))
|
11
13
|
|
12
|
-
|
14
|
+
logger = logging.getLogger(__name__)
|
13
15
|
|
16
|
+
HPRINT_DEBUG = os.getenv("HPRINT_DEBUG")
|
14
17
|
|
15
18
|
__all__ = ['pretty_print', 'hprint']
|
16
19
|
|
@@ -26,7 +29,10 @@ def json_print(data):
|
|
26
29
|
try:
|
27
30
|
_print(json.dumps([dict(d) for d in data], indent=4, sort_keys=True))
|
28
31
|
except Exception:
|
29
|
-
|
32
|
+
try:
|
33
|
+
_pprint([dict(d) for d in data])
|
34
|
+
except Exception:
|
35
|
+
_pprint(data)
|
30
36
|
else:
|
31
37
|
_pprint(data)
|
32
38
|
|
@@ -41,10 +47,14 @@ def _chain_get(data, chain, default=None):
|
|
41
47
|
return result.get(attrs[-1], default)
|
42
48
|
|
43
49
|
|
44
|
-
def _get(obj, key, default='
|
50
|
+
def _get(obj, key, default='[none]'):
|
45
51
|
if not key:
|
46
52
|
return obj
|
47
|
-
return _chain_get(obj, key, default)
|
53
|
+
# return _chain_get(obj, key, default)
|
54
|
+
result = _chain_get(obj, key, default)
|
55
|
+
if result is None:
|
56
|
+
return default
|
57
|
+
return result
|
48
58
|
|
49
59
|
|
50
60
|
def tabulate_numbered_print(data, mappings, offset=0):
|
@@ -73,25 +83,42 @@ def _len(x):
|
|
73
83
|
return min(len(str(x)), HPRINT_WRAP)
|
74
84
|
|
75
85
|
|
86
|
+
def _indent(s: str, indent_cols, max_cols):
|
87
|
+
lines = s.splitlines()
|
88
|
+
if len(lines) <= 1:
|
89
|
+
lines = textwrap.wrap(s, max_cols)
|
90
|
+
if len(lines) <= 1:
|
91
|
+
return s.ljust(max_cols)
|
92
|
+
return lines[0] + '\n' + '\n'.join([(' ' * indent_cols + "| " + line) for line in lines[1:]])
|
93
|
+
|
94
|
+
|
76
95
|
def x_print(records, headers, offset=0, header=True):
|
77
96
|
headers = list(headers)
|
78
97
|
left_max_len = max(len(max(headers, key=len)), len(f"-[ RECORD {len(records)} ]-")) + 1
|
79
98
|
right_max_len = max(_len(max(record, key=_len)) for record in records) + 1
|
99
|
+
output = []
|
80
100
|
for i, record in enumerate(records, 1 + offset):
|
81
101
|
if header:
|
82
|
-
|
102
|
+
output.append(f'-[ RECORD {i} ]'.ljust(left_max_len, '-') + '+' + '-' * right_max_len)
|
103
|
+
# _print(f'-[ RECORD {i} ]'.ljust(left_max_len, '-') + '+' + '-' * right_max_len)
|
83
104
|
for j, v in enumerate(record):
|
84
|
-
_print(f'{headers[j]}'.ljust(left_max_len) + '| ' + str(v).ljust(right_max_len))
|
105
|
+
# _print(f'{headers[j]}'.ljust(left_max_len) + '| ' + str(v).ljust(right_max_len))
|
106
|
+
output.append(f'{headers[j]}'.ljust(left_max_len) + '| ' + _indent(str(v), left_max_len, right_max_len))
|
107
|
+
# _print(f'{headers[j]}'.ljust(left_max_len) + '| ' + _indent(str(v), left_max_len, right_max_len))
|
108
|
+
return os.linesep.join(output)
|
85
109
|
|
86
110
|
|
87
111
|
def tabulate_print(data, mappings, x=False, offset=0, header=True, raw=False, tf='simple'):
|
88
112
|
if not data:
|
89
113
|
return
|
90
114
|
if not mappings:
|
91
|
-
|
115
|
+
mappings = {}
|
116
|
+
# keys = set()
|
92
117
|
for entry in data:
|
93
|
-
keys = keys.union(entry.keys())
|
94
|
-
|
118
|
+
# keys = keys.union(entry.keys())
|
119
|
+
for k in entry.keys():
|
120
|
+
mappings[k] = k
|
121
|
+
# mappings = {k: k for k in keys}
|
95
122
|
# entry_with_most_keys = max(data, key=len)
|
96
123
|
# mappings = {k: k for k in entry_with_most_keys.keys()}
|
97
124
|
headers = mappings.keys()
|
@@ -107,15 +134,15 @@ def tabulate_print(data, mappings, x=False, offset=0, header=True, raw=False, tf
|
|
107
134
|
attrs.append(_get(item, k))
|
108
135
|
tabdata.append(attrs)
|
109
136
|
if x:
|
110
|
-
x_print(tabdata, headers, offset=offset, header=header)
|
137
|
+
output = x_print(tabdata, headers, offset=offset, header=header)
|
111
138
|
else:
|
112
139
|
output = tabulate(tabdata, headers=headers if header else (), tablefmt=tf)
|
113
|
-
|
114
|
-
|
115
|
-
|
140
|
+
if raw:
|
141
|
+
return output
|
142
|
+
_print(output)
|
116
143
|
|
117
144
|
|
118
|
-
def hprint(data, *, mappings=None, json_format=False, as_json=False, x=False, offset=0, numbered=False, missing_value='
|
145
|
+
def hprint(data, *, mappings=None, json_format=False, as_json=False, x=False, offset=0, numbered=False, missing_value='[none]', tf='simple', header=True, raw=False):
|
119
146
|
as_json = as_json or json_format
|
120
147
|
if not data:
|
121
148
|
return
|
@@ -124,16 +151,17 @@ def hprint(data, *, mappings=None, json_format=False, as_json=False, x=False, of
|
|
124
151
|
_get = partial(_get, default=missing_value)
|
125
152
|
try:
|
126
153
|
if as_json:
|
154
|
+
if raw:
|
155
|
+
return data
|
127
156
|
json_print(data)
|
128
157
|
elif not x and numbered:
|
129
158
|
tabulate_numbered_print(data, mappings, offset=offset)
|
130
159
|
else:
|
131
160
|
return tabulate_print(data, mappings=mappings, x=x, offset=offset, header=header, raw=raw, tf=tf)
|
132
161
|
except Exception:
|
133
|
-
if HPRINT_DEBUG:
|
134
|
-
traceback.print_exc()
|
135
|
-
|
136
162
|
json_print(data)
|
163
|
+
if HPRINT_DEBUG or logger.isEnabledFor(logging.DEBUG):
|
164
|
+
traceback.print_exc()
|
137
165
|
finally:
|
138
166
|
_get = _get0
|
139
167
|
|
@@ -0,0 +1,6 @@
|
|
1
|
+
hprint/__init__.py,sha256=oicJxWP69JCK_LQquban44ty2yAA4HWALdEJu3ShADQ,5204
|
2
|
+
hprint-2.0.5.dist-info/LICENSE,sha256=5IASoCg1AtQddnqyVxltUyoadbZzUpRa_0QvUB-Hwlg,1065
|
3
|
+
hprint-2.0.5.dist-info/METADATA,sha256=X828ivFl0Fl6MnAO0aT9jPEYp9k29NC3Hf0CPE8IUkw,5298
|
4
|
+
hprint-2.0.5.dist-info/WHEEL,sha256=pkctZYzUS4AYVn6dJ-7367OJZivF2e8RA9b_ZBjif18,92
|
5
|
+
hprint-2.0.5.dist-info/top_level.txt,sha256=yHUSDt5IDs8J8taLMOu-SaOZK24DyzM0TidniiEJ-Ng,7
|
6
|
+
hprint-2.0.5.dist-info/RECORD,,
|
hprint-2.0.3.dist-info/RECORD
DELETED
@@ -1,6 +0,0 @@
|
|
1
|
-
hprint/__init__.py,sha256=VZ1QdYaueU3MFNUCxBukTDR18eu-w8vtUMoo21cLgI8,4072
|
2
|
-
hprint-2.0.3.dist-info/LICENSE,sha256=5IASoCg1AtQddnqyVxltUyoadbZzUpRa_0QvUB-Hwlg,1065
|
3
|
-
hprint-2.0.3.dist-info/METADATA,sha256=HaWnEdA3Pymi2ZCLEJTV4a4o39IJCrLm5B16MFVCnJs,5298
|
4
|
-
hprint-2.0.3.dist-info/WHEEL,sha256=2wepM1nk4DS4eFpYrW1TTqPcoGNfHhhO_i5m4cOimbo,92
|
5
|
-
hprint-2.0.3.dist-info/top_level.txt,sha256=yHUSDt5IDs8J8taLMOu-SaOZK24DyzM0TidniiEJ-Ng,7
|
6
|
-
hprint-2.0.3.dist-info/RECORD,,
|
File without changes
|
File without changes
|