<s> """<STR_LIT>""" <EOL> __all__ = [ '<STR_LIT:A>' , '<STR_LIT>' , '<STR_LIT:D>' , '<STR_LIT>' ] <EOL> from decimal import Decimal <EOL> from django . utils . functional import total_ordering <EOL> from django . utils import six <EOL> NUMERIC_TYPES = six . integer_types + ( float , Decimal ) <EOL> AREA_PREFIX = "<STR_LIT>" <EOL> def pretty_name ( obj ) : <EOL> return obj . __name__ if obj . __class__ == type else obj . __class__ . __name__ <EOL> @ total_ordering <EOL> class MeasureBase ( object ) : <EOL> STANDARD_UNIT = None <EOL> ALIAS = { } <EOL> UNITS = { } <EOL> LALIAS = { } <EOL> def __init__ ( self , default_unit = None , ** kwargs ) : <EOL> value , self . _default_unit = self . default_units ( kwargs ) <EOL> setattr ( self , self . STANDARD_UNIT , value ) <EOL> if default_unit and isinstance ( default_unit , six . string_types ) : <EOL> self . _default_unit = default_unit <EOL> def _get_standard ( self ) : <EOL> return getattr ( self , self . STANDARD_UNIT ) <EOL> def _set_standard ( self , value ) : <EOL> setattr ( self , self . STANDARD_UNIT , value ) <EOL> standard = property ( _get_standard , _set_standard ) <EOL> def __getattr__ ( self , name ) : <EOL> if name in self . UNITS : <EOL> return self . standard / self . UNITS [ name ] <EOL> else : <EOL> raise AttributeError ( '<STR_LIT>' % name ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( pretty_name ( self ) , self . _default_unit , <EOL> getattr ( self , self . _default_unit ) ) <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' % ( getattr ( self , self . _default_unit ) , self . _default_unit ) <EOL> def __eq__ ( self , other ) : <EOL> if isinstance ( other , self . __class__ ) : <EOL> return self . standard == other . standard <EOL> else : <EOL> return NotImplemented <EOL> def __lt__ ( self , other ) : <EOL> if isinstance ( other , self . __class__ ) : <EOL> return self . standard < other . standard <EOL> else : <EOL> return NotImplemented <EOL> def __add__ ( self , other ) : <EOL> if isinstance ( other , self . __class__ ) : <EOL> return self . __class__ ( default_unit = self . _default_unit , <EOL> ** { self . STANDARD_UNIT : ( self . standard + other . standard ) } ) <EOL> else : <EOL> raise TypeError ( '<STR_LIT>' % { "<STR_LIT:class>" : pretty_name ( self ) } ) <EOL> def __iadd__ ( self , other ) : <EOL> if isinstance ( other , self . __class__ ) : <EOL> self . standard += other . standard <EOL> return self <EOL> else : <EOL> raise TypeError ( '<STR_LIT>' % { "<STR_LIT:class>" : pretty_name ( self ) } ) <EOL> def __sub__ ( self , other ) : <EOL> if isinstance ( other , self . __class__ ) : <EOL> return self . __class__ ( default_unit = self . _default_unit , <EOL> ** { self . STANDARD_UNIT : ( self . standard - other . standard ) } ) <EOL> else : <EOL> raise TypeError ( '<STR_LIT>' % { "<STR_LIT:class>" : pretty_name ( self ) } ) <EOL> def __isub__ ( self , other ) : <EOL> if isinstance ( other , self . __class__ ) : <EOL> self . standard -= other . standard <EOL> return self <EOL> else : <EOL> raise TypeError ( '<STR_LIT>' % { "<STR_LIT:class>" : pretty_name ( self ) } ) <EOL> def __mul__ ( self , other ) : <EOL> if isinstance ( other , NUMERIC_TYPES ) : <EOL> return self . __class__ ( default_unit = self . _default_unit , <EOL> ** { self . STANDARD_UNIT : ( self . standard * other ) } ) <EOL> else : <EOL> raise TypeError ( '<STR_LIT>' % { "<STR_LIT:class>" : pretty_name ( self ) } ) <EOL> def __imul__ ( self , other ) : <EOL> if isinstance ( other , NUMERIC_TYPES ) : <EOL> self . standard *= float ( other ) <EOL> return self <EOL> else : <EOL> raise TypeError ( '<STR_LIT>' % { "<STR_LIT:class>" : pretty_name ( self ) } ) <EOL> def __rmul__ ( self , other ) : <EOL> return self * other <EOL> def __truediv__ ( self , other ) : <EOL> if isinstance ( other , self . __class__ ) : <EOL> return self . standard / other . standard <EOL> if isinstance ( other , NUMERIC_TYPES ) : <EOL> return self . __class__ ( default_unit = self . _default_unit , <EOL> ** { self . STANDARD_UNIT : ( self . standard / other ) } ) <EOL> else : <EOL> raise TypeError ( '<STR_LIT>' % { "<STR_LIT:class>" : pretty_name ( self ) } ) <EOL> def __div__ ( self , other ) : <EOL> return type ( self ) . __truediv__ ( self , other ) <EOL> def __itruediv__ ( self , other ) : <EOL> if isinstance ( other , NUMERIC_TYPES ) : <EOL> self . standard /= float ( other ) <EOL> return self <EOL> else : <EOL> raise TypeError ( '<STR_LIT>' % { "<STR_LIT:class>" : pretty_name ( self ) } ) <EOL> def __idiv__ ( self , other ) : <EOL> return type ( self ) . __itruediv__ ( self , other ) <EOL> def __bool__ ( self ) : <EOL> return bool ( self . standard ) <EOL> def __nonzero__ ( self ) : <EOL> return type ( self ) . __bool__ ( self ) <EOL> def default_units ( self , kwargs ) : <EOL> """<STR_LIT>""" <EOL> val = <NUM_LIT:0.0> <EOL> default_unit = self . STANDARD_UNIT <EOL> for unit , value in six . iteritems ( kwargs ) : <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> STANDARD_UNIT = "<STR_LIT:m>" <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 __mul__ ( self , other ) : <EOL> if isinstance ( other , self . __class__ ) : <EOL> return Area ( default_unit = AREA_PREFIX + self . _default_unit , <EOL> ** { AREA_PREFIX + self . STANDARD_UNIT : ( self . standard * other . standard ) } ) <EOL> elif isinstance ( other , NUMERIC_TYPES ) : <EOL> return self . __class__ ( default_unit = self . _default_unit , <EOL> ** { self . STANDARD_UNIT : ( self . standard * other ) } ) <EOL> else : <EOL> raise TypeError ( '<STR_LIT>' % { <EOL> "<STR_LIT>" : pretty_name ( self . __class__ ) , <EOL> } ) <EOL> class Area ( MeasureBase ) : <EOL> STANDARD_UNIT = AREA_PREFIX + Distance . STANDARD_UNIT <EOL> UNITS = dict ( [ ( '<STR_LIT>' % ( AREA_PREFIX , k ) , v ** <NUM_LIT:2> ) for k , v in Distance . UNITS . items ( ) ] ) <EOL> ALIAS = dict ( [ ( k , '<STR_LIT>' % ( AREA_PREFIX , v ) ) for k , v in Distance . ALIAS . items ( ) ] ) <EOL> LALIAS = dict ( [ ( k . lower ( ) , v ) for k , v in ALIAS . items ( ) ] ) <EOL> def __truediv__ ( self , other ) : <EOL> if isinstance ( other , NUMERIC_TYPES ) : <EOL> return self . __class__ ( default_unit = self . _default_unit , <EOL> ** { self . STANDARD_UNIT : ( self . standard / other ) } ) <EOL> else : <EOL> raise TypeError ( '<STR_LIT>' % { "<STR_LIT:class>" : pretty_name ( self ) } ) <EOL> def __div__ ( self , other ) : <EOL> return type ( self ) . __truediv__ ( self , other ) <EOL> D = Distance <EOL> A = Area </s>
