hprint 2.0.7__py3-none-any.whl → 2.0.9__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
@@ -6,6 +6,7 @@ import os
6
6
  import traceback
7
7
  import logging
8
8
  import textwrap
9
+ from .utils import chain_get
9
10
 
10
11
  _print = partial(print, flush=True)
11
12
 
@@ -18,6 +19,10 @@ HPRINT_DEBUG = os.getenv("HPRINT_DEBUG")
18
19
  __all__ = ['pretty_print', 'hprint']
19
20
 
20
21
 
22
+ def _no_convertion_func(x):
23
+ return x
24
+
25
+
21
26
  def _pprint(obj):
22
27
  _print(pformat(obj, indent=4))
23
28
 
@@ -27,7 +32,7 @@ def json_print(data):
27
32
  _print(json.dumps(data, indent=4, sort_keys=True, default=str))
28
33
  elif isinstance(data, list):
29
34
  try:
30
- _print(json.dumps([dict(d) for d in data], indent=4, sort_keys=True), default=str)
35
+ _print(json.dumps([dict(d) for d in data], indent=4, sort_keys=True, default=str))
31
36
  except Exception:
32
37
  try:
33
38
  _pprint([dict(d) for d in data])
@@ -37,27 +42,16 @@ def json_print(data):
37
42
  _pprint(data)
38
43
 
39
44
 
40
- def _chain_get(data, chain, default=None):
41
- attrs = chain.split('.')
42
- if len(attrs) == 1:
43
- return data.get(attrs[0], default)
44
- result = data
45
- for attr in attrs[:-1]:
46
- result = result.get(attr, {})
47
- return result.get(attrs[-1], default)
48
-
49
-
50
45
  def _get(obj, key, default='[none]'):
51
46
  if not key:
52
47
  return obj
53
- # return _chain_get(obj, key, default)
54
- result = _chain_get(obj, key, default)
48
+ result = chain_get(obj, key, default)
55
49
  if result is None:
56
50
  return default
57
51
  return result
58
52
 
59
53
 
60
- def tabulate_numbered_print(data, mappings, offset=0):
54
+ def tabulate_numbered_print(data, mappings, offset=0, convert=True):
61
55
  if not data:
62
56
  return
63
57
  if not mappings:
@@ -72,6 +66,8 @@ def tabulate_numbered_print(data, mappings, offset=0):
72
66
  k = mappings[h]
73
67
  if isinstance(k, tuple):
74
68
  (k0, func) = k
69
+ if not convert:
70
+ func = _no_convertion_func
75
71
  attrs.append(func(_get(item, k0)))
76
72
  else:
77
73
  attrs.append(_get(item, k))
@@ -108,7 +104,7 @@ def x_print(records, headers, offset=0, header=True):
108
104
  return os.linesep.join(output)
109
105
 
110
106
 
111
- def tabulate_print(data, mappings, x=False, offset=0, header=True, raw=False, tf='simple'):
107
+ def tabulate_print(data, mappings, x=False, offset=0, header=True, raw=False, tf='simple', convert=True):
112
108
  if not data:
113
109
  return
114
110
  if not mappings:
@@ -129,6 +125,8 @@ def tabulate_print(data, mappings, x=False, offset=0, header=True, raw=False, tf
129
125
  k = mappings[h]
130
126
  if isinstance(k, tuple):
131
127
  (k0, func) = k
128
+ if not convert:
129
+ func = _no_convertion_func
132
130
  attrs.append(func(_get(item, k0)))
133
131
  else:
134
132
  attrs.append(_get(item, k))
@@ -142,7 +140,7 @@ def tabulate_print(data, mappings, x=False, offset=0, header=True, raw=False, tf
142
140
  _print(output)
143
141
 
144
142
 
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):
143
+ 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, convert=True):
146
144
  as_json = as_json or json_format
147
145
  if not data:
148
146
  return
