Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Fix basic_units.py on Python 3. PEP8 and PyLint cleanup. #980

Merged
merged 2 commits into from

2 participants

@cgohlke

No description provided.

examples/units/basic_units.py
@@ -235,21 +246,23 @@ def get_conversion_fn(self, unit):
return self.conversions[unit]
def convert_value_to(self, value, unit):
- #print 'convert value to: value ="%s", unit="%s"'%(value, type(unit)), self.conversions
+ #print 'convert value to: value ="%s", unit="%s"'%(value, type(unit)),
+ #self.conversions
@pelson Collaborator
pelson added a note

This changes the meaning if the print statement is uncommented. Could just remove the print statement...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@pelson pelson commented on the diff
examples/units/basic_units.py
((6 lines not shown))
self.proxy_target = self.value
- def get_compressed_copy(self, mask):
@pelson Collaborator
pelson added a note

Where has this gone?

@pelson Collaborator
pelson added a note

I guess it is just an example, and if its not used in the example, then its not actually needed...

@cgohlke
cgohlke added a note

It was a duplicate.

@pelson Collaborator
pelson added a note

ok. thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@pelson
Collaborator

+1

@pelson
Collaborator

Thanks @cgohlke we share a common interest in being picky about whitespace/trying to make PyLint happy :-)

@pelson pelson merged commit 1418e4a into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 51 additions and 45 deletions.
  1. +51 −45 examples/units/basic_units.py
View
96 examples/units/basic_units.py
@@ -1,4 +1,5 @@
import math
+
import numpy as np
import matplotlib.units as units
@@ -6,35 +7,42 @@
from matplotlib.axes import Axes
from matplotlib.cbook import iterable
+
class ProxyDelegate(object):
def __init__(self, fn_name, proxy_type):
self.proxy_type = proxy_type
self.fn_name = fn_name
+
def __get__(self, obj, objtype=None):
return self.proxy_type(self.fn_name, obj)
+
class TaggedValueMeta (type):
def __init__(cls, name, bases, dict):
for fn_name in cls._proxies.keys():
try:
dummy = getattr(cls, fn_name)
except AttributeError:
- setattr(cls, fn_name, ProxyDelegate(fn_name, cls._proxies[fn_name]))
+ setattr(cls, fn_name,
+ ProxyDelegate(fn_name, cls._proxies[fn_name]))
+
class PassThroughProxy(object):
def __init__(self, fn_name, obj):
self.fn_name = fn_name
self.target = obj.proxy_target
+
def __call__(self, *args):
- #print 'passthrough', self.target, self.fn_name
fn = getattr(self.target, self.fn_name)
ret = fn(*args)
return ret
+
class ConvertArgsProxy(PassThroughProxy):
def __init__(self, fn_name, obj):
PassThroughProxy.__init__(self, fn_name, obj)
self.unit = obj.unit
+
def __call__(self, *args):
converted_args = []
for a in args:
@@ -45,16 +53,19 @@ def __call__(self, *args):
converted_args = tuple([c.get_value() for c in converted_args])
return PassThroughProxy.__call__(self, *converted_args)
+
class ConvertReturnProxy(PassThroughProxy):
def __init__(self, fn_name, obj):
PassThroughProxy.__init__(self, fn_name, obj)
self.unit = obj.unit
+
def __call__(self, *args):
ret = PassThroughProxy.__call__(self, *args)
if (type(ret) == type(NotImplemented)):
return NotImplemented
return TaggedValue(ret, self.unit)
+
class ConvertAllProxy(PassThroughProxy):
def __init__(self, fn_name, obj):
PassThroughProxy.__init__(self, fn_name, obj)
@@ -91,17 +102,17 @@ def __call__(self, *args):
return NotImplemented
return TaggedValue(ret, ret_unit)
-class TaggedValue (object):
- __metaclass__ = TaggedValueMeta
- _proxies = {'__add__':ConvertAllProxy,
- '__sub__':ConvertAllProxy,
- '__mul__':ConvertAllProxy,
- '__rmul__':ConvertAllProxy,
- '__cmp__':ConvertAllProxy,
- '__lt__':ConvertAllProxy,
- '__gt__':ConvertAllProxy,
- '__len__':PassThroughProxy}
+class _TaggedValue(object):
+
+ _proxies = {'__add__': ConvertAllProxy,
+ '__sub__': ConvertAllProxy,
+ '__mul__': ConvertAllProxy,
+ '__rmul__': ConvertAllProxy,
+ '__cmp__': ConvertAllProxy,
+ '__lt__': ConvertAllProxy,
+ '__gt__': ConvertAllProxy,
+ '__len__': PassThroughProxy}
def __new__(cls, value, unit):
# generate a new subclass for value
@@ -120,13 +131,9 @@ def __new__(cls, value, unit):
def __init__(self, value, unit):
self.value = value
- self.unit = unit
+ self.unit = unit
self.proxy_target = self.value
- def get_compressed_copy(self, mask):
@pelson Collaborator
pelson added a note

Where has this gone?

@cgohlke
cgohlke added a note

It was a duplicate.

@pelson Collaborator
pelson added a note

I guess it is just an example, and if its not used in the example, then its not actually needed...

@pelson Collaborator
pelson added a note

ok. thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
- compressed_value = np.ma.masked_array(self.value, mask=mask).compressed()
- return TaggedValue(compressed_value, self.unit)
-
def __getattribute__(self, name):
if (name.startswith('__')):
return object.__getattribute__(self, name)
@@ -135,7 +142,7 @@ def __getattribute__(self, name):
return getattr(variable, name)
return object.__getattribute__(self, name)
- def __array__(self, t = None, context = None):
+ def __array__(self, t=None, context=None):
if t is not None:
return np.asarray(self.value).astype(t)
else:
@@ -158,6 +165,7 @@ class IteratorProxy(object):
def __init__(self, iter, unit):
self.iter = iter
self.unit = unit
+
def __next__(self):
value = next(self.iter)
return TaggedValue(value, self.unit)
@@ -169,7 +177,6 @@ def get_compressed_copy(self, mask):
return TaggedValue(new_value, self.unit)
def convert_to(self, unit):
- #print 'convert to', unit, self.unit
if (unit == self.unit or not unit):
return self
new_value = self.unit.convert_value_to(self.value, unit)
@@ -182,14 +189,17 @@ def get_unit(self):
return self.unit
+TaggedValue = TaggedValueMeta('TaggedValue', (_TaggedValue, ), {})
+
+
class BasicUnit(object):
def __init__(self, name, fullname=None):
self.name = name
- if fullname is None: fullname = name
+ if fullname is None:
+ fullname = name
self.fullname = fullname
self.conversions = dict()
-
def __repr__(self):
return 'BasicUnit(%s)'%self.name
@@ -201,11 +211,11 @@ def __call__(self, value):
def __mul__(self, rhs):
value = rhs
- unit = self
+ unit = self
if hasattr(rhs, 'get_unit'):
value = rhs.get_value()
- unit = rhs.get_unit()
- unit = unit_resolver('__mul__', (self, unit))
+ unit = rhs.get_unit()
+ unit = unit_resolver('__mul__', (self, unit))
if (unit == NotImplemented):
return NotImplemented
return TaggedValue(value, unit)
@@ -235,21 +245,21 @@ def get_conversion_fn(self, unit):
return self.conversions[unit]
def convert_value_to(self, value, unit):
- #print 'convert value to: value ="%s", unit="%s"'%(value, type(unit)), self.conversions
conversion_fn = self.conversions[unit]
ret = conversion_fn(value)
return ret
-
def get_unit(self):
return self
+
class UnitResolver(object):
def addition_rule(self, units):
for unit_1, unit_2 in zip(units[:-1], units[1:]):
if (unit_1 != unit_2):
return NotImplemented
return units[0]
+
def multiplication_rule(self, units):
non_null = [u for u in units if u]
if (len(non_null) > 1):
@@ -257,13 +267,12 @@ def multiplication_rule(self, units):
return non_null[0]
op_dict = {
- '__mul__':multiplication_rule,
- '__rmul__':multiplication_rule,
- '__add__':addition_rule,
- '__radd__':addition_rule,
- '__sub__':addition_rule,
- '__rsub__':addition_rule,
- }
+ '__mul__': multiplication_rule,
+ '__rmul__': multiplication_rule,
+ '__add__': addition_rule,
+ '__radd__': addition_rule,
+ '__sub__': addition_rule,
+ '__rsub__': addition_rule}
def __call__(self, operation, units):
if (operation not in self.op_dict):
@@ -271,8 +280,8 @@ def __call__(self, operation, units):
return self.op_dict[operation](self, units)
-unit_resolver = UnitResolver()
+unit_resolver = UnitResolver()
cm = BasicUnit('cm', 'centimeters')
inch = BasicUnit('inch', 'inches')
@@ -288,11 +297,12 @@ def __call__(self, operation, units):
hertz = BasicUnit('Hz', 'Hertz')
minutes = BasicUnit('min', 'minutes')
-secs.add_conversion_fn(hertz, lambda x:1./x)
+secs.add_conversion_fn(hertz, lambda x: 1./x)
secs.add_conversion_factor(minutes, 1/60.0)
+
# radians formatting
-def rad_fn(x,pos=None):
+def rad_fn(x, pos=None):
n = int((x / np.pi) * 2.0 + 0.25)
if n == 0:
return '0'
@@ -335,7 +345,6 @@ def axisinfo(unit, axis):
def convert(val, unit, axis):
if units.ConversionInterface.is_numlike(val):
return val
- #print 'convert checking iterable'
if iterable(val):
return [thisval.convert_to(unit).get_value() for thisval in val]
else:
@@ -350,15 +359,12 @@ def default_units(x, axis):
return x.unit
-
-def cos( x ):
- if ( iterable(x) ):
- result = []
- for val in x:
- result.append( math.cos( val.convert_to( radians ).get_value() ) )
- return result
+def cos(x):
+ if iterable(x):
+ return [math.cos(val.convert_to(radians).get_value()) for val in x]
else:
- return math.cos( x.convert_to( radians ).get_value() )
+ return math.cos(x.convert_to(radians).get_value())
+
basicConverter = BasicUnitConverter()
units.registry[BasicUnit] = basicConverter
Something went wrong with that request. Please try again.