From 2a19d7d32bf8bd4af8e12da2caa9ef2e15393e4c Mon Sep 17 00:00:00 2001 From: Sebastien Bourdeauducq Date: Sun, 5 Oct 2014 21:01:08 +0800 Subject: [PATCH] language/units: use only a string to identify units --- artiq/language/units.py | 20 ++++---------------- artiq/transforms/inline.py | 3 +-- artiq/transforms/tools.py | 3 +-- 3 files changed, 6 insertions(+), 20 deletions(-) diff --git a/artiq/language/units.py b/artiq/language/units.py index 0584706f1..588631f5e 100644 --- a/artiq/language/units.py +++ b/artiq/language/units.py @@ -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 diff --git a/artiq/transforms/inline.py b/artiq/transforms/inline.py index 9bb9388fb..6a846cfaa 100644 --- a/artiq/transforms/inline.py +++ b/artiq/transforms/inline.py @@ -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): diff --git a/artiq/transforms/tools.py b/artiq/transforms/tools.py index b5fb5d547..02eecab45 100644 --- a/artiq/transforms/tools.py +++ b/artiq/transforms/tools.py @@ -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