<s> """<STR_LIT>""" <EOL> __all__ = [ '<STR_LIT:A>' , '<STR_LIT>' , '<STR_LIT:D>' , '<STR_LIT>' ] <EOL> from decimal import Decimal <EOL> class MeasureBase ( object ) : <EOL> def default_units ( self , kwargs ) : <EOL> """<STR_LIT>""" <EOL> val = <NUM_LIT:0.0> <EOL> for unit , value in kwargs . iteritems ( ) : <EOL> if not isinstance ( value , float ) : value = float ( value ) <EOL> if unit in self . UNITS : <EOL> val += self . UNITS [ unit ] * value <EOL> default_unit = unit <EOL> elif unit in self . ALIAS : <EOL> u = self . ALIAS [ unit ] <EOL> val += self . UNITS [ u ] * value <EOL> default_unit = u <EOL> else : <EOL> lower = unit . lower ( ) <EOL> if lower in self . UNITS : <EOL> val += self . UNITS [ lower ] * value <EOL> default_unit = lower <EOL> elif lower in self . LALIAS : <EOL> u = self . LALIAS [ lower ] <EOL> val += self . UNITS [ u ] * value <EOL> default_unit = u <EOL> else : <EOL> raise AttributeError ( '<STR_LIT>' % unit ) <EOL> return val , default_unit <EOL> @ classmethod <EOL> def unit_attname ( cls , unit_str ) : <EOL> """<STR_LIT>""" <EOL> lower = unit_str . lower ( ) <EOL> if unit_str in cls . UNITS : <EOL> return unit_str <EOL> elif lower in cls . UNITS : <EOL> return lower <EOL> elif lower in cls . LALIAS : <EOL> return cls . LALIAS [ lower ] <EOL> else : <EOL> raise Exception ( '<STR_LIT>' % unit_str ) <EOL> class Distance ( MeasureBase ) : <EOL> UNITS = { <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT:m>' : <NUM_LIT:1.0> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> } <EOL> ALIAS = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:m>' , <EOL> '<STR_LIT>' : '<STR_LIT:m>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> "<STR_LIT>" : '<STR_LIT>' , <EOL> "<STR_LIT>" : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> LALIAS = dict ( [ ( k . lower ( ) , v ) for k , v in ALIAS . items ( ) ] ) <EOL> def __init__ ( self , default_unit = None , ** kwargs ) : <EOL> self . m , self . _default_unit = self . default_units ( kwargs ) <EOL> if default_unit and isinstance ( default_unit , str ) : <EOL> self . _default_unit = default_unit <EOL> def __getattr__ ( self , name ) : <EOL> if name in self . UNITS : <EOL> return self . m / self . UNITS [ name ] <EOL> else : <EOL> raise AttributeError ( '<STR_LIT>' % name ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( self . _default_unit , getattr ( self , self . _default_unit ) ) <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' % ( getattr ( self , self . _default_unit ) , self . _default_unit ) <EOL> def __cmp__ ( self , other ) : <EOL> if isinstance ( other , Distance ) : <EOL> return cmp ( self . m , other . m ) <EOL> else : <EOL> return NotImplemented <EOL> def __add__ ( self , other ) : <EOL> if isinstance ( other , Distance ) : <EOL> return Distance ( default_unit = self . _default_unit , m = ( self . m + other . m ) ) <EOL> else : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> def __iadd__ ( self , other ) : <EOL> if isinstance ( other , Distance ) : <EOL> self . m += other . m <EOL> return self <EOL> else : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> def __sub__ ( self , other ) : <EOL> if isinstance ( other , Distance ) : <EOL> return Distance ( default_unit = self . _default_unit , m = ( self . m - other . m ) ) <EOL> else : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> def __isub__ ( self , other ) : <EOL> if isinstance ( other , Distance ) : <EOL> self . m -= other . m <EOL> return self <EOL> else : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> def __mul__ ( self , other ) : <EOL> if isinstance ( other , ( int , float , long , Decimal ) ) : <EOL> return Distance ( default_unit = self . _default_unit , m = ( self . m * float ( other ) ) ) <EOL> elif isinstance ( other , Distance ) : <EOL> return Area ( default_unit = '<STR_LIT>' + self . _default_unit , sq_m = ( self . m * other . m ) ) <EOL> else : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> def __imul__ ( self , other ) : <EOL> if isinstance ( other , ( int , float , long , Decimal ) ) : <EOL> self . m *= float ( other ) <EOL> return self <EOL> else : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> def __rmul__ ( self , other ) : <EOL> return self * other <EOL> def __div__ ( self , other ) : <EOL> if isinstance ( other , ( int , float , long , Decimal ) ) : <EOL> return Distance ( default_unit = self . _default_unit , m = ( self . m / float ( other ) ) ) <EOL> else : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> def __idiv__ ( self , other ) : <EOL> if isinstance ( other , ( int , float , long , Decimal ) ) : <EOL> self . m /= float ( other ) <EOL> return self <EOL> else : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> def __nonzero__ ( self ) : <EOL> return bool ( self . m ) <EOL> class Area ( MeasureBase ) : <EOL> UNITS = dict ( [ ( '<STR_LIT>' % k , v ** <NUM_LIT:2> ) for k , v in Distance . UNITS . items ( ) ] ) <EOL> ALIAS = dict ( [ ( k , '<STR_LIT>' % v ) for k , v in Distance . ALIAS . items ( ) ] ) <EOL> LALIAS = dict ( [ ( k . lower ( ) , v ) for k , v in ALIAS . items ( ) ] ) <EOL> def __init__ ( self , default_unit = None , ** kwargs ) : <EOL> self . sq_m , self . _default_unit = self . default_units ( kwargs ) <EOL> if default_unit and isinstance ( default_unit , str ) : <EOL> self . _default_unit = default_unit <EOL> def __getattr__ ( self , name ) : <EOL> if name in self . UNITS : <EOL> return self . sq_m / self . UNITS [ name ] <EOL> else : <EOL> raise AttributeError ( '<STR_LIT>' + name ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( self . _default_unit , getattr ( self , self . _default_unit ) ) <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' % ( getattr ( self , self . _default_unit ) , self . _default_unit ) <EOL> def __cmp__ ( self , other ) : <EOL> if isinstance ( other , Area ) : <EOL> return cmp ( self . sq_m , other . sq_m ) <EOL> else : <EOL> return NotImplemented <EOL> def __add__ ( self , other ) : <EOL> if isinstance ( other , Area ) : <EOL> return Area ( default_unit = self . _default_unit , sq_m = ( self . sq_m + other . sq_m ) ) <EOL> else : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> def __iadd__ ( self , other ) : <EOL> if isinstance ( other , Area ) : <EOL> self . sq_m += other . sq_m <EOL> return self <EOL> else : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> def __sub__ ( self , other ) : <EOL> if isinstance ( other , Area ) : <EOL> return Area ( default_unit = self . _default_unit , sq_m = ( self . sq_m - other . sq_m ) ) <EOL> else : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> def __isub__ ( self , other ) : <EOL> if isinstance ( other , Area ) : <EOL> self . sq_m -= other . sq_m <EOL> return self <EOL> else : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> def __mul__ ( self , other ) : <EOL> if isinstance ( other , ( int , float , long , Decimal ) ) : <EOL> return Area ( default_unit = self . _default_unit , sq_m = ( self . sq_m * float ( other ) ) ) <EOL> else : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> def __imul__ ( self , other ) : <EOL> if isinstance ( other , ( int , float , long , Decimal ) ) : <EOL> self . sq_m *= float ( other ) <EOL> return self <EOL> else : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> def __rmul__ ( self , other ) : <EOL> return self * other <EOL> def __div__ ( self , other ) : <EOL> if isinstance ( other , ( int , float , long , Decimal ) ) : <EOL> return Area ( default_unit = self . _default_unit , sq_m = ( self . sq_m / float ( other ) ) ) <EOL> else : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> def __idiv__ ( self , other ) : <EOL> if isinstance ( other , ( int , float , long , Decimal ) ) : <EOL> self . sq_m /= float ( other ) <EOL> return self <EOL> else : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> def __nonzero__ ( self ) : <EOL> return bool ( self . sq_m ) <EOL> D = Distance <EOL> A = Area </s>
