Skip to content

Commit

Permalink
Enforce the use of UnitDescriptors as units. (#380)
Browse files Browse the repository at this point in the history
  * Enforce the use of UnitDescriptors as units.
  * Add unit test for UNECE unit enforcement.
  • Loading branch information
jettisonjoe authored Jul 21, 2016
1 parent 86d9ff7 commit 41062a2
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 11 deletions.
9 changes: 5 additions & 4 deletions openhtf/bin/units_from_xls.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@
from openhtf.util import units
# The following three lines are equivalent:
@measures(units.METRE_PER_SECOND)
@measures(units.Unit('m/s'))
@measures(units.Unit('metre per second'))
# The following three expressions are equivalent:
units.METRE_PER_SECOND
units.Unit('m/s')
units.Unit('metre per second')
OpenHTF uses UNECE unit codes internally because they are relatively complete
and modern, and because they are recognized internationally. For full details
Expand Down Expand Up @@ -136,6 +136,7 @@ def __call__(self, name_or_suffix):
UNITS_BY_ALL = {}
UNITS_BY_ALL.update(UNITS_BY_NAME)
UNITS_BY_ALL.update(UNITS_BY_SUFFIX)
UNITS_BY_ALL[None] = NONE
Unit = UnitLookup(UNITS_BY_ALL)
'''
Expand Down
17 changes: 14 additions & 3 deletions openhtf/util/measurements.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ def WidgetTestPhase(test):

import openhtf
from openhtf.util import validators
from openhtf.util import units

_LOG = logging.getLogger(__name__)

Expand Down Expand Up @@ -126,14 +127,24 @@ def Doc(self, docstring):
self.docstring = docstring
return self

def WithUnits(self, units):
def _maybe_make_unit_desc(self, unit_desc):
"""Return the UnitDescriptor or convert a string to one."""
if isinstance(unit_desc, str) or unit_desc is None:
unit_desc = units.Unit(unit_desc)
if not isinstance(unit_desc, units.UnitDescriptor):
raise TypeError('Invalid units for measurement %s: %s' % (self.name,
unit_desc))
return unit_desc

def WithUnits(self, unit_desc):
"""Declare the units for this Measurement, returns self for chaining."""
self.units = units
self.units = self._maybe_make_unit_desc(unit_desc)
return self

def WithDimensions(self, *dimensions):
"""Declare dimensions for this Measurement, returns self for chaining."""
self.dimensions = dimensions
self.dimensions = tuple(
self._maybe_make_unit_desc(dim) for dim in dimensions)
return self

def WithValidator(self, validator):
Expand Down
9 changes: 5 additions & 4 deletions openhtf/util/units.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
from openhtf.util import units
# The following three lines are equivalent:
@measures(units.METRE_PER_SECOND)
@measures(units.Unit('m/s'))
@measures(units.Unit('metre per second'))
# The following three expressions are equivalent:
units.METRE_PER_SECOND
units.Unit('m/s')
units.Unit('metre per second')
OpenHTF uses UNECE unit codes internally because they are relatively complete
and modern, and because they are recognized internationally. For full details
Expand Down Expand Up @@ -4254,5 +4254,6 @@ def __call__(self, name_or_suffix):
UNITS_BY_ALL = {}
UNITS_BY_ALL.update(UNITS_BY_NAME)
UNITS_BY_ALL.update(UNITS_BY_SUFFIX)
UNITS_BY_ALL[None] = NONE

Unit = UnitLookup(UNITS_BY_ALL)
4 changes: 4 additions & 0 deletions test/util/measurements_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ def setUpClass(cls):
with open(RECORD_FILENAME, 'rb') as picklefile:
cls.record = pickle.load(picklefile)

def testUnitEnforcement(self):
"""Creating a measurement with invalid units should raise."""
self.assertRaises(TypeError, Measurement('bad_units').WithUnits, 1701)

def testMeasurements(self):
result = util.NonLocalResult()
def _SaveResult(test_record):
Expand Down

0 comments on commit 41062a2

Please sign in to comment.