Skip to content

Commit 31e92fb

Browse files
authored
Merge pull request #154 from BCDA-APS/dict-table_153
fixes #153 adds: * `apstools.utils.dictionary_table()` * `apstools.utils.print_RE_md()`
2 parents 9029084 + 25f9915 commit 31e92fb

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed

apstools/utils.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
66
~cleanupText
77
~connect_pvlist
8+
~dictionary_table
89
~EmailNotifications
910
~ExcelDatabaseFileBase
1011
~ExcelDatabaseFileGeneric
@@ -13,6 +14,7 @@
1314
~ipython_profile_name
1415
~pairwise
1516
~print_snapshot_list
17+
~print_RE_md
1618
~text_encode
1719
~to_unicode_or_bust
1820
~trim_string_for_EPICS
@@ -71,6 +73,91 @@ def mapper(c):
7173
return "".join([mapper(c) for c in text])
7274

7375

76+
def dictionary_table(dictionary, fmt="simple"):
77+
"""
78+
return a text table from ``dictionary``
79+
80+
PARAMETERS
81+
82+
dictionary : dict
83+
Python dictionary
84+
fmt : str
85+
Any of the format names provided by
86+
`spec2nexus <https://pyresttable.readthedocs.io/en/latest/examples/index.html#examples>`_
87+
One of these: ``simple | plain | grid | complex | markdown | list-table | html``
88+
89+
default: ``simple``
90+
91+
RETURNS
92+
93+
table : str or `None`
94+
multiline text table with dictionary contents in chosen format
95+
or ``None`` if dictionary has no contents
96+
97+
EXAMPLE::
98+
99+
In [8]: RE.md
100+
Out[8]: {'login_id': 'jemian:wow.aps.anl.gov', 'beamline_id': 'developer', 'proposal_id': None, 'pid': 19072, 'scan_id': 10, 'version': {'bluesky': '1.5.2', 'ophyd': '1.3.3', 'apstools': '1.1.5', 'epics': '3.3.3'}}
101+
102+
In [9]: print(dictionary_table(RE.md))
103+
=========== =============================================================================
104+
key value
105+
=========== =============================================================================
106+
beamline_id developer
107+
login_id jemian:wow.aps.anl.gov
108+
pid 19072
109+
proposal_id None
110+
scan_id 10
111+
version {'bluesky': '1.5.2', 'ophyd': '1.3.3', 'apstools': '1.1.5', 'epics': '3.3.3'}
112+
=========== =============================================================================
113+
114+
"""
115+
if len(dictionary) == 0:
116+
return
117+
_t = pyRestTable.Table()
118+
_t.addLabel("key")
119+
_t.addLabel("value")
120+
for k, v in sorted(dictionary.items()):
121+
_t.addRow((k, str(v)))
122+
return _t.reST(fmt=fmt)
123+
124+
125+
def print_RE_md(dictionary=RE.md, fmt="simple"):
126+
"""
127+
custom print the RunEngine metadata in a table
128+
129+
EXAMPLE::
130+
131+
In [4]: print_RE_md()
132+
RunEngine metadata dictionary:
133+
======================== ===================================
134+
key value
135+
======================== ===================================
136+
EPICS_CA_MAX_ARRAY_BYTES 1280000
137+
EPICS_HOST_ARCH linux-x86_64
138+
beamline_id APS USAXS 9-ID-C
139+
login_id usaxs:usaxscontrol.xray.aps.anl.gov
140+
pid 67933
141+
proposal_id testing Bluesky installation
142+
scan_id 0
143+
versions ======== =====
144+
key value
145+
======== =====
146+
apstools 1.1.3
147+
bluesky 1.5.2
148+
epics 3.3.1
149+
ophyd 1.3.3
150+
======== =====
151+
======================== ===================================
152+
153+
"""
154+
md = dict(dictionary) # copy of input for editing
155+
v = dictionary_table(md["versions"], fmt=fmt) # sub-table
156+
md["versions"] = str(v).rstrip()
157+
print("RunEngine metadata dictionary:")
158+
print(dictionary_table(md, fmt=fmt))
159+
160+
74161
def pairwise(iterable):
75162
"""
76163
break a list (or other iterable) into pairs

0 commit comments

Comments
 (0)