mirror of https://github.com/m-labs/artiq.git
tools: refactor short_format with metadata
This commit is contained in:
parent
337273acb6
commit
bf38fc8b0f
|
@ -15,6 +15,7 @@ from sipyco import pyon
|
||||||
from artiq import __version__ as artiq_version
|
from artiq import __version__ as artiq_version
|
||||||
from artiq.appdirs import user_config_dir
|
from artiq.appdirs import user_config_dir
|
||||||
from artiq.language.environment import is_public_experiment
|
from artiq.language.environment import is_public_experiment
|
||||||
|
from artiq.language import units
|
||||||
|
|
||||||
|
|
||||||
__all__ = ["parse_arguments", "elide", "short_format", "file_import",
|
__all__ = ["parse_arguments", "elide", "short_format", "file_import",
|
||||||
|
@ -54,20 +55,40 @@ def elide(s, maxlen):
|
||||||
return s
|
return s
|
||||||
|
|
||||||
|
|
||||||
def short_format(v):
|
def short_format(v, metadata={}):
|
||||||
|
m = metadata
|
||||||
|
unit = m.get("unit", "")
|
||||||
|
default_scale = getattr(units, unit, 1)
|
||||||
|
scale = m.get("scale", default_scale)
|
||||||
|
precision = m.get("precision", None)
|
||||||
if v is None:
|
if v is None:
|
||||||
return "None"
|
return "None"
|
||||||
t = type(v)
|
t = type(v)
|
||||||
if np.issubdtype(t, np.number) or np.issubdtype(t, np.bool_):
|
if np.issubdtype(t, np.number):
|
||||||
return str(v)
|
v_t = np.divide(v, scale)
|
||||||
|
v_str = np.format_float_positional(v_t,
|
||||||
|
precision=precision,
|
||||||
|
unique=True)
|
||||||
|
v_str += " " + unit if unit else ""
|
||||||
|
return v_str
|
||||||
|
elif np.issubdtype(t, np.bool_):
|
||||||
|
return str(v)
|
||||||
elif np.issubdtype(t, np.unicode_):
|
elif np.issubdtype(t, np.unicode_):
|
||||||
return "\"" + elide(v, 50) + "\""
|
return "\"" + elide(v, 50) + "\""
|
||||||
else:
|
elif t is np.ndarray:
|
||||||
r = t.__name__
|
v_t = np.divide(v, scale)
|
||||||
if t is list or t is dict or t is set:
|
v_str = np.array2string(v_t,
|
||||||
r += " ({})".format(len(v))
|
max_line_width=1000,
|
||||||
if t is np.ndarray:
|
precision=precision,
|
||||||
r += " " + str(np.shape(v))
|
suppress_small=True,
|
||||||
|
separator=', ',
|
||||||
|
threshold=4,
|
||||||
|
edgeitems=2,
|
||||||
|
floatmode='maxprec')
|
||||||
|
v_str += " " + unit if unit else ""
|
||||||
|
return v_str
|
||||||
|
elif isinstance(v, (dict, list)):
|
||||||
|
r = t.__name__ + " ({})".format(len(v))
|
||||||
return r
|
return r
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue