hprint 2.1.0__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 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
 
@@ -35,10 +56,10 @@ def _pprint(obj):
35
56
 
36
57
  def json_print(data):
37
58
  if isinstance(data, dict):
38
- _print(json.dumps(data, indent=4, sort_keys=True, default=str))
59
+ _print(json.dumps(data, indent=4, sort_keys=True, default=str, ensure_ascii=False))
39
60
  elif isinstance(data, list):
40
61
  try:
41
- _print(json.dumps([dict(d) for d in data], indent=4, sort_keys=True, default=str))
62
+ _print(json.dumps([dict(d) for d in data], indent=4, sort_keys=True, default=str, ensure_ascii=False))
42
63
  except Exception:
43
64
  try:
44
65
  _pprint([dict(d) for d in data])
@@ -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 = None
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
- _print(tabulate(tabdata, headers=headers, floatfmt='', intfmt=''))
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(data, mappings, x=False, offset=0, header=True, raw=False, tf='simple', convert=True, missing_value=_MISSING_VALUE):
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 = None
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
- output = tabulate(tabdata, headers=headers if header else (), tablefmt=tf, floatfmt='', intfmt='')
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 hprint(data, *, mappings=None, json_format=False, as_json=False, x=False, offset=0, numbered=False, missing_value=_MISSING_VALUE, tf='simple', header=True, raw=False, convert=True):
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(data, mappings, offset=offset, convert=convert, missing_value=missing_value)
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(data, mappings=mappings, x=x, offset=offset, header=header, raw=raw, tf=tf, convert=convert, missing_value=missing_value)
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
- Metadata-Version: 2.1
1
+ Metadata-Version: 2.4
2
2
  Name: hprint
3
- Version: 2.1.0
4
- Summary: Print python object in table/json format
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: utils,print,json
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,12 +20,26 @@ 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
26
28
  Description-Content-Type: text/markdown
27
29
  License-File: LICENSE
28
30
  Requires-Dist: tabulate
31
+ Dynamic: author
32
+ Dynamic: author-email
33
+ Dynamic: classifier
34
+ Dynamic: description
35
+ Dynamic: description-content-type
36
+ Dynamic: home-page
37
+ Dynamic: keywords
38
+ Dynamic: license
39
+ Dynamic: license-file
40
+ Dynamic: requires-dist
41
+ Dynamic: requires-python
42
+ Dynamic: summary
29
43
 
30
44
  hprint
31
45
  ======
@@ -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,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: bdist_wheel (0.42.0)
2
+ Generator: setuptools (80.9.0)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5
 
@@ -1,8 +0,0 @@
1
- hprint/__init__.py,sha256=AqKaCS5sv6NuDFbJYf5TDYKEhZGOQclGnHopD2q-GTM,5623
2
- hprint/dsutils.py,sha256=UD1L8-23_0320w7ESc_Vwc148w_CLSyJ6t7T5t3yu9M,786
3
- hprint/utils.py,sha256=ziZQyo-c3QhACQCnFaRjoKZTT9EhjMLu2Qf53jimhHo,603
4
- hprint-2.1.0.dist-info/LICENSE,sha256=5IASoCg1AtQddnqyVxltUyoadbZzUpRa_0QvUB-Hwlg,1065
5
- hprint-2.1.0.dist-info/METADATA,sha256=LU7M7DAwV3OfDsgxyaW4_Q8pcu8jsRQjalUnVuxHdaw,5298
6
- hprint-2.1.0.dist-info/WHEEL,sha256=oiQVh_5PnQM0E3gPdiz09WCNmwiHDMaGer_elqB3coM,92
7
- hprint-2.1.0.dist-info/top_level.txt,sha256=yHUSDt5IDs8J8taLMOu-SaOZK24DyzM0TidniiEJ-Ng,7
8
- hprint-2.1.0.dist-info/RECORD,,