<s> """<STR_LIT>""" <EOL> from collections import * <EOL> import sys <EOL> try : <EOL> defaultdict <EOL> except NameError : <EOL> class defaultdict ( dict ) : <EOL> """<STR_LIT>""" <EOL> def __new__ ( cls , default_factory = None ) : <EOL> return dict . __new__ ( cls ) <EOL> def __init__ ( self , default_factory ) : <EOL> self . default_factory = default_factory <EOL> def __missing__ ( self , key ) : <EOL> try : <EOL> return self . default_factory ( ) <EOL> except : <EOL> raise KeyError ( "<STR_LIT>" % key ) <EOL> def __getitem__ ( self , key ) : <EOL> if not dict . __contains__ ( self , key ) : <EOL> dict . __setitem__ ( self , key , self . __missing__ ( key ) ) <EOL> return dict . __getitem__ ( self , key ) <EOL> try : <EOL> namedtuple <EOL> except NameError : <EOL> from operator import itemgetter as _itemgetter <EOL> from keyword import iskeyword as _iskeyword <EOL> import sys as _sys <EOL> def namedtuple ( 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 = tuple ( map ( str , field_names ) ) <EOL> if rename : <EOL> names = list ( field_names ) <EOL> seen = set ( ) <EOL> for i , name in enumerate ( names ) : <EOL> if ( not min ( c . isalnum ( ) or c == '<STR_LIT:_>' for c in name ) <EOL> or _iskeyword ( name ) <EOL> or not name or name [ <NUM_LIT:0> ] . isdigit ( ) <EOL> or name . startswith ( '<STR_LIT:_>' ) <EOL> or name in seen ) : <EOL> names [ i ] = '<STR_LIT>' % i <EOL> seen . add ( name ) <EOL> field_names = tuple ( names ) <EOL> for name in ( typename , ) + field_names : <EOL> if not min ( c . isalnum ( ) or c == '<STR_LIT:_>' for c in name ) : <EOL> raise ValueError ( '<STR_LIT>' % name ) <EOL> if _iskeyword ( name ) : <EOL> raise ValueError ( '<STR_LIT>' % name ) <EOL> if name [ <NUM_LIT:0> ] . isdigit ( ) : <EOL> raise ValueError ( '<STR_LIT>' % name ) <EOL> seen_names = set ( ) <EOL> for name in field_names : <EOL> if name . startswith ( '<STR_LIT:_>' ) and not rename : <EOL> raise ValueError ( '<STR_LIT>' % name ) <EOL> if name in seen_names : <EOL> raise ValueError ( '<STR_LIT>' % name ) <EOL> seen_names . add ( name ) <EOL> numfields = len ( field_names ) <EOL> argtxt = repr ( field_names ) . replace ( "<STR_LIT:'>" , "<STR_LIT>" ) [ <NUM_LIT:1> : - <NUM_LIT:1> ] <EOL> reprtxt = '<STR_LIT:U+002CU+0020>' . join ( '<STR_LIT>' % name for name in field_names ) <EOL> template = '''<STR_LIT>''' % locals ( ) <EOL> for i , name in enumerate ( field_names ) : <EOL> template += '<STR_LIT>' % ( name , i ) <EOL> if verbose : <EOL> print ( template ) <EOL> namespace = dict ( _itemgetter = _itemgetter , __name__ = '<STR_LIT>' % typename , <EOL> _property = property , _tuple = tuple ) <EOL> try : <EOL> exec ( template ) in namespace <EOL> except SyntaxError : <EOL> e = sys . exc_info ( ) [ <NUM_LIT:1> ] <EOL> raise SyntaxError ( str ( e ) + '<STR_LIT>' + template ) <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> from cPickle import loads , dumps <EOL> Point = namedtuple ( '<STR_LIT>' , '<STR_LIT>' , True ) <EOL> p = Point ( x = <NUM_LIT:10> , y = <NUM_LIT:20> ) <EOL> assert p == loads ( dumps ( p , - <NUM_LIT:1> ) ) <EOL> class Point ( namedtuple ( '<STR_LIT>' , '<STR_LIT>' ) ) : <EOL> @ property <EOL> def hypot ( self ) : <EOL> return ( self . x ** <NUM_LIT:2> + self . y ** <NUM_LIT:2> ) ** <NUM_LIT:0.5> <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' % ( self . x , self . y , self . hypot ) <EOL> for p in Point ( <NUM_LIT:3> , <NUM_LIT:4> ) , Point ( <NUM_LIT> , <NUM_LIT:5> ) , Point ( <NUM_LIT> / <NUM_LIT:7> , <NUM_LIT:6> ) : <EOL> print ( p ) <EOL> class Point ( namedtuple ( '<STR_LIT>' , '<STR_LIT>' ) ) : <EOL> '<STR_LIT>' <EOL> _make = classmethod ( tuple . __new__ ) <EOL> def _replace ( self , _map = map , ** kwds ) : <EOL> return self . _make ( _map ( kwds . get , ( '<STR_LIT:x>' , '<STR_LIT:y>' ) , self ) ) <EOL> print ( Point ( <NUM_LIT:11> , <NUM_LIT> ) . _replace ( x = <NUM_LIT:100> ) ) <EOL> import doctest <EOL> TestResults = namedtuple ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> print ( TestResults ( * doctest . testmod ( ) ) ) </s>
