mirror of
https://github.com/m-labs/artiq.git
synced 2024-12-25 03:08:27 +08:00
language/units: add strip_unit function
This commit is contained in:
parent
14e481d154
commit
ee9d616733
@ -239,3 +239,26 @@ def check_unit(value, unit):
|
||||
else:
|
||||
if not isinstance(value, Quantity) or value.unit != unit:
|
||||
raise DimensionError
|
||||
|
||||
def strip_unit(value, unit):
|
||||
"""Check that the value has the specified unit and returns its amount.
|
||||
Raises ``DimensionError`` if the units does not match.
|
||||
|
||||
If the passed value is not a ``Quantity``, it is assumed to be in the
|
||||
specified unit and is returned unchanged.
|
||||
|
||||
If ``unit`` is ``None``, passing a ``Quantity`` as value raises
|
||||
``DimensionError``.
|
||||
|
||||
"""
|
||||
if unit is None:
|
||||
if isinstance(value, Quantity):
|
||||
raise DimensionError
|
||||
else:
|
||||
return value
|
||||
else:
|
||||
if isinstance(value, Quantity):
|
||||
if value.unit != unit:
|
||||
raise DimensionError
|
||||
else:
|
||||
return value.amount
|
||||
|
Loading…
Reference in New Issue
Block a user