mirror of https://github.com/m-labs/artiq.git
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:
|
else:
|
||||||
if not isinstance(value, Quantity) or value.unit != unit:
|
if not isinstance(value, Quantity) or value.unit != unit:
|
||||||
raise DimensionError
|
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