<s> """<STR_LIT>""" <EOL> import warnings <EOL> from . import Q_ <EOL> from . log import LOGGER as _LOG <EOL> from stringparser import Parser <EOL> class DimensionalityWarning ( Warning ) : <EOL> pass <EOL> def _do_nothing ( value ) : <EOL> return value <EOL> def _getitem ( a , b ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return a [ b ] <EOL> except KeyError : <EOL> return a [ type ( b ) ] <EOL> getitem = _getitem <EOL> def convert_to ( units , on_dimensionless = '<STR_LIT>' , on_incompatible = '<STR_LIT>' , <EOL> return_float = False ) : <EOL> """<STR_LIT>""" <EOL> if on_dimensionless not in ( '<STR_LIT:ignore>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" . format ( on_dimensionless ) ) <EOL> if on_incompatible not in ( '<STR_LIT:ignore>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" . format ( on_dimensionless ) ) <EOL> if isinstance ( units , str ) : <EOL> units = Q_ ( <NUM_LIT:1> , units ) <EOL> elif not isinstance ( units , Q_ ) : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if return_float : <EOL> def _inner ( value ) : <EOL> if isinstance ( value , Q_ ) : <EOL> try : <EOL> return value . to ( units ) . magnitude <EOL> except ValueError as e : <EOL> if on_incompatible == '<STR_LIT>' : <EOL> raise ValueError ( e ) <EOL> elif on_incompatible == '<STR_LIT>' : <EOL> msg = '<STR_LIT>' . format ( value , units ) <EOL> warnings . warn ( msg , DimensionalityWarning ) <EOL> _LOG . warn ( msg ) <EOL> return value . magnitude <EOL> else : <EOL> if not units . dimensionless : <EOL> if on_dimensionless == '<STR_LIT>' : <EOL> raise ValueError ( '<STR_LIT>' . format ( value , units ) ) <EOL> elif on_dimensionless == '<STR_LIT>' : <EOL> msg = '<STR_LIT>' . format ( value , units ) <EOL> warnings . warn ( msg , DimensionalityWarning ) <EOL> _LOG . warn ( msg ) <EOL> return float ( value ) <EOL> return _inner <EOL> else : <EOL> def _inner ( value ) : <EOL> if isinstance ( value , Q_ ) : <EOL> try : <EOL> return value . to ( units ) <EOL> except ValueError as e : <EOL> if on_incompatible == '<STR_LIT>' : <EOL> raise ValueError ( e ) <EOL> elif on_incompatible == '<STR_LIT>' : <EOL> msg = '<STR_LIT>' . format ( value , units ) <EOL> warnings . warn ( msg , DimensionalityWarning ) <EOL> _LOG . warn ( msg ) <EOL> return float ( value . magnitude ) * units <EOL> else : <EOL> if not units . dimensionless : <EOL> if on_dimensionless == '<STR_LIT>' : <EOL> raise ValueError ( '<STR_LIT>' . format ( value , units ) ) <EOL> elif on_dimensionless == '<STR_LIT>' : <EOL> msg = '<STR_LIT>' . format ( value , units ) <EOL> warnings . warn ( msg , DimensionalityWarning ) <EOL> _LOG . warn ( msg ) <EOL> return float ( value ) * units <EOL> return _inner <EOL> class Processor ( object ) : <EOL> """<STR_LIT>""" <EOL> def __new__ ( cls , processors ) : <EOL> if isinstance ( processors , ( tuple , list ) ) : <EOL> if len ( processors ) > <NUM_LIT:1> : <EOL> inst = super ( ) . __new__ ( cls ) <EOL> inst . processors = tuple ( cls . _to_callable ( processor ) <EOL> for processor in processors ) <EOL> return inst <EOL> else : <EOL> return cls . _to_callable ( processors [ <NUM_LIT:0> ] ) <EOL> else : <EOL> return cls . _to_callable ( processors ) <EOL> def __call__ ( self , values ) : <EOL> return tuple ( processor ( value ) <EOL> for processor , value in zip ( self . processors , values ) ) <EOL> @ classmethod <EOL> def _to_callable ( cls , obj ) : <EOL> if callable ( obj ) : <EOL> return obj <EOL> if obj is None : <EOL> return _do_nothing <EOL> return cls . to_callable ( obj ) <EOL> @ classmethod <EOL> def to_callable ( cls , obj ) : <EOL> raise TypeError ( '<STR_LIT>' . format ( obj ) ) <EOL> def __len__ ( self ) : <EOL> if isinstance ( self . processors , tuple ) : <EOL> return len ( self . processors ) <EOL> return <NUM_LIT:1> <EOL> class FromQuantityProcessor ( Processor ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def to_callable ( cls , obj ) : <EOL> if isinstance ( obj , ( str , Q_ ) ) : <EOL> return convert_to ( obj , return_float = True ) <EOL> raise TypeError ( '<STR_LIT>' <EOL> '<STR_LIT>' . format ( obj ) ) <EOL> class ToQuantityProcessor ( Processor ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def to_callable ( cls , obj ) : <EOL> if isinstance ( obj , ( str , Q_ ) ) : <EOL> return convert_to ( obj , on_dimensionless = '<STR_LIT:ignore>' ) <EOL> raise TypeError ( '<STR_LIT>' <EOL> '<STR_LIT>' . format ( obj ) ) <EOL> class ParseProcessor ( Processor ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def to_callable ( cls , obj ) : <EOL> if isinstance ( obj , str ) : <EOL> return Parser ( obj ) <EOL> raise TypeError ( '<STR_LIT>' <EOL> '<STR_LIT>' . format ( obj ) ) <EOL> class MapProcessor ( Processor ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def to_callable ( cls , obj ) : <EOL> if isinstance ( obj , dict ) : <EOL> return get_mapping ( obj ) <EOL> if isinstance ( obj , set ) : <EOL> return check_membership ( obj ) <EOL> raise TypeError ( '<STR_LIT>' <EOL> '<STR_LIT>' . format ( obj ) ) <EOL> class ReverseMapProcessor ( Processor ) : <EOL> """<STR_LIT>""" <EOL> __reversed_cache = { } <EOL> @ classmethod <EOL> def to_callable ( cls , obj ) : <EOL> if isinstance ( obj , dict ) : <EOL> obj = cls . __reversed_cache . setdefault ( id ( obj ) , <EOL> { value : key for key , value <EOL> in obj . items ( ) } ) <EOL> return get_mapping ( obj ) <EOL> if isinstance ( obj , set ) : <EOL> return check_membership ( obj ) <EOL> raise TypeError ( '<STR_LIT>' <EOL> '<STR_LIT>' . format ( obj ) ) <EOL> class RangeProcessor ( Processor ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def to_callable ( cls , obj ) : <EOL> if not isinstance ( obj , ( list , tuple ) ) : <EOL> raise TypeError ( '<STR_LIT>' <EOL> '<STR_LIT>' . format ( obj ) ) <EOL> if not len ( obj ) in ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ) : <EOL> raise TypeError ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( len ( obj ) ) ) <EOL> if len ( obj ) == <NUM_LIT:1> : <EOL> return check_range_and_coerce_step ( <NUM_LIT:0> , * obj ) <EOL> return check_range_and_coerce_step ( * obj ) <EOL> def check_range_and_coerce_step ( low , high , step = None ) : <EOL> """<STR_LIT>""" <EOL> def _inner ( value ) : <EOL> if not ( low <= value <= high ) : <EOL> raise ValueError ( '<STR_LIT>' . format ( value , low , high ) ) <EOL> if step : <EOL> value = round ( ( value - low ) / step ) * step + low <EOL> return value <EOL> return _inner <EOL> def check_membership ( container ) : <EOL> """<STR_LIT>""" <EOL> def _inner ( value ) : <EOL> if value not in container : <EOL> raise ValueError ( '<STR_LIT>' . format ( value , container ) ) <EOL> return value <EOL> return _inner <EOL> def get_mapping ( container ) : <EOL> """<STR_LIT>""" <EOL> def _inner ( key ) : <EOL> if key not in container : <EOL> raise ValueError ( "<STR_LIT>" . format ( key , tuple ( container . keys ( ) ) ) ) <EOL> return container [ key ] <EOL> return _inner </s>
