66from matplotlib .axes import Axes
77from matplotlib .cbook import iterable
88
9+
910class ProxyDelegate (object ):
1011 def __init__ (self , fn_name , proxy_type ):
1112 self .proxy_type = proxy_type
1213 self .fn_name = fn_name
14+
1315 def __get__ (self , obj , objtype = None ):
1416 return self .proxy_type (self .fn_name , obj )
1517
18+
1619class TaggedValueMeta (type ):
1720 def __init__ (cls , name , bases , dict ):
1821 for fn_name in cls ._proxies .keys ():
1922 try :
2023 dummy = getattr (cls , fn_name )
2124 except AttributeError :
22- setattr (cls , fn_name , ProxyDelegate (fn_name , cls ._proxies [fn_name ]))
25+ setattr (cls , fn_name ,
26+ ProxyDelegate (fn_name , cls ._proxies [fn_name ]))
27+
2328
2429class PassThroughProxy (object ):
2530 def __init__ (self , fn_name , obj ):
2631 self .fn_name = fn_name
2732 self .target = obj .proxy_target
33+
2834 def __call__ (self , * args ):
2935 #print 'passthrough', self.target, self.fn_name
3036 fn = getattr (self .target , self .fn_name )
3137 ret = fn (* args )
3238 return ret
3339
40+
3441class ConvertArgsProxy (PassThroughProxy ):
3542 def __init__ (self , fn_name , obj ):
3643 PassThroughProxy .__init__ (self , fn_name , obj )
3744 self .unit = obj .unit
45+
3846 def __call__ (self , * args ):
3947 converted_args = []
4048 for a in args :
@@ -45,16 +53,19 @@ def __call__(self, *args):
4553 converted_args = tuple ([c .get_value () for c in converted_args ])
4654 return PassThroughProxy .__call__ (self , * converted_args )
4755
56+
4857class ConvertReturnProxy (PassThroughProxy ):
4958 def __init__ (self , fn_name , obj ):
5059 PassThroughProxy .__init__ (self , fn_name , obj )
5160 self .unit = obj .unit
61+
5262 def __call__ (self , * args ):
5363 ret = PassThroughProxy .__call__ (self , * args )
5464 if (type (ret ) == type (NotImplemented )):
5565 return NotImplemented
5666 return TaggedValue (ret , self .unit )
5767
68+
5869class ConvertAllProxy (PassThroughProxy ):
5970 def __init__ (self , fn_name , obj ):
6071 PassThroughProxy .__init__ (self , fn_name , obj )
@@ -91,17 +102,17 @@ def __call__(self, *args):
91102 return NotImplemented
92103 return TaggedValue (ret , ret_unit )
93104
94- class TaggedValue (object ):
95105
96- __metaclass__ = TaggedValueMeta
97- _proxies = {'__add__' :ConvertAllProxy ,
98- '__sub__' :ConvertAllProxy ,
99- '__mul__' :ConvertAllProxy ,
100- '__rmul__' :ConvertAllProxy ,
101- '__cmp__' :ConvertAllProxy ,
102- '__lt__' :ConvertAllProxy ,
103- '__gt__' :ConvertAllProxy ,
104- '__len__' :PassThroughProxy }
106+ class _TaggedValue (object ):
107+
108+ _proxies = {'__add__' : ConvertAllProxy ,
109+ '__sub__' : ConvertAllProxy ,
110+ '__mul__' : ConvertAllProxy ,
111+ '__rmul__' : ConvertAllProxy ,
112+ '__cmp__' : ConvertAllProxy ,
113+ '__lt__' : ConvertAllProxy ,
114+ '__gt__' : ConvertAllProxy ,
115+ '__len__' : PassThroughProxy }
105116
106117 def __new__ (cls , value , unit ):
107118 # generate a new subclass for value
@@ -120,13 +131,9 @@ def __new__(cls, value, unit):
120131
121132 def __init__ (self , value , unit ):
122133 self .value = value
123- self .unit = unit
134+ self .unit = unit
124135 self .proxy_target = self .value
125136
126- def get_compressed_copy (self , mask ):
127- compressed_value = np .ma .masked_array (self .value , mask = mask ).compressed ()
128- return TaggedValue (compressed_value , self .unit )
129-
130137 def __getattribute__ (self , name ):
131138 if (name .startswith ('__' )):
132139 return object .__getattribute__ (self , name )
@@ -135,7 +142,7 @@ def __getattribute__(self, name):
135142 return getattr (variable , name )
136143 return object .__getattribute__ (self , name )
137144
138- def __array__ (self , t = None , context = None ):
145+ def __array__ (self , t = None , context = None ):
139146 if t is not None :
140147 return np .asarray (self .value ).astype (t )
141148 else :
@@ -158,6 +165,7 @@ class IteratorProxy(object):
158165 def __init__ (self , iter , unit ):
159166 self .iter = iter
160167 self .unit = unit
168+
161169 def __next__ (self ):
162170 value = next (self .iter )
163171 return TaggedValue (value , self .unit )
@@ -182,14 +190,17 @@ def get_unit(self):
182190 return self .unit
183191
184192
193+ TaggedValue = TaggedValueMeta ('TaggedValue' , (_TaggedValue , ), {})
194+
195+
185196class BasicUnit (object ):
186197 def __init__ (self , name , fullname = None ):
187198 self .name = name
188- if fullname is None : fullname = name
199+ if fullname is None :
200+ fullname = name
189201 self .fullname = fullname
190202 self .conversions = dict ()
191203
192-
193204 def __repr__ (self ):
194205 return 'BasicUnit(%s)' % self .name
195206
@@ -201,11 +212,11 @@ def __call__(self, value):
201212
202213 def __mul__ (self , rhs ):
203214 value = rhs
204- unit = self
215+ unit = self
205216 if hasattr (rhs , 'get_unit' ):
206217 value = rhs .get_value ()
207- unit = rhs .get_unit ()
208- unit = unit_resolver ('__mul__' , (self , unit ))
218+ unit = rhs .get_unit ()
219+ unit = unit_resolver ('__mul__' , (self , unit ))
209220 if (unit == NotImplemented ):
210221 return NotImplemented
211222 return TaggedValue (value , unit )
@@ -235,44 +246,45 @@ def get_conversion_fn(self, unit):
235246 return self .conversions [unit ]
236247
237248 def convert_value_to (self , value , unit ):
238- #print 'convert value to: value ="%s", unit="%s"'%(value, type(unit)), self.conversions
249+ #print 'convert value to: value ="%s", unit="%s"'%(value, type(unit)),
250+ #self.conversions
239251 conversion_fn = self .conversions [unit ]
240252 ret = conversion_fn (value )
241253 return ret
242254
243-
244255 def get_unit (self ):
245256 return self
246257
258+
247259class UnitResolver (object ):
248260 def addition_rule (self , units ):
249261 for unit_1 , unit_2 in zip (units [:- 1 ], units [1 :]):
250262 if (unit_1 != unit_2 ):
251263 return NotImplemented
252264 return units [0 ]
265+
253266 def multiplication_rule (self , units ):
254267 non_null = [u for u in units if u ]
255268 if (len (non_null ) > 1 ):
256269 return NotImplemented
257270 return non_null [0 ]
258271
259272 op_dict = {
260- '__mul__' :multiplication_rule ,
261- '__rmul__' :multiplication_rule ,
262- '__add__' :addition_rule ,
263- '__radd__' :addition_rule ,
264- '__sub__' :addition_rule ,
265- '__rsub__' :addition_rule ,
266- }
273+ '__mul__' : multiplication_rule ,
274+ '__rmul__' : multiplication_rule ,
275+ '__add__' : addition_rule ,
276+ '__radd__' : addition_rule ,
277+ '__sub__' : addition_rule ,
278+ '__rsub__' : addition_rule }
267279
268280 def __call__ (self , operation , units ):
269281 if (operation not in self .op_dict ):
270282 return NotImplemented
271283
272284 return self .op_dict [operation ](self , units )
273285
274- unit_resolver = UnitResolver ()
275286
287+ unit_resolver = UnitResolver ()
276288
277289cm = BasicUnit ('cm' , 'centimeters' )
278290inch = BasicUnit ('inch' , 'inches' )
@@ -288,11 +300,12 @@ def __call__(self, operation, units):
288300hertz = BasicUnit ('Hz' , 'Hertz' )
289301minutes = BasicUnit ('min' , 'minutes' )
290302
291- secs .add_conversion_fn (hertz , lambda x :1. / x )
303+ secs .add_conversion_fn (hertz , lambda x : 1. / x )
292304secs .add_conversion_factor (minutes , 1 / 60.0 )
293305
306+
294307# radians formatting
295- def rad_fn (x ,pos = None ):
308+ def rad_fn (x , pos = None ):
296309 n = int ((x / np .pi ) * 2.0 + 0.25 )
297310 if n == 0 :
298311 return '0'
@@ -350,15 +363,12 @@ def default_units(x, axis):
350363 return x .unit
351364
352365
353-
354- def cos ( x ):
355- if ( iterable (x ) ):
356- result = []
357- for val in x :
358- result .append ( math .cos ( val .convert_to ( radians ).get_value () ) )
359- return result
366+ def cos (x ):
367+ if iterable (x ):
368+ return [math .cos (val .convert_to (radians ).get_value ()) for val in x ]
360369 else :
361- return math .cos ( x .convert_to ( radians ).get_value () )
370+ return math .cos (x .convert_to (radians ).get_value ())
371+
362372
363373basicConverter = BasicUnitConverter ()
364374units .registry [BasicUnit ] = basicConverter
0 commit comments