1212 ~ExcelDatabaseFileBase
1313 ~ExcelDatabaseFileGeneric
1414 ~ExcelReadError
15+ ~ipython_profile_name
1516 ~itemizer
1617 ~json_export
1718 ~json_import
18- ~ipython_profile_name
19+ ~list_recent_scans
1920 ~pairwise
2021 ~print_snapshot_list
2122 ~print_RE_md
@@ -140,7 +141,7 @@ def dictionary_table(dictionary, fmt="simple"):
140141
141142 default: ``simple``
142143
143- .. [#] *pyRestTable* : https://pyresttable.readthedocs.io/en/latest/examples/index.html#examples
144+ .. [#] *pyRestTable* : https://pyresttable.readthedocs.io/en/latest/examples/index.html#examples
144145
145146 RETURNS
146147
@@ -181,6 +182,65 @@ def itemizer(fmt, items):
181182 return [fmt % k for k in items ]
182183
183184
185+ def list_recent_scans (num = 20 , keys = [], printing = True , db = None ):
186+ """
187+ make a table of the most recent scans
188+
189+ PARAMETERS
190+
191+ num : int (default: ``20``)
192+ Make the table include the ``num`` most recent scans.
193+ keys : [str] (default: ``[]``)
194+ Include these additional keys from the start document.
195+ printing : bool (default: ``True``)
196+ If True, print the table to stdout
197+ db : object (default: ``db`` from the IPython shell)
198+ Instance of ``databroker.Broker()``
199+
200+ RETURNS
201+
202+ object:
203+ Instance of `pyRestTable.Table()``
204+
205+ EXAMPLE::
206+
207+ In [7]: APS_utils.list_recent_scans(num=5, keys=["proposal_id","pid"])
208+ ========= ========================== ======= ========= =========== =====
209+ short_uid date/time scan_id plan_name proposal_id pid
210+ ========= ========================== ======= ========= =========== =====
211+ 235cc8e 2019-07-26 19:59:57.377210 156 scan testing 31185
212+ 82406dd 2019-07-26 19:57:30.607125 155 scan testing 31185
213+ f6249d8 2019-07-25 16:45:36.114533 151 count testing 15321
214+ 9457fa4 2019-07-25 16:19:07.410803 150 count testing 4845
215+ f17f026 2019-07-25 16:19:04.929030 149 count testing 4845
216+ ========= ========================== ======= ========= =========== =====
217+
218+ """
219+ try :
220+ from IPython import get_ipython
221+ global_db = get_ipython ().user_ns ["db" ]
222+ except AttributeError as _exc :
223+ global_db = None
224+ db = db or global_db
225+
226+ keys .insert (0 , "plan_name" )
227+ keys .insert (0 , "scan_id" )
228+
229+ table = pyRestTable .Table ()
230+ table .labels = "short_uid date/time" .split () + keys
231+
232+ for h in db [- abs (num ):]:
233+ row = [
234+ h .start ["uid" ][:7 ],
235+ datetime .datetime .fromtimestamp (h .start ['time' ]),
236+ ] + [h .start .get (k , "" ) for k in keys ]
237+ table .addRow (row )
238+
239+ if printing :
240+ print (table )
241+ return table
242+
243+
184244def print_RE_md (dictionary = None , fmt = "simple" , printing = True ):
185245 """
186246 custom print the RunEngine metadata in a table
@@ -210,7 +270,11 @@ def print_RE_md(dictionary=None, fmt="simple", printing=True):
210270 ======================== ===================================
211271
212272 """
213- global RE
273+ try :
274+ from IPython import get_ipython
275+ RE = get_ipython ().user_ns ["RE" ]
276+ except AttributeError as _exc :
277+ RE = None
214278 dictionary = dictionary or RE .md
215279 md = dict (dictionary ) # copy of input for editing
216280 v = dictionary_table (md ["versions" ], fmt = fmt ) # sub-table
@@ -279,30 +343,13 @@ def show_ophyd_symbols(show_pv=True, printing=True, verbose=False, symbols=None)
279343 If True, also show ``str(obj``.
280344 symbols: dict (default: `globals()`)
281345 If None, use global symbol table.
282- If not None, use provided dictionary.
283-
284- **TIP** ``globals()`` only gets the module's globals
285-
286- To get ``globals()`` from the global namespace, need to
287- pass that from the global namespace into this function.
288- Define this function *in* the global namespace::
289-
290- from apstools import utils as APS_utils
291-
292- def show_ophyd_symbols(
293- show_pv=True,
294- printing=True,
295- verbose=False,
296- symbols=None
297- ):
298- symbols = symbols or globals()
299- return APS_utils.show_ophyd_symbols(
300- show_pv=show_pv,
301- printing=printing,
302- verbose=verbose,
303- symbols=symbols
304- )
346+ If not None, use provided dictionary.
305347
348+ RETURNS
349+
350+ object:
351+ Instance of `pyRestTable.Table()``
352+
306353 EXAMPLE::
307354
308355 In [1]: show_ophyd_symbols()
@@ -333,7 +380,13 @@ def show_ophyd_symbols(
333380 table .addLabel ("EPICS PV" )
334381 if verbose :
335382 table .addLabel ("object representation" )
336- g = symbols or globals ()
383+ table .addLabel ("label(s)" )
384+ try :
385+ from IPython import get_ipython
386+ g = get_ipython ().user_ns
387+ except AttributeError as _exc :
388+ g = globals ()
389+ g = symbols or g
337390 for k , v in sorted (g .items ()):
338391 if isinstance (v , (ophyd .Signal , ophyd .Device )):
339392 row = [k , v .__class__ .__name__ ]
@@ -346,6 +399,7 @@ def show_ophyd_symbols(
346399 row .append ("" )
347400 if verbose :
348401 row .append (str (v ))
402+ row .append (' ' .join (v ._ophyd_labels_ ))
349403 table .addRow (row )
350404 if printing :
351405 print (table )
0 commit comments