forked from M-Labs/artiq
language/units: use only a string to identify units
This commit is contained in:
parent
2944592201
commit
2a19d7d32b
|
@ -5,17 +5,6 @@ _prefixes_str = "pnum_kMG"
|
|||
_smallest_prefix = _Fraction(1, 10**12)
|
||||
|
||||
|
||||
class Unit:
|
||||
"""Represents a fundamental unit (second, hertz, ...).
|
||||
|
||||
"""
|
||||
def __init__(self, name):
|
||||
self.name = name
|
||||
|
||||
def __eq__(self, other):
|
||||
return isinstance(other, Unit) and self.name == other.name
|
||||
|
||||
|
||||
class DimensionError(Exception):
|
||||
"""Exception raised when attempting operations on incompatible units
|
||||
(e.g. adding seconds and hertz).
|
||||
|
@ -25,7 +14,8 @@ class DimensionError(Exception):
|
|||
|
||||
|
||||
class Quantity:
|
||||
"""Represents an amount in a given fundamental unit (:class:`Unit`).
|
||||
"""Represents an amount in a given fundamental unit (identified by a
|
||||
string).
|
||||
|
||||
The amount can be of any Python numerical type (integer, float,
|
||||
Fraction, ...).
|
||||
|
@ -144,14 +134,12 @@ class Quantity:
|
|||
return self.amount >= other.amount
|
||||
|
||||
|
||||
def _register_unit(name, prefixes):
|
||||
unit = Unit(name)
|
||||
globals()[name+"_unit"] = unit
|
||||
def _register_unit(unit, prefixes):
|
||||
amount = _smallest_prefix
|
||||
for prefix in _prefixes_str:
|
||||
if prefix in prefixes:
|
||||
quantity = Quantity(amount, unit)
|
||||
full_name = prefix + name if prefix != "_" else name
|
||||
full_name = prefix + unit if prefix != "_" else unit
|
||||
globals()[full_name] = quantity
|
||||
amount *= 1000
|
||||
|
||||
|
|
|
@ -48,8 +48,7 @@ class _ReferenceManager:
|
|||
for kg in core_language.kernel_globals:
|
||||
self.use_count[kg] = 1
|
||||
for name in ("int", "round", "int64", "round64", "float", "array",
|
||||
"range", "Fraction", "Quantity", "EncodedException",
|
||||
"s_unit", "Hz_unit"):
|
||||
"range", "Fraction", "Quantity", "EncodedException"):
|
||||
self.use_count[name] = 1
|
||||
|
||||
def new_name(self, base_name):
|
||||
|
|
|
@ -35,8 +35,7 @@ def value_to_ast(value):
|
|||
if isinstance(value, units.Quantity):
|
||||
return ast.Call(
|
||||
func=ast.Name("Quantity", ast.Load()),
|
||||
args=[value_to_ast(value.amount),
|
||||
ast.Name(value.unit.name+"_unit", ast.Load())],
|
||||
args=[value_to_ast(value.amount), ast.Str(value.unit)],
|
||||
keywords=[], starargs=None, kwargs=None)
|
||||
return None
|
||||
|
||||
|
|
Loading…
Reference in New Issue