Skip to content

Commit

Permalink
Remove the _Unit factory, and make it a class method. (#131)
Browse files Browse the repository at this point in the history
  • Loading branch information
pelson authored and bjlittle committed Jan 17, 2019
1 parent c6ef762 commit 1836f32
Showing 1 changed file with 26 additions and 21 deletions.
47 changes: 26 additions & 21 deletions cf_units/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -614,18 +614,6 @@ def _num2date_to_nearest_second(time_value, utime):
return result


########################################################################
#
# unit wrapper class for unidata/ucar UDUNITS-2
#
########################################################################

def _Unit(category, ut_unit, calendar=None, origin=None):
unit = _OrderedHashable.__new__(Unit)
unit._init(category, ut_unit, calendar, origin)
return unit


_CACHE = {}


Expand Down Expand Up @@ -716,6 +704,8 @@ class Unit(_OrderedHashable):
"""
def _init_from_tuple(self, values):
# Implements the required interface for an _OrderedHashable.
# This will also ensure a Unit._init(*Unit.names) method exists.
for name, value in zip(self._names, values):
object.__setattr__(self, name, value)

Expand Down Expand Up @@ -858,7 +848,17 @@ def __init__(self, unit, calendar=None):
msg = 'Expected string-like calendar argument, got {!r}.'
raise TypeError(msg.format(type(calendar)))

self._init_from_tuple((category, ut_unit, calendar_, unit,))
# Call the OrderedHashable's init.
self._init(category, ut_unit, calendar_, unit,)

@classmethod
def _new_from_existing_ut(cls, category, ut_unit,
calendar=None, origin=None):
# Short-circuit __init__ if we know what we are doing and already
# have a UT handle.
unit = cls.__new__(cls)
unit._init(category, ut_unit, calendar, origin)
return unit

def _propogate_error(self, msg, ud_err):
"""
Expand Down Expand Up @@ -1317,7 +1317,7 @@ def offset_by_time(self, origin):
except _ud.UdunitsError as e:
self._propogate_error('Failed to offset %r' % self, e)
calendar = None
return _Unit(_CATEGORY_UDUNIT, ut_unit, calendar)
return Unit._new_from_existing_ut(_CATEGORY_UDUNIT, ut_unit, calendar)

def invert(self):
"""
Expand Down Expand Up @@ -1345,7 +1345,8 @@ def invert(self):
except _ud.UdunitsError as e:
self._propogate_error('Failed to invert %r' % self, e)
calendar = None
result = _Unit(_CATEGORY_UDUNIT, ut_unit, calendar)
result = Unit._new_from_existing_ut(
_CATEGORY_UDUNIT, ut_unit, calendar)
return result

def root(self, root):
Expand Down Expand Up @@ -1388,7 +1389,8 @@ def root(self, root):
self._propogate_error('Failed to take the root of %r' %
self, e)
calendar = None
result = _Unit(_CATEGORY_UDUNIT, ut_unit, calendar)
result = Unit._new_from_existing_ut(
_CATEGORY_UDUNIT, ut_unit, calendar)
return result

def log(self, base):
Expand Down Expand Up @@ -1425,7 +1427,8 @@ def log(self, base):
msg = 'Failed to calculate logorithmic base of %r' % self
self._propogate_error(msg, e)
calendar = None
result = _Unit(_CATEGORY_UDUNIT, ut_unit, calendar)
result = Unit._new_from_existing_ut(
_CATEGORY_UDUNIT, ut_unit, calendar)
return result

def __str__(self):
Expand Down Expand Up @@ -1482,7 +1485,8 @@ def _offset_common(self, offset):
self._propogate_error('Failed to offset %r' % self, e)
else:
calendar = None
result = _Unit(_CATEGORY_UDUNIT, ut_unit, calendar)
result = Unit._new_from_existing_ut(
_CATEGORY_UDUNIT, ut_unit, calendar)
return result

def __add__(self, other):
Expand All @@ -1509,15 +1513,16 @@ def _op_common(self, other, op_func):
raise ValueError("Cannot %s a 'no-unit'." % op_label)

if self.is_unknown() or other.is_unknown():
result = _Unit(_CATEGORY_UNKNOWN, None)
result = Unit(_UNKNOWN_UNIT_STRING)
else:
try:
ut_unit = op_func(self.ut_unit, other.ut_unit)
except _ud.UdunitsError as e:
msg = 'Failed to %s %r by %r' % (op_label, self, other)
self._propogate_error(msg, e)
calendar = None
result = _Unit(_CATEGORY_UDUNIT, ut_unit, calendar)
result = Unit._new_from_existing_ut(
_CATEGORY_UDUNIT, ut_unit, calendar)
return result

def __rmul__(self, other):
Expand Down Expand Up @@ -1665,7 +1670,7 @@ def __pow__(self, power):
except _ud.UdunitsError as e:
self._propogate_error('Failed to raise the power of %r' %
self, e)
result = _Unit(_CATEGORY_UDUNIT, ut_unit)
result = Unit._new_from_existing_ut(_CATEGORY_UDUNIT, ut_unit)
return result

def __eq__(self, other):
Expand Down

0 comments on commit 1836f32

Please sign in to comment.