<s> import copy <EOL> from collections import Iterable , Mapping , Sequence <EOL> from inspect import isclass <EOL> from . base import Object <EOL> from . compatibility import Compatibility <EOL> from . naming import Namable , frozendict <EOL> from . typing import Type , TypeCheck , TypeFactory , TypeMetaclass <EOL> class ListFactory ( TypeFactory ) : <EOL> PROVIDES = '<STR_LIT>' <EOL> @ staticmethod <EOL> def create ( type_dict , * type_parameters ) : <EOL> """<STR_LIT>""" <EOL> assert len ( type_parameters ) == <NUM_LIT:1> <EOL> klazz = TypeFactory . new ( type_dict , * type_parameters [ <NUM_LIT:0> ] ) <EOL> assert isclass ( klazz ) <EOL> assert issubclass ( klazz , Object ) <EOL> return TypeMetaclass ( '<STR_LIT>' % klazz . __name__ , ( ListContainer , ) , { '<STR_LIT>' : klazz } ) <EOL> class ListContainer ( Object , Namable , Type ) : <EOL> """<STR_LIT>""" <EOL> __slots__ = ( '<STR_LIT>' , ) <EOL> def __init__ ( self , vals ) : <EOL> self . _values = self . _coerce_values ( copy . copy ( vals ) ) <EOL> super ( ListContainer , self ) . __init__ ( ) <EOL> def get ( self ) : <EOL> return tuple ( v . get ( ) for v in self . _values ) <EOL> def dup ( self ) : <EOL> return self . __class__ ( self . _values ) <EOL> def __hash__ ( self ) : <EOL> return hash ( self . get ( ) ) <EOL> def __repr__ ( self ) : <EOL> si , _ = self . interpolate ( ) <EOL> return '<STR_LIT>' % ( self . __class__ . __name__ , <EOL> '<STR_LIT:U+002CU+0020>' . join ( str ( v ) if Compatibility . PY3 else unicode ( v ) for v in si . _values ) ) <EOL> def __iter__ ( self ) : <EOL> si , _ = self . interpolate ( ) <EOL> return iter ( si . _values ) <EOL> def __getitem__ ( self , index_or_slice ) : <EOL> si , _ = self . interpolate ( ) <EOL> return si . _values [ index_or_slice ] <EOL> def __contains__ ( self , item ) : <EOL> si , _ = self . interpolate ( ) <EOL> if isinstance ( item , self . TYPE ) : <EOL> return item in si . _values <EOL> else : <EOL> return item in si . get ( ) <EOL> def __eq__ ( self , other ) : <EOL> if not isinstance ( other , ListContainer ) : return False <EOL> if self . TYPE . serialize_type ( ) != other . TYPE . serialize_type ( ) : return False <EOL> si , _ = self . interpolate ( ) <EOL> oi , _ = other . interpolate ( ) <EOL> return si . _values == oi . _values <EOL> @ staticmethod <EOL> def isiterable ( values ) : <EOL> return isinstance ( values , Sequence ) and not isinstance ( values , Compatibility . stringy ) <EOL> def _coerce_values ( self , values ) : <EOL> if not ListContainer . isiterable ( values ) : <EOL> raise ValueError ( "<STR_LIT>" % repr ( values ) ) <EOL> def coerced ( value ) : <EOL> return value if isinstance ( value , self . TYPE ) else self . TYPE ( value ) <EOL> return tuple ( [ coerced ( v ) for v in values ] ) <EOL> def check ( self ) : <EOL> assert ListContainer . isiterable ( self . _values ) <EOL> for element in self . _values : <EOL> assert isinstance ( element , self . TYPE ) <EOL> typecheck = element . in_scope ( * self . scopes ( ) ) . check ( ) <EOL> if not typecheck . ok ( ) : <EOL> return TypeCheck . failure ( "<STR_LIT>" % ( self . __class__ . __name__ , <EOL> typecheck . message ( ) ) ) <EOL> return TypeCheck . success ( ) <EOL> def interpolate ( self ) : <EOL> unbound = set ( ) <EOL> interpolated = [ ] <EOL> for element in self . _values : <EOL> einterp , eunbound = element . in_scope ( * self . scopes ( ) ) . interpolate ( ) <EOL> interpolated . append ( einterp ) <EOL> unbound . update ( eunbound ) <EOL> return self . __class__ ( interpolated ) , list ( unbound ) <EOL> def find ( self , ref ) : <EOL> if not ref . is_index ( ) : <EOL> raise Namable . NamingError ( self , ref ) <EOL> try : <EOL> intvalue = int ( ref . action ( ) . value ) <EOL> except ValueError : <EOL> raise Namable . NamingError ( self , ref ) <EOL> if len ( self . _values ) <= intvalue : <EOL> raise Namable . NotFound ( self , ref ) <EOL> else : <EOL> namable = self . _values [ intvalue ] <EOL> if ref . rest ( ) . is_empty ( ) : <EOL> return namable . in_scope ( * self . scopes ( ) ) <EOL> else : <EOL> if not isinstance ( namable , Namable ) : <EOL> raise Namable . Unnamable ( namable ) <EOL> else : <EOL> return namable . in_scope ( * self . scopes ( ) ) . find ( ref . rest ( ) ) <EOL> @ classmethod <EOL> def type_factory ( cls ) : <EOL> return '<STR_LIT>' <EOL> @ classmethod <EOL> def type_parameters ( cls ) : <EOL> return ( cls . TYPE . serialize_type ( ) , ) <EOL> List = TypeFactory . wrapper ( ListFactory ) <EOL> class MapFactory ( TypeFactory ) : <EOL> PROVIDES = '<STR_LIT>' <EOL> @ staticmethod <EOL> def create ( type_dict , * type_parameters ) : <EOL> assert len ( type_parameters ) == <NUM_LIT:2> , '<STR_LIT>' % repr ( type_parameters ) <EOL> key_klazz , value_klazz = type_parameters <EOL> key_klazz , value_klazz = ( TypeFactory . new ( type_dict , * key_klazz ) , <EOL> TypeFactory . new ( type_dict , * value_klazz ) ) <EOL> assert isclass ( key_klazz ) and isclass ( value_klazz ) <EOL> assert issubclass ( key_klazz , Object ) and issubclass ( value_klazz , Object ) <EOL> return TypeMetaclass ( '<STR_LIT>' % ( key_klazz . __name__ , value_klazz . __name__ ) , ( MapContainer , ) , <EOL> { '<STR_LIT>' : key_klazz , '<STR_LIT>' : value_klazz } ) <EOL> class MapContainer ( Object , Namable , Type ) : <EOL> """<STR_LIT>""" <EOL> __slots__ = ( '<STR_LIT>' , ) <EOL> def __init__ ( self , * args ) : <EOL> """<STR_LIT>""" <EOL> if len ( args ) == <NUM_LIT:1> and isinstance ( args [ <NUM_LIT:0> ] , Mapping ) : <EOL> self . _map = self . _coerce_map ( copy . copy ( args [ <NUM_LIT:0> ] ) ) <EOL> elif all ( isinstance ( arg , Iterable ) and len ( arg ) == <NUM_LIT:2> for arg in args ) : <EOL> self . _map = self . _coerce_tuple ( args ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" % repr ( args ) ) <EOL> super ( MapContainer , self ) . __init__ ( ) <EOL> def get ( self ) : <EOL> return frozendict ( ( k . get ( ) , v . get ( ) ) for ( k , v ) in self . _map ) <EOL> def _coerce_wrapper ( self , key , value ) : <EOL> coerced_key = key if isinstance ( key , self . KEYTYPE ) else self . KEYTYPE ( key ) <EOL> coerced_value = value if isinstance ( value , self . VALUETYPE ) else self . VALUETYPE ( value ) <EOL> return ( coerced_key , coerced_value ) <EOL> def _coerce_map ( self , input_map ) : <EOL> return tuple ( self . _coerce_wrapper ( key , value ) for key , value in input_map . items ( ) ) <EOL> def _coerce_tuple ( self , input_tuple ) : <EOL> return tuple ( self . _coerce_wrapper ( key , value ) for key , value in input_tuple ) <EOL> def __hash__ ( self ) : <EOL> return hash ( self . get ( ) ) <EOL> def __iter__ ( self ) : <EOL> si , _ = self . interpolate ( ) <EOL> return ( t [ <NUM_LIT:0> ] for t in si . _map ) <EOL> def __getitem__ ( self , key ) : <EOL> if not isinstance ( key , self . KEYTYPE ) : <EOL> try : <EOL> key = self . KEYTYPE ( key ) <EOL> except ValueError : <EOL> raise KeyError ( "<STR_LIT>" % self . KEYTYPE . __name__ ) <EOL> si , _ = self . interpolate ( ) <EOL> for tup in si . _map : <EOL> if key == tup [ <NUM_LIT:0> ] : <EOL> return tup [ <NUM_LIT:1> ] <EOL> raise KeyError ( "<STR_LIT>" % key ) <EOL> def __contains__ ( self , item ) : <EOL> try : <EOL> self [ item ] <EOL> return True <EOL> except KeyError : <EOL> return False <EOL> def dup ( self ) : <EOL> return self . __class__ ( * self . _map ) <EOL> def __repr__ ( self ) : <EOL> si , _ = self . interpolate ( ) <EOL> return '<STR_LIT>' % ( self . __class__ . __name__ , <EOL> '<STR_LIT:U+002CU+0020>' . join ( '<STR_LIT>' % ( key , val ) for key , val in si . _map ) ) <EOL> def __eq__ ( self , other ) : <EOL> if not isinstance ( other , MapContainer ) : return False <EOL> if self . KEYTYPE . serialize_type ( ) != other . KEYTYPE . serialize_type ( ) : return False <EOL> if self . VALUETYPE . serialize_type ( ) != other . VALUETYPE . serialize_type ( ) : return False <EOL> si , _ = self . interpolate ( ) <EOL> oi , _ = other . interpolate ( ) <EOL> return si . _map == oi . _map <EOL> def check ( self ) : <EOL> assert isinstance ( self . _map , tuple ) <EOL> for key , value in self . _map : <EOL> assert isinstance ( key , self . KEYTYPE ) <EOL> assert isinstance ( value , self . VALUETYPE ) <EOL> keycheck = key . in_scope ( * self . scopes ( ) ) . check ( ) <EOL> valuecheck = value . in_scope ( * self . scopes ( ) ) . check ( ) <EOL> if not keycheck . ok ( ) : <EOL> return TypeCheck . failure ( "<STR_LIT>" % ( self . __class__ . __name__ , <EOL> key , keycheck . message ( ) ) ) <EOL> if not valuecheck . ok ( ) : <EOL> return TypeCheck . failure ( "<STR_LIT>" % ( self . __class__ . __name__ , <EOL> key , value , valuecheck . message ( ) ) ) <EOL> return TypeCheck . success ( ) <EOL> def interpolate ( self ) : <EOL> unbound = set ( ) <EOL> interpolated = [ ] <EOL> for key , value in self . _map : <EOL> kinterp , kunbound = key . in_scope ( * self . scopes ( ) ) . interpolate ( ) <EOL> vinterp , vunbound = value . in_scope ( * self . scopes ( ) ) . interpolate ( ) <EOL> unbound . update ( kunbound ) <EOL> unbound . update ( vunbound ) <EOL> interpolated . append ( ( kinterp , vinterp ) ) <EOL> return self . __class__ ( * interpolated ) , list ( unbound ) <EOL> def find ( self , ref ) : <EOL> if not ref . is_index ( ) : <EOL> raise Namable . NamingError ( self , ref ) <EOL> kvalue = self . KEYTYPE ( ref . action ( ) . value ) <EOL> for key , namable in self . _map : <EOL> if kvalue == key : <EOL> if ref . rest ( ) . is_empty ( ) : <EOL> return namable . in_scope ( * self . scopes ( ) ) <EOL> else : <EOL> if not isinstance ( namable , Namable ) : <EOL> raise Namable . Unnamable ( namable ) <EOL> else : <EOL> return namable . in_scope ( * self . scopes ( ) ) . find ( ref . rest ( ) ) <EOL> raise Namable . NotFound ( self , ref ) <EOL> @ classmethod <EOL> def type_factory ( cls ) : <EOL> return '<STR_LIT>' <EOL> @ classmethod <EOL> def type_parameters ( cls ) : <EOL> return ( cls . KEYTYPE . serialize_type ( ) , cls . VALUETYPE . serialize_type ( ) ) <EOL> Map = TypeFactory . wrapper ( MapFactory ) </s>