@@ -155,9 +153,9 @@ def hprint(data, *, mappings=None, json_format=False, as_json=False, x=False, of
155
153
  return data
156
154
  json_print(data)
157
155
  elif not x and numbered:
158
- tabulate_numbered_print(data, mappings, offset=offset)
156
+ tabulate_numbered_print(data, mappings, offset=offset, convert=convert)
159
157
  else:
160
- return tabulate_print(data, mappings=mappings, x=x, offset=offset, header=header, raw=raw, tf=tf)
158
+ return tabulate_print(data, mappings=mappings, x=x, offset=offset, header=header, raw=raw, tf=tf, convert=convert)
161
159
  except Exception:
162
160
  json_print(data)
163
161
  if HPRINT_DEBUG or logger.isEnabledFor(logging.DEBUG):
hprint/utils.py CHANGED
@@ -9,3 +9,13 @@ def install_print_with_flush():
9
9
 
10
10
  setattr(builtins, 'print0', print)
11
11
  setattr(builtins, 'print', partial(print, flush=True))
12
+
13
+
14
+ def chain_get(data, chain, default=None):
15
+ attrs = chain.split('.')
16
+ if len(attrs) == 1:
17
+ return data.get(attrs[0], default)
18
+ result = data
19
+ for attr in attrs[:-1]:
20
+ result = result.get(attr, {})
21
+ return result.get(attrs[-1], default)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: hprint
3
- Version: 2.0.7
3
+ Version: 2.0.9
4
4
  Summary: Print python object in table/json format
5
5
  Home-page: https://github.com/ruanhao/hprint
6
6
  Author: Hao Ruan
@@ -0,0 +1,8 @@
1
+ hprint/__init__.py,sha256=xXW7vedi4pjyYZZQDafyY38u0E1_daTIiVw_-LFDM5I,5225
2
+ hprint/dsutils.py,sha256=UD1L8-23_0320w7ESc_Vwc148w_CLSyJ6t7T5t3yu9M,786
3
+ hprint/utils.py,sha256=xKqAi2Ks6zOoD4Jl_eioVcD1G33GNtFIOpmaeAYD0C4,549
4
+ hprint-2.0.9.dist-info/LICENSE,sha256=5IASoCg1AtQddnqyVxltUyoadbZzUpRa_0QvUB-Hwlg,1065
5
+ hprint-2.0.9.dist-info/METADATA,sha256=LYGNmKhzQ1eZ7FLghhIWw92cO5woYSthvBk1n9vSofY,5298
6
+ hprint-2.0.9.dist-info/WHEEL,sha256=oiQVh_5PnQM0E3gPdiz09WCNmwiHDMaGer_elqB3coM,92
7
+ hprint-2.0.9.dist-info/top_level.txt,sha256=yHUSDt5IDs8J8taLMOu-SaOZK24DyzM0TidniiEJ-Ng,7
8
+ hprint-2.0.9.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: bdist_wheel (0.41.1)
2
+ Generator: bdist_wheel (0.42.0)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5
 
@@ -1,8 +0,0 @@
1
- hprint/__init__.py,sha256=3_P_hQrHKSF-5f1Zj4aFDimk-jeQv3XgthRYB7dU8V0,5230
2
- hprint/dsutils.py,sha256=UD1L8-23_0320w7ESc_Vwc148w_CLSyJ6t7T5t3yu9M,786
3
- hprint/utils.py,sha256=9yZ6uiE7DQSnQLNnpBuu-SyXsaGVs3EWnBepUJhZHtw,283
4
- hprint-2.0.7.dist-info/LICENSE,sha256=5IASoCg1AtQddnqyVxltUyoadbZzUpRa_0QvUB-Hwlg,1065
5
- hprint-2.0.7.dist-info/METADATA,sha256=qh9uSz_fjdim_hE8zqET3mGWs_o7fPyNKvP1LeLQTTs,5298
6
- hprint-2.0.7.dist-info/WHEEL,sha256=5sUXSg9e4bi7lTLOHcm6QEYwO5TIF1TNbTSVFVjcJcc,92
7
- hprint-2.0.7.dist-info/top_level.txt,sha256=yHUSDt5IDs8J8taLMOu-SaOZK24DyzM0TidniiEJ-Ng,7
8
- hprint-2.0.7.dist-info/RECORD,,