language/units: cleanup and add docstrings

This commit is contained in:
Sebastien Bourdeauducq 2014-09-29 23:41:37 +08:00
parent f105e5e214
commit d3e70ec53d
2 changed files with 31 additions and 5 deletions

View File

@ -1,25 +1,45 @@
from collections import namedtuple from fractions import Fraction as _Fraction
from fractions import Fraction
_prefixes_str = "pnum_kMG" _prefixes_str = "pnum_kMG"
_smallest_prefix = Fraction(1, 10**12) _smallest_prefix = _Fraction(1, 10**12)
Unit = namedtuple("Unit", "name")
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
(e.g. adding seconds and hertz).
"""
pass pass
class Quantity: class Quantity:
"""Represents an amount in a given fundamental unit (:class:`Unit`).
The amount can be of any Python numerical type (integer, float,
Fraction, ...).
Arithmetic operations and comparisons are directly delegated to the
underlying numerical types.
"""
def __init__(self, amount, unit): def __init__(self, amount, unit):
self.amount = amount self.amount = amount
self.unit = unit self.unit = unit
def __repr__(self): def __repr__(self):
r_amount = self.amount r_amount = self.amount
if isinstance(r_amount, int) or isinstance(r_amount, Fraction): if isinstance(r_amount, int) or isinstance(r_amount, _Fraction):
r_prefix = 0 r_prefix = 0
r_amount = r_amount/_smallest_prefix r_amount = r_amount/_smallest_prefix
if r_amount: if r_amount:

View File

@ -6,3 +6,9 @@ Core language reference
.. automodule:: artiq.language.core .. automodule:: artiq.language.core
:members: :members:
:mod:`artiq.language.units` module
---------------------------------
.. automodule:: artiq.language.units
:members: