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)
|
_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):
|
class DimensionError(Exception):
|
||||||
"""Exception raised when attempting operations on incompatible units
|
"""Exception raised when attempting operations on incompatible units
|
||||||
(e.g. adding seconds and hertz).
|
(e.g. adding seconds and hertz).
|
||||||
|
@ -25,7 +14,8 @@ class DimensionError(Exception):
|
||||||
|
|
||||||
|
|
||||||
class Quantity:
|
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,
|
The amount can be of any Python numerical type (integer, float,
|
||||||
Fraction, ...).
|
Fraction, ...).
|
||||||
|
@ -144,14 +134,12 @@ class Quantity:
|
||||||
return self.amount >= other.amount
|
return self.amount >= other.amount
|
||||||
|
|
||||||
|
|
||||||
def _register_unit(name, prefixes):
|
def _register_unit(unit, prefixes):
|
||||||
unit = Unit(name)
|
|
||||||
globals()[name+"_unit"] = unit
|
|
||||||
amount = _smallest_prefix
|
amount = _smallest_prefix
|
||||||
for prefix in _prefixes_str:
|
for prefix in _prefixes_str:
|
||||||
if prefix in prefixes:
|
if prefix in prefixes:
|
||||||
quantity = Quantity(amount, unit)
|
quantity = Quantity(amount, unit)
|
||||||
full_name = prefix + name if prefix != "_" else name
|
full_name = prefix + unit if prefix != "_" else unit
|
||||||
globals()[full_name] = quantity
|
globals()[full_name] = quantity
|
||||||
amount *= 1000
|
amount *= 1000
|
||||||
|
|
||||||
|
|
|
@ -48,8 +48,7 @@ class _ReferenceManager:
|
||||||
for kg in core_language.kernel_globals:
|
for kg in core_language.kernel_globals:
|
||||||
self.use_count[kg] = 1
|
self.use_count[kg] = 1
|
||||||
for name in ("int", "round", "int64", "round64", "float", "array",
|
for name in ("int", "round", "int64", "round64", "float", "array",
|
||||||
"range", "Fraction", "Quantity", "EncodedException",
|
"range", "Fraction", "Quantity", "EncodedException"):
|
||||||
"s_unit", "Hz_unit"):
|
|
||||||
self.use_count[name] = 1
|
self.use_count[name] = 1
|
||||||
|
|
||||||
def new_name(self, base_name):
|
def new_name(self, base_name):
|
||||||
|
|
|
@ -35,8 +35,7 @@ def value_to_ast(value):
|
||||||
if isinstance(value, units.Quantity):
|
if isinstance(value, units.Quantity):
|
||||||
return ast.Call(
|
return ast.Call(
|
||||||
func=ast.Name("Quantity", ast.Load()),
|
func=ast.Name("Quantity", ast.Load()),
|
||||||
args=[value_to_ast(value.amount),
|
args=[value_to_ast(value.amount), ast.Str(value.unit)],
|
||||||
ast.Name(value.unit.name+"_unit", ast.Load())],
|
|
||||||
keywords=[], starargs=None, kwargs=None)
|
keywords=[], starargs=None, kwargs=None)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue