-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathinfo.py
65 lines (54 loc) · 2.06 KB
/
info.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#######################################################################
# Copyright (c) 2019-present, Blosc Development Team <[email protected]>
# All rights reserved.
#
# This source code is licensed under a BSD-style license (found in the
# LICENSE file in the root directory of this source tree)
#######################################################################
import io
import pprint
from textwrap import TextWrapper
def info_text_report_(items: list) -> str:
with io.StringIO() as buf:
print(items, file=buf)
return buf.getvalue()
def info_text_report(items: list) -> str:
keys = [k for k, v in items]
max_key_len = max(len(k) for k in keys)
report = ""
for k, v in items:
if isinstance(v, dict):
# rich way, this is disabled because it doesn't work well in the notebooks
# with io.StringIO() as buf:
# v_sorted = {k: val for k, val in sorted(v.items())}
# rich.print(v_sorted, file=buf)
# str_v = buf.getvalue()[:-1] # remove the trailing \n
# text = k.ljust(max_key_len) + " : " + str_v
# pprint way
text = k.ljust(max_key_len) + " : " + pprint.pformat(v)
else:
wrapper = TextWrapper(
width=96,
initial_indent=k.ljust(max_key_len) + " : ",
subsequent_indent=" " * max_key_len + " : ",
)
text = wrapper.fill(str(v))
report += text + "\n"
return report
def info_html_report(items: list) -> str:
report = '<table class="NDArray-info">'
report += "<tbody>"
for k, v in items:
report += f'<tr><th style="text-align: left">{k}</th><td style="text-align: left">{v}</td></tr>'
report += "</tbody>"
report += "</table>"
return report
class InfoReporter:
def __init__(self, obj):
self.obj = obj
def __repr__(self):
items = self.obj.info_items
return info_text_report(items)
def _repr_html_(self):
items = self.obj.info_items
return info_html_report(items)