forked from M-Labs/artiq
1
0
Fork 0

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
1 changed files with 7 additions and 3 deletions

View File

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