<s> """<STR_LIT>""" <EOL> import sys as _sys <EOL> from operator import itemgetter as _itemgetter <EOL> from keyword import iskeyword as _iskeyword <EOL> from collections import OrderedDict <EOL> class tagtuple ( tuple ) : <EOL> """<STR_LIT>""" <EOL> __slots__ = ( ) <EOL> def __new__ ( cls , * args ) : <EOL> """<STR_LIT>""" <EOL> return super ( tagtuple , cls ) . __new__ ( cls , args ) <EOL> def __repr__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return type ( self ) . __name__ + super ( tagtuple , self ) . __repr__ ( ) <EOL> def __getnewargs__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return tuple ( self ) <EOL> def __eq__ ( self , other ) : <EOL> return type ( self ) is type ( other ) and super ( tagtuple , self ) . __eq__ ( other ) <EOL> def __ne__ ( self , other ) : <EOL> return not self . __eq__ ( other ) <EOL> def __getslice__ ( self , i , j ) : <EOL> return type ( self ) ( * super ( tagtuple , self ) . __getslice__ ( i , j ) ) <EOL> __add__ = property ( ) <EOL> __contains__ = property ( ) <EOL> __mul__ = property ( ) <EOL> __rmul__ = property ( ) <EOL> count = property ( ) <EOL> index = property ( ) <EOL> _class_template = '''<STR_LIT>''' <EOL> _repr_template = '<STR_LIT>' <EOL> _field_template = '''<STR_LIT>''' <EOL> def rectuple ( typename , field_names , verbose = False , rename = False ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( field_names , basestring ) : <EOL> field_names = field_names . replace ( '<STR_LIT:U+002C>' , '<STR_LIT:U+0020>' ) . split ( ) <EOL> field_names = map ( str , field_names ) <EOL> if rename : <EOL> seen = set ( ) <EOL> for index , name in enumerate ( field_names ) : <EOL> if ( not all ( c . isalnum ( ) or c == '<STR_LIT:_>' for c in name ) <EOL> or _iskeyword ( name ) <EOL> or not name <EOL> or name [ <NUM_LIT:0> ] . isdigit ( ) <EOL> or name . startswith ( '<STR_LIT:_>' ) <EOL> or name in seen ) : <EOL> field_names [ index ] = '<STR_LIT>' % index <EOL> seen . add ( name ) <EOL> for name in [ typename ] + field_names : <EOL> if not all ( c . isalnum ( ) or c == '<STR_LIT:_>' for c in name ) : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' % name ) <EOL> if _iskeyword ( name ) : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' % name ) <EOL> if name [ <NUM_LIT:0> ] . isdigit ( ) : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' % name ) <EOL> seen = set ( ) <EOL> for name in field_names : <EOL> if name . startswith ( '<STR_LIT:_>' ) and not rename : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' % name ) <EOL> if name in seen : <EOL> raise ValueError ( '<STR_LIT>' % name ) <EOL> seen . add ( name ) <EOL> class_definition = _class_template . format ( <EOL> typename = typename , <EOL> field_names = tuple ( field_names ) , <EOL> num_fields = len ( field_names ) , <EOL> arg_list = repr ( tuple ( field_names ) ) . replace ( "<STR_LIT:'>" , "<STR_LIT>" ) [ <NUM_LIT:1> : - <NUM_LIT:1> ] , <EOL> repr_fmt = '<STR_LIT:U+002CU+0020>' . join ( _repr_template . format ( name = name ) <EOL> for name in field_names ) , <EOL> field_defs = '<STR_LIT:\n>' . join ( _field_template . format ( index = index , name = name ) <EOL> for index , name in enumerate ( field_names ) ) <EOL> ) <EOL> if verbose : <EOL> print class_definition <EOL> namespace = dict ( _itemgetter = _itemgetter , __name__ = '<STR_LIT>' % typename , <EOL> OrderedDict = OrderedDict , _property = property , _tuple = tuple ) <EOL> try : <EOL> exec class_definition in namespace <EOL> except SyntaxError as e : <EOL> raise SyntaxError ( e . message + '<STR_LIT>' + class_definition ) <EOL> result = namespace [ typename ] <EOL> try : <EOL> result . __module__ = _sys . _getframe ( <NUM_LIT:1> ) . f_globals . get ( '<STR_LIT>' , '<STR_LIT:__main__>' ) <EOL> except ( AttributeError , ValueError ) : <EOL> pass <EOL> return result <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> import pickle <EOL> from itertools import chain , product <EOL> print "<STR_LIT>" <EOL> print <EOL> class A ( tagtuple ) : <EOL> __slots__ = ( ) <EOL> class B ( tagtuple ) : <EOL> __slots__ = ( ) <EOL> a = A ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ) <EOL> b = B ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ) <EOL> t = ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ) <EOL> print "<STR_LIT>" , a <EOL> print "<STR_LIT>" , b <EOL> print "<STR_LIT>" , t <EOL> print <EOL> print "<STR_LIT>" , a == b <EOL> print "<STR_LIT>" , a != b <EOL> print "<STR_LIT>" , hash ( a ) == hash ( b ) <EOL> print "<STR_LIT>" , a <= b <EOL> print "<STR_LIT>" , b <= a <EOL> print <EOL> print "<STR_LIT>" , a == t <EOL> print "<STR_LIT>" , a != t <EOL> print "<STR_LIT>" , hash ( a ) == hash ( t ) <EOL> print "<STR_LIT>" , a <= t <EOL> print "<STR_LIT>" , t <= a <EOL> print <EOL> d = { } <EOL> d [ a ] = <NUM_LIT:1> <EOL> d [ b ] = <NUM_LIT:2> <EOL> d [ t ] = <NUM_LIT:3> <EOL> print "<STR_LIT>" , d <EOL> s = set ( ) <EOL> s . add ( a ) <EOL> s . add ( b ) <EOL> s . add ( t ) <EOL> print "<STR_LIT>" , s <EOL> print <EOL> print "<STR_LIT>" , tuple ( x for x in a ) <EOL> print "<STR_LIT>" , list ( a ) <EOL> print "<STR_LIT>" , tuple ( a ) <EOL> print <EOL> a0 = pickle . loads ( pickle . dumps ( a , <NUM_LIT:0> ) ) <EOL> a1 = pickle . loads ( pickle . dumps ( a , <NUM_LIT:1> ) ) <EOL> a2 = pickle . loads ( pickle . dumps ( a , <NUM_LIT:2> ) ) <EOL> print "<STR_LIT>" , a0 <EOL> print "<STR_LIT>" , a1 <EOL> print "<STR_LIT>" , a2 <EOL> print "<STR_LIT>" , a0 == a , hash ( a0 ) == hash ( a ) <EOL> print "<STR_LIT>" , a1 == a , hash ( a1 ) == hash ( a ) <EOL> print "<STR_LIT>" , a2 == a , hash ( a2 ) == hash ( a ) <EOL> print <EOL> print "<STR_LIT>" , a [ : ] <EOL> print "<STR_LIT>" , a [ <NUM_LIT:1> : - <NUM_LIT:1> ] <EOL> print "<STR_LIT>" , a + a <EOL> print "<STR_LIT>" , a + b <EOL> print "<STR_LIT>" , ( <NUM_LIT:0> , ) + a <EOL> print "<STR_LIT>" , a + ( <NUM_LIT:0> , ) <EOL> print "<STR_LIT>" , <NUM_LIT:2> * a <EOL> print "<STR_LIT>" , a * <NUM_LIT:2> <EOL> print <EOL> print "<STR_LIT>" , A ( * chain ( ( x ** <NUM_LIT:2> for x in range ( <NUM_LIT:10> ) ) , a ) ) <EOL> print "<STR_LIT>" , A ( * product ( range ( <NUM_LIT:3> ) , repeat = <NUM_LIT:2> ) ) <EOL> print <EOL> print "<STR_LIT>" <EOL> print <EOL> A = rectuple ( '<STR_LIT:A>' , '<STR_LIT>' , verbose = True ) <EOL> B = rectuple ( '<STR_LIT:B>' , '<STR_LIT>' , verbose = True ) <EOL> a = A ( <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> b = B ( <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> t = ( <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> print "<STR_LIT>" , a <EOL> print "<STR_LIT>" , b <EOL> print "<STR_LIT>" , t <EOL> print <EOL> print "<STR_LIT>" , a == b <EOL> print "<STR_LIT>" , a != b <EOL> print "<STR_LIT>" , hash ( a ) == hash ( b ) <EOL> print "<STR_LIT>" , a <= b <EOL> print "<STR_LIT>" , b <= a <EOL> print <EOL> print "<STR_LIT>" , a == t <EOL> print "<STR_LIT>" , a != t <EOL> print "<STR_LIT>" , hash ( a ) == hash ( t ) <EOL> print "<STR_LIT>" , a <= t <EOL> print "<STR_LIT>" , t <= a <EOL> print <EOL> d = { } <EOL> d [ a ] = <NUM_LIT:1> <EOL> d [ b ] = <NUM_LIT:2> <EOL> d [ t ] = <NUM_LIT:3> <EOL> print "<STR_LIT>" , d <EOL> s = set ( ) <EOL> s . add ( a ) <EOL> s . add ( b ) <EOL> s . add ( t ) <EOL> print "<STR_LIT>" , s <EOL> print <EOL> print "<STR_LIT>" , tuple ( x for x in a ) <EOL> print "<STR_LIT>" , list ( a ) <EOL> print "<STR_LIT>" , tuple ( a ) <EOL> print <EOL> a0 = pickle . loads ( pickle . dumps ( a , <NUM_LIT:0> ) ) <EOL> a1 = pickle . loads ( pickle . dumps ( a , <NUM_LIT:1> ) ) <EOL> a2 = pickle . loads ( pickle . dumps ( a , <NUM_LIT:2> ) ) <EOL> print "<STR_LIT>" , a0 <EOL> print "<STR_LIT>" , a1 <EOL> print "<STR_LIT>" , a2 <EOL> print "<STR_LIT>" , a0 == a , hash ( a0 ) == hash ( a ) <EOL> print "<STR_LIT>" , a1 == a , hash ( a1 ) == hash ( a ) <EOL> print "<STR_LIT>" , a2 == a , hash ( a2 ) == hash ( a ) </s>
