hprint 2.0.4__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 CHANGED
@@ -5,10 +5,11 @@ from pprint import pformat
5
5
  import os
6
6
  import traceback
7
7
  import logging
8
+ import textwrap
8
9
 
9
10
  _print = partial(print, flush=True)
10
11
 
11
- HPRINT_WRAP = os.getenv("HPRINT_WRAP", 50)
12
+ HPRINT_WRAP = int(os.getenv("HPRINT_WRAP", 50))
12
13
 
13
14
  logger = logging.getLogger(__name__)
14
15
 
@@ -28,7 +29,10 @@ def json_print(data):
28
29
  try:
29
30
  _print(json.dumps([dict(d) for d in data], indent=4, sort_keys=True))
30
31
  except Exception:
31
- _pprint([dict(d) for d in data])
32
+ try:
33
+ _pprint([dict(d) for d in data])
34
+ except Exception:
35
+ _pprint(data)
32
36
  else:
33
37
  _pprint(data)
34
38
 
@@ -43,10 +47,14 @@ def _chain_get(data, chain, default=None):
43
47
  return result.get(attrs[-1], default)
44
48
 
45
49
 
46
- def _get(obj, key, default='n/a'):
50
+ def _get(obj, key, default='[none]'):
47
51
  if not key:
48
52
  return obj
49
- 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
50
58
 
51
59
 
52
60
  def tabulate_numbered_print(data, mappings, offset=0):
@@ -78,7 +86,9 @@ def _len(x):
78
86
  def _indent(s: str, indent_cols, max_cols):
79
87
  lines = s.splitlines()
80
88
  if len(lines) <= 1:
81
- return s.ljust(max_cols)
89
+ lines = textwrap.wrap(s, max_cols)
90
+ if len(lines) <= 1:
91
+ return s.ljust(max_cols)
82
92
  return lines[0] + '\n' + '\n'.join([(' ' * indent_cols + "| " + line) for line in lines[1:]])
83
93
 
84
94
 
@@ -86,22 +96,29 @@ def x_print(records, headers, offset=0, header=True):
86
96
  headers = list(headers)
87
97
  left_max_len = max(len(max(headers, key=len)), len(f"-[ RECORD {len(records)} ]-")) + 1
88
98
  right_max_len = max(_len(max(record, key=_len)) for record in records) + 1
99
+ output = []
89
100
  for i, record in enumerate(records, 1 + offset):
90
101
  if header:
91
- _print(f'-[ RECORD {i} ]'.ljust(left_max_len, '-') + '+' + '-' * right_max_len)
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)
92
104
  for j, v in enumerate(record):
93
105
  # _print(f'{headers[j]}'.ljust(left_max_len) + '| ' + str(v).ljust(right_max_len))
94
- _print(f'{headers[j]}'.ljust(left_max_len) + '| ' + _indent(str(v), left_max_len, 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)
95
109
 
96
110
 
97
111
  def tabulate_print(data, mappings, x=False, offset=0, header=True, raw=False, tf='simple'):
98
112
  if not data:
99
113
  return
100
114
  if not mappings:
101
- keys = set()
115
+ mappings = {}
116
+ # keys = set()
102
117
  for entry in data:
103
- keys = keys.union(entry.keys())
104
- mappings = {k: k for k in keys}
118
+ # keys = keys.union(entry.keys())
119
+ for k in entry.keys():
120
+ mappings[k] = k
121
+ # mappings = {k: k for k in keys}
105
122
  # entry_with_most_keys = max(data, key=len)
106
123
  # mappings = {k: k for k in entry_with_most_keys.keys()}
107
124
  headers = mappings.keys()
@@ -117,15 +134,15 @@ def tabulate_print(data, mappings, x=False, offset=0, header=True, raw=False, tf
117
134
  attrs.append(_get(item, k))
118
135
  tabdata.append(attrs)
119
136
  if x:
120
- x_print(tabdata, headers, offset=offset, header=header)
137
+ output = x_print(tabdata, headers, offset=offset, header=header)
121
138
  else:
122
139
  output = tabulate(tabdata, headers=headers if header else (), tablefmt=tf)
123
- if raw:
124
- return output
125
- _print(output)
140
+ if raw:
141
+ return output
142
+ _print(output)
126
143
 
127
144
 
128
- def hprint(data, *, mappings=None, json_format=False, as_json=False, x=False, offset=0, numbered=False, missing_value='n/a', tf='simple', header=True, raw=False):
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):
129
146
  as_json = as_json or json_format
130
147
  if not data:
131
148
  return
@@ -134,6 +151,8 @@ def hprint(data, *, mappings=None, json_format=False, as_json=False, x=False, of
134
151
  _get = partial(_get, default=missing_value)
135
152
  try:
136
153
  if as_json:
154
+ if raw:
155
+ return data
137
156
  json_print(data)
138
157
  elif not x and numbered:
139
158
  tabulate_numbered_print(data, mappings, offset=offset)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: hprint
3
- Version: 2.0.4
3
+ Version: 2.0.5
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,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,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: bdist_wheel (0.38.4)
2
+ Generator: bdist_wheel (0.40.0)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5
 
@@ -1,6 +0,0 @@
1
- hprint/__init__.py,sha256=8_nnG9LHFAuDa-yOt7GyOYe2su8kUBwV8AWS3WjkgWE,4501
2
- hprint-2.0.4.dist-info/LICENSE,sha256=5IASoCg1AtQddnqyVxltUyoadbZzUpRa_0QvUB-Hwlg,1065
3
- hprint-2.0.4.dist-info/METADATA,sha256=ajJVE7HDHSpGCwnsnmSfDDXmoOP1oiwETNMwWVekbPw,5298
4
- hprint-2.0.4.dist-info/WHEEL,sha256=2wepM1nk4DS4eFpYrW1TTqPcoGNfHhhO_i5m4cOimbo,92
5
- hprint-2.0.4.dist-info/top_level.txt,sha256=yHUSDt5IDs8J8taLMOu-SaOZK24DyzM0TidniiEJ-Ng,7
6
- hprint-2.0.4.dist-info/RECORD,,