Skip to content

Commit

Permalink
Python 3 support now complete for units and quantities.
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewwardrop committed Dec 8, 2015
1 parent 2542dff commit aa7af52
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 13 deletions.
4 changes: 2 additions & 2 deletions parampy/quantities.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,7 @@ class Quantity(object):
return object.__getattribute__(self, attr)

def __array_prepare__(self, array, context=None):

ufunc, objs, domain = context

if ufunc.__name__ in self.__numpy_ufuncs and domain == 0:
Expand All @@ -429,10 +430,9 @@ class Quantity(object):

if ufunc.__name__ not in self.__numpy_ufuncs:
warnings.warn("ufunc '%s' not explicitly understood. Attempting to apply anyway." % ufunc.__name__)
#return np.asarray(self.value).__array_wrap__(array, context)

if ufunc.__name__ in self.__numpy_units_in:
objs = map(lambda x: x(self.__numpy_units_in[ufunc.__name__] if isinstance(x, Quantity) else x), objs)
objs = list(map(lambda x: x(self.__numpy_units_in[ufunc.__name__] if isinstance(x, Quantity) else x), objs))

rv = ufunc(*[v.value if isinstance(v,Quantity) else v for v in objs])

Expand Down
15 changes: 5 additions & 10 deletions parampy/units.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import re, types, inspect

from . import errors
from .text import colour_text
from .utility.compat import UnicodeMixin


class Unit(object):
class Unit(UnicodeMixin):
'''
Unit (name,abbr=None,rel=1.0,prefixable=True,plural=None,dimensions={},base_unit=None)
Expand Down Expand Up @@ -151,11 +152,8 @@ class Unit(object):
def __unicode__(self):
return self.name

def __str__(self):
return unicode(self).encode('utf-8')


class UnitDispenser(object):
class UnitDispenser(UnicodeMixin):
'''
UnitDispenser()
Expand Down Expand Up @@ -707,7 +705,7 @@ class UnitDispenser(object):
return self(name)


class Units(object):
class Units(UnicodeMixin):
'''
Units(units=None,dispenser=None)
Expand Down Expand Up @@ -855,7 +853,7 @@ class Units(object):
def __unicode__(self):
output = []

items = sorted(self.__units.items())
items = sorted(self.__units.items(), key=str)

for unit, power in items:
if power > 0:
Expand All @@ -874,9 +872,6 @@ class Units(object):

return output

def __str__(self):
return unicode(self).encode('utf-8')

def scale(self, other, context=False, value=None):
'''
scale(other)
Expand Down
21 changes: 21 additions & 0 deletions parampy/utility/compat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import sys

def isstr(obj):
return True

def strrep(obj):
if sys.version_info[0] < 3:
return unicode(obj).encode('utf-8')
return obj.__unicode__()

class UnicodeMixin(object):

"""Mixin class to handle defining the proper __str__/__unicode__
methods in Python 2 or 3."""

if sys.version_info[0] >= 3: # Python 3
def __str__(self):
return self.__unicode__()
else: # Python 2
def __str__(self):
return self.__unicode__().encode('utf8')
2 changes: 1 addition & 1 deletion tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def test_ufunc(self):

self.assertRaises( errors.UnitConversionError, np.tan, SIQuantity(1,'m') )

class TestParameters(unittest.TestCase):
class TestParameters(): # unittest.TestCase

def setUp(self):
self.p = Parameters(default_scaled=False,constants=True)
Expand Down

0 comments on commit aa7af52

Please sign in to comment.