hprint 2.1.1__py3-none-any.whl → 2.1.2__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 +77 -13
- {hprint-2.1.1.dist-info → hprint-2.1.2.dist-info}/METADATA +5 -3
- hprint-2.1.2.dist-info/RECORD +8 -0
- hprint-2.1.1.dist-info/RECORD +0 -8
- {hprint-2.1.1.dist-info → hprint-2.1.2.dist-info}/WHEEL +0 -0
- {hprint-2.1.1.dist-info → hprint-2.1.2.dist-info}/licenses/LICENSE +0 -0
- {hprint-2.1.1.dist-info → hprint-2.1.2.dist-info}/top_level.txt +0 -0
hprint/__init__.py
CHANGED
@@ -1,6 +1,8 @@
|
|
1
|
+
from typing import Any, Union
|
1
2
|
from functools import partial
|
2
3
|
from tabulate import tabulate
|
3
4
|
import json
|
5
|
+
import copy
|
4
6
|
from pprint import pformat
|
5
7
|
import os
|
6
8
|
import traceback
|
@@ -8,6 +10,14 @@ import logging
|
|
8
10
|
import textwrap
|
9
11
|
from .utils import chain_get
|
10
12
|
|
13
|
+
|
14
|
+
DEFAULTS = {
|
15
|
+
'tablefmt': 'simple',
|
16
|
+
'disable_numparse': True
|
17
|
+
# 'intfmt': '',
|
18
|
+
# 'floatfmt': '',
|
19
|
+
}
|
20
|
+
|
11
21
|
_MISSING_VALUE = '[none]'
|
12
22
|
|
13
23
|
_print = partial(print, flush=True)
|
@@ -21,6 +31,17 @@ HPRINT_DEBUG = os.getenv("HPRINT_DEBUG")
|
|
21
31
|
__all__ = ['pretty_print', 'hprint']
|
22
32
|
|
23
33
|
|
34
|
+
def _format_numeric(value: Union[int, float], tabulate_kwargs: dict) -> Any:
|
35
|
+
"""
|
36
|
+
Format numeric values according to the provided tabulate kwargs.
|
37
|
+
"""
|
38
|
+
if 'floatfmt' in tabulate_kwargs and isinstance(value, float):
|
39
|
+
return f"{value:{tabulate_kwargs['floatfmt']}}"
|
40
|
+
elif 'intfmt' in tabulate_kwargs and isinstance(value, int):
|
41
|
+
return f"{value:{tabulate_kwargs['intfmt']}}"
|
42
|
+
return value
|
43
|
+
|
44
|
+
|
24
45
|
def _if_null(x, default):
|
25
46
|
return default if x is None else x
|
26
47
|
|
@@ -54,7 +75,7 @@ def _get(obj, key):
|
|
54
75
|
return chain_get(obj, key)
|
55
76
|
|
56
77
|
|
57
|
-
def tabulate_numbered_print(data, mappings, offset=0, convert=True, missing_value=_MISSING_VALUE):
|
78
|
+
def tabulate_numbered_print(data, mappings, offset=0, convert=True, missing_value=_MISSING_VALUE, raw=False, **tabulate_kwargs):
|
58
79
|
if not data:
|
59
80
|
return
|
60
81
|
if not mappings:
|
@@ -69,7 +90,7 @@ def tabulate_numbered_print(data, mappings, offset=0, convert=True, missing_valu
|
|
69
90
|
k = mappings[h]
|
70
91
|
if isinstance(k, (tuple, list)):
|
71
92
|
if len(k) == 2:
|
72
|
-
default =
|
93
|
+
default = missing_value
|
73
94
|
(k0, func) = k
|
74
95
|
elif len(k) == 3:
|
75
96
|
(k0, default, func) = k
|
@@ -77,11 +98,18 @@ def tabulate_numbered_print(data, mappings, offset=0, convert=True, missing_valu
|
|
77
98
|
raise ValueError(f"Invalid mapping {k}")
|
78
99
|
if not convert:
|
79
100
|
func = _no_convertion_func
|
80
|
-
attrs.append(func(_if_null(_get(item, k0), default)))
|
101
|
+
attrs.append(func(_if_null(_format_numeric(_get(item, k0), tabulate_kwargs), default)))
|
81
102
|
else:
|
82
|
-
attrs.append(_if_null(_get(item, k), missing_value))
|
103
|
+
attrs.append(_if_null(_format_numeric(_get(item, k), tabulate_kwargs), missing_value))
|
83
104
|
tabdata.append(attrs)
|
84
|
-
|
105
|
+
if 'floatfmt' in tabulate_kwargs:
|
106
|
+
del tabulate_kwargs['floatfmt']
|
107
|
+
if 'intfmt' in tabulate_kwargs:
|
108
|
+
del tabulate_kwargs['intfmt']
|
109
|
+
output = tabulate(tabdata, headers=headers, **tabulate_kwargs)
|
110
|
+
if raw:
|
111
|
+
return output
|
112
|
+
_print(output)
|
85
113
|
|
86
114
|
|
87
115
|
def _len(x):
|
@@ -113,7 +141,11 @@ def x_print(records, headers, offset=0, header=True):
|
|
113
141
|
return os.linesep.join(output)
|
114
142
|
|
115
143
|
|
116
|
-
def tabulate_print(
|
144
|
+
def tabulate_print(
|
145
|
+
data, mappings, x=False, offset=0, header=True, raw=False,
|
146
|
+
convert=True, missing_value=_MISSING_VALUE,
|
147
|
+
**tabulate_kwargs,
|
148
|
+
):
|
117
149
|
if not data:
|
118
150
|
return
|
119
151
|
if not mappings:
|
@@ -129,7 +161,7 @@ def tabulate_print(data, mappings, x=False, offset=0, header=True, raw=False, tf
|
|
129
161
|
k = mappings[h]
|
130
162
|
if isinstance(k, (tuple, list)):
|
131
163
|
if len(k) == 2:
|
132
|
-
default =
|
164
|
+
default = missing_value
|
133
165
|
(k0, func) = k
|
134
166
|
elif len(k) == 3:
|
135
167
|
(k0, default, func) = k
|
@@ -137,20 +169,45 @@ def tabulate_print(data, mappings, x=False, offset=0, header=True, raw=False, tf
|
|
137
169
|
raise ValueError(f"Invalid mapping {k}")
|
138
170
|
if not convert:
|
139
171
|
func = _no_convertion_func
|
140
|
-
attrs.append(func(_if_null(_get(item, k0), default)))
|
172
|
+
attrs.append(func(_if_null(_format_numeric(_get(item, k0), tabulate_kwargs), default)))
|
141
173
|
else:
|
142
|
-
attrs.append(_if_null(_get(item, k), missing_value))
|
174
|
+
attrs.append(_if_null(_format_numeric(_get(item, k), tabulate_kwargs), missing_value))
|
143
175
|
tabdata.append(attrs)
|
144
176
|
if x:
|
145
177
|
output = x_print(tabdata, headers, offset=offset, header=header)
|
146
178
|
else:
|
147
|
-
|
179
|
+
if 'floatfmt' in tabulate_kwargs:
|
180
|
+
del tabulate_kwargs['floatfmt']
|
181
|
+
if 'intfmt' in tabulate_kwargs:
|
182
|
+
del tabulate_kwargs['intfmt']
|
183
|
+
output = tabulate(tabdata, headers=headers if header else (), **tabulate_kwargs)
|
148
184
|
if raw:
|
149
185
|
return output
|
150
186
|
_print(output)
|
151
187
|
|
152
188
|
|
153
|
-
def
|
189
|
+
def _set_defaults(tabulate_kwargs: dict[str, Any]) -> None:
|
190
|
+
for k, d in DEFAULTS.items():
|
191
|
+
if k not in tabulate_kwargs:
|
192
|
+
tabulate_kwargs[k] = d
|
193
|
+
|
194
|
+
|
195
|
+
def hprint(
|
196
|
+
data,
|
197
|
+
*,
|
198
|
+
mappings=None,
|
199
|
+
json_format=False,
|
200
|
+
as_json=False,
|
201
|
+
x=False,
|
202
|
+
offset=0,
|
203
|
+
numbered=False,
|
204
|
+
missing_value=_MISSING_VALUE,
|
205
|
+
header=True,
|
206
|
+
raw=False,
|
207
|
+
convert=True,
|
208
|
+
**tabulate_kwargs
|
209
|
+
):
|
210
|
+
_set_defaults(tabulate_kwargs)
|
154
211
|
as_json = as_json or json_format
|
155
212
|
if not data:
|
156
213
|
return
|
@@ -160,9 +217,16 @@ def hprint(data, *, mappings=None, json_format=False, as_json=False, x=False, of
|
|
160
217
|
return data
|
161
218
|
json_print(data)
|
162
219
|
elif not x and numbered:
|
163
|
-
tabulate_numbered_print(
|
220
|
+
return tabulate_numbered_print(
|
221
|
+
copy.deepcopy(data), mappings, offset=offset, convert=convert, missing_value=missing_value, raw=raw,
|
222
|
+
**tabulate_kwargs
|
223
|
+
)
|
164
224
|
else:
|
165
|
-
return tabulate_print(
|
225
|
+
return tabulate_print(
|
226
|
+
data, mappings=mappings, x=x, offset=offset, header=header, raw=raw,
|
227
|
+
convert=convert, missing_value=missing_value,
|
228
|
+
**tabulate_kwargs
|
229
|
+
)
|
166
230
|
except Exception:
|
167
231
|
json_print(data)
|
168
232
|
if HPRINT_DEBUG or logger.isEnabledFor(logging.DEBUG):
|
@@ -1,12 +1,12 @@
|
|
1
1
|
Metadata-Version: 2.4
|
2
2
|
Name: hprint
|
3
|
-
Version: 2.1.
|
4
|
-
Summary: Print python object in table
|
3
|
+
Version: 2.1.2
|
4
|
+
Summary: Print python object list in table format
|
5
5
|
Home-page: https://github.com/ruanhao/hprint
|
6
6
|
Author: Hao Ruan
|
7
7
|
Author-email: ruanhao1116@gmail.com
|
8
8
|
License: MIT
|
9
|
-
Keywords:
|
9
|
+
Keywords: table,print,json,utils,format
|
10
10
|
Classifier: Development Status :: 5 - Production/Stable
|
11
11
|
Classifier: Environment :: Web Environment
|
12
12
|
Classifier: Intended Audience :: Developers
|
@@ -20,6 +20,8 @@ Classifier: Programming Language :: Python :: 3.8
|
|
20
20
|
Classifier: Programming Language :: Python :: 3.9
|
21
21
|
Classifier: Programming Language :: Python :: 3.10
|
22
22
|
Classifier: Programming Language :: Python :: 3.11
|
23
|
+
Classifier: Programming Language :: Python :: 3.12
|
24
|
+
Classifier: Programming Language :: Python :: 3.13
|
23
25
|
Classifier: Programming Language :: Python :: 3 :: Only
|
24
26
|
Classifier: Topic :: Software Development :: Libraries
|
25
27
|
Requires-Python: >=3.7, <4
|
@@ -0,0 +1,8 @@
|
|
1
|
+
hprint/__init__.py,sha256=A9W4vvGdhxrkIBTCy7_PsLHx3vAwJWJ-ft1Op_xFAcI,7295
|
2
|
+
hprint/dsutils.py,sha256=UD1L8-23_0320w7ESc_Vwc148w_CLSyJ6t7T5t3yu9M,786
|
3
|
+
hprint/utils.py,sha256=ziZQyo-c3QhACQCnFaRjoKZTT9EhjMLu2Qf53jimhHo,603
|
4
|
+
hprint-2.1.2.dist-info/licenses/LICENSE,sha256=5IASoCg1AtQddnqyVxltUyoadbZzUpRa_0QvUB-Hwlg,1065
|
5
|
+
hprint-2.1.2.dist-info/METADATA,sha256=rHnJfX7N-lXcsg6Pq2XNGPeYTjdlhOyQaKdYqCFMv2c,5667
|
6
|
+
hprint-2.1.2.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
7
|
+
hprint-2.1.2.dist-info/top_level.txt,sha256=yHUSDt5IDs8J8taLMOu-SaOZK24DyzM0TidniiEJ-Ng,7
|
8
|
+
hprint-2.1.2.dist-info/RECORD,,
|
hprint-2.1.1.dist-info/RECORD
DELETED
@@ -1,8 +0,0 @@
|
|
1
|
-
hprint/__init__.py,sha256=zJYTFYq88Qgppddo2TIdm_5ocaQ6AdqAxyi9bcWhJpU,5663
|
2
|
-
hprint/dsutils.py,sha256=UD1L8-23_0320w7ESc_Vwc148w_CLSyJ6t7T5t3yu9M,786
|
3
|
-
hprint/utils.py,sha256=ziZQyo-c3QhACQCnFaRjoKZTT9EhjMLu2Qf53jimhHo,603
|
4
|
-
hprint-2.1.1.dist-info/licenses/LICENSE,sha256=5IASoCg1AtQddnqyVxltUyoadbZzUpRa_0QvUB-Hwlg,1065
|
5
|
-
hprint-2.1.1.dist-info/METADATA,sha256=bQWzNdxemNJ4uEizWAg41sHHAW92K4B2qSoD3uYbXTw,5552
|
6
|
-
hprint-2.1.1.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
7
|
-
hprint-2.1.1.dist-info/top_level.txt,sha256=yHUSDt5IDs8J8taLMOu-SaOZK24DyzM0TidniiEJ-Ng,7
|
8
|
-
hprint-2.1.1.dist-info/RECORD,,
|
File without changes
|
File without changes
|
File without changes
|