1+ import six
2+
13import math
24
35import numpy as np
@@ -17,7 +19,7 @@ def __get__(self, obj, objtype=None):
1719return self .proxy_type (self .fn_name ,obj )
1820
1921
20- class TaggedValueMeta (type ):
22+ class TaggedValueMeta (type ):
2123def __init__ (cls ,name ,bases ,dict ):
2224for fn_name in cls ._proxies .keys ():
2325try :
@@ -102,7 +104,7 @@ def __call__(self, *args):
102104return TaggedValue (ret ,ret_unit )
103105
104106
105- class _TaggedValue ( object ):
107+ class TaggedValue ( six . with_metaclass ( TaggedValueMeta ) ):
106108
107109_proxies = {'__add__' :ConvertAllProxy ,
108110'__sub__' :ConvertAllProxy ,
@@ -134,19 +136,13 @@ def __init__(self, value, unit):
134136self .proxy_target = self .value
135137
136138def __getattribute__ (self ,name ):
137- if ( name .startswith ('__' ) ):
139+ if name .startswith ('__' ):
138140return object .__getattribute__ (self ,name )
139141variable = object .__getattribute__ (self ,'value' )
140- if ( hasattr (variable ,name )and name not in self .__class__ .__dict__ ) :
142+ if hasattr (variable ,name )and name not in self .__class__ .__dict__ :
141143return getattr (variable ,name )
142144return object .__getattribute__ (self ,name )
143145
144- def __array__ (self ,t = None ,context = None ):
145- if t is not None :
146- return np .asarray (self .value ).astype (t )
147- else :
148- return np .asarray (self .value ,'O' )
149-
150146def __array_wrap__ (self ,array ,context ):
151147return TaggedValue (array ,self .unit )
152148
@@ -160,16 +156,8 @@ def __len__(self):
160156return len (self .value )
161157
162158def __iter__ (self ):
163- class IteratorProxy (object ):
164- def __init__ (self ,iter ,unit ):
165- self .iter = iter
166- self .unit = unit
167-
168- def __next__ (self ):
169- value = next (self .iter )
170- return TaggedValue (value ,self .unit )
171- next = __next__ # for Python 2
172- return IteratorProxy (iter (self .value ),self .unit )
159+ for inner in self .value :
160+ yield TaggedValue (inner ,self .unit )
173161
174162def get_compressed_copy (self ,mask ):
175163new_value = np .ma .masked_array (self .value ,mask = mask ).compressed ()
@@ -188,9 +176,6 @@ def get_unit(self):
188176return self .unit
189177
190178
191- TaggedValue = TaggedValueMeta ('TaggedValue' , (_TaggedValue , ), {})
192-
193-
194179class BasicUnit (object ):
195180def __init__ (self ,name ,fullname = None ):
196181self .name = name