From c67ced65e36fa38b4369275924ffb5e2df505cdd Mon Sep 17 00:00:00 2001 From: Adriaan Date: Mon, 20 Feb 2017 10:14:32 +0100 Subject: [PATCH] feature: Add print human readable snapshot to instrument * Added print human readable snapshot to base instrument class * Support change from units to unit * Updated the print instrument snapshot. - Added a docstring. - Made the formatter better * Implemented variable width, cropping long messages and left alignment * Updated docstring --- qcodes/instrument/base.py | 44 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/qcodes/instrument/base.py b/qcodes/instrument/base.py index d5acf90ab14..d2dc887423c 100644 --- a/qcodes/instrument/base.py +++ b/qcodes/instrument/base.py @@ -3,6 +3,7 @@ import time import warnings import weakref +import numpy as np from qcodes.utils.metadata import Metadatable from qcodes.utils.helpers import DelegateAttributes, strip_attrs, full_class @@ -434,7 +435,48 @@ def snapshot_base(self, update=False): snap[attr] = getattr(self, attr) return snap - # + def print_readable_snapshot(self, update=False, max_chars=80): + """ + Prints a readable version of the snapshot. + The readable snapshot includes the name, value and unit of each + parameter. + A convenience function to quickly get an overview of the status of an instrument. + + Args: + update (bool) : If True, update the state by querying the + instrument. If False, just use the latest values in memory. + This argument gets passed to the snapshot function. + max_chars (int) : the maximum number of characters per line. The + readable snapshot will be cropped if this value is exceeded. + Defaults to 80 to be consistent with default terminal width. + """ + floating_types = (float, np.integer, np.floating) + snapshot = self.snapshot(update=update) + + par_lengths = [len(p) for p in snapshot['parameters']] + + # Min of 50 is to prevent a super long parameter name to break this + # function + par_field_len = min(max(par_lengths)+1, 50) + + print(self.name + ':') + print('{0:<{1}}'.format('\tparameter ', par_field_len) + 'value') + print('-'*80) + for par in sorted(snapshot['parameters']): + msg = '{0:<{1}}:'.format(snapshot['parameters'][par]['name'], par_field_len) + val = snapshot['parameters'][par]['value'] + unit = snapshot['parameters'][par]['unit'] + if isinstance(val, floating_types): + msg += '\t{:.5g} '.format(val) + else: + msg += '\t{} '.format(val) + if unit is not '': # corresponds to no unit + msg += '({})'.format(unit) + # Truncate the message if it is longer than max length + if len(msg) > max_chars and not max_chars==-1: + msg = msg[0:max_chars-3] + '...' + print(msg) + # `write_raw` and `ask_raw` are the interface to hardware # # `write` and `ask` are standard wrappers to help with error reporting # #