tools: add scale_from_metadata helper func

This commit is contained in:
Simon Renblad 2023-07-20 12:26:43 +08:00 committed by Sébastien Bourdeauducq
parent 4fbff1648c
commit a7569a0b2d

View File

@ -18,7 +18,8 @@ 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", "scale_from_metadata",
"short_format", "file_import",
"get_experiment",
"exc_to_warning", "asyncio_wait_or_cancel",
"get_windows_drives", "get_user_config_dir"]
@ -54,12 +55,15 @@ def elide(s, maxlen):
s += "..."
return s
def scale_from_metadata(metadata):
unit = metadata.get("unit", "")
default_scale = getattr(units, unit, 1)
return metadata.get("scale", default_scale)
def short_format(v, metadata={}):
m = metadata
unit = m.get("unit", "")
default_scale = getattr(units, unit, 1)
scale = m.get("scale", default_scale)
scale = scale_from_metadata(m)
precision = m.get("precision", None)
if v is None:
return "None"