<s> __all__ = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> from _abcoll import * <EOL> import _abcoll <EOL> __all__ += _abcoll . __all__ <EOL> from _collections import deque , defaultdict <EOL> from operator import itemgetter as _itemgetter <EOL> from keyword import iskeyword as _iskeyword <EOL> import sys as _sys <EOL> import heapq as _heapq <EOL> from itertools import repeat as _repeat , chain as _chain , starmap as _starmap <EOL> try : <EOL> from thread import get_ident as _get_ident <EOL> except ImportError : <EOL> from dummy_thread import get_ident as _get_ident <EOL> class OrderedDict ( dict ) : <EOL> '<STR_LIT>' <EOL> def __init__ ( self , * args , ** kwds ) : <EOL> '''<STR_LIT>''' <EOL> if len ( args ) > <NUM_LIT:1> : <EOL> raise TypeError ( '<STR_LIT>' % len ( args ) ) <EOL> try : <EOL> self . __root <EOL> except AttributeError : <EOL> self . __root = root = [ ] <EOL> root [ : ] = [ root , root , None ] <EOL> self . __map = { } <EOL> self . __update ( * args , ** kwds ) <EOL> def __setitem__ ( self , key , value , PREV = <NUM_LIT:0> , NEXT = <NUM_LIT:1> , dict_setitem = dict . __setitem__ ) : <EOL> '<STR_LIT>' <EOL> if key not in self : <EOL> root = self . __root <EOL> last = root [ PREV ] <EOL> last [ NEXT ] = root [ PREV ] = self . __map [ key ] = [ last , root , key ] <EOL> dict_setitem ( self , key , value ) <EOL> def __delitem__ ( self , key , PREV = <NUM_LIT:0> , NEXT = <NUM_LIT:1> , dict_delitem = dict . __delitem__ ) : <EOL> '<STR_LIT>' <EOL> dict_delitem ( self , key ) <EOL> link_prev , link_next , key = self . __map . pop ( key ) <EOL> link_prev [ NEXT ] = link_next <EOL> link_next [ PREV ] = link_prev <EOL> def __iter__ ( self ) : <EOL> '<STR_LIT>' <EOL> NEXT , KEY = <NUM_LIT:1> , <NUM_LIT:2> <EOL> root = self . __root <EOL> curr = root [ NEXT ] <EOL> while curr is not root : <EOL> yield curr [ KEY ] <EOL> curr = curr [ NEXT ] <EOL> def __reversed__ ( self ) : <EOL> '<STR_LIT>' <EOL> PREV , KEY = <NUM_LIT:0> , <NUM_LIT:2> <EOL> root = self . __root <EOL> curr = root [ PREV ] <EOL> while curr is not root : <EOL> yield curr [ KEY ] <EOL> curr = curr [ PREV ] <EOL> def clear ( self ) : <EOL> '<STR_LIT>' <EOL> for node in self . __map . itervalues ( ) : <EOL> del node [ : ] <EOL> root = self . __root <EOL> root [ : ] = [ root , root , None ] <EOL> self . __map . clear ( ) <EOL> dict . clear ( self ) <EOL> def keys ( self ) : <EOL> '<STR_LIT>' <EOL> return list ( self ) <EOL> def values ( self ) : <EOL> '<STR_LIT>' <EOL> return [ self [ key ] for key in self ] <EOL> def items ( self ) : <EOL> '<STR_LIT>' <EOL> return [ ( key , self [ key ] ) for key in self ] <EOL> def iterkeys ( self ) : <EOL> '<STR_LIT>' <EOL> return iter ( self ) <EOL> def itervalues ( self ) : <EOL> '<STR_LIT>' <EOL> for k in self : <EOL> yield self [ k ] <EOL> def iteritems ( self ) : <EOL> '<STR_LIT>' <EOL> for k in self : <EOL> yield ( k , self [ k ] ) <EOL> update = MutableMapping . update <EOL> __update = update <EOL> __marker = object ( ) <EOL> def pop ( self , key , default = __marker ) : <EOL> '''<STR_LIT>''' <EOL> if key in self : <EOL> result = self [ key ] <EOL> del self [ key ] <EOL> return result <EOL> if default is self . __marker : <EOL> raise KeyError ( key ) <EOL> return default <EOL> def setdefault ( self , key , default = None ) : <EOL> '<STR_LIT>' <EOL> if key in self : <EOL> return self [ key ] <EOL> self [ key ] = default <EOL> return default <EOL> def popitem ( self , last = True ) : <EOL> '''<STR_LIT>''' <EOL> if not self : <EOL> raise KeyError ( '<STR_LIT>' ) <EOL> key = next ( reversed ( self ) if last else iter ( self ) ) <EOL> value = self . pop ( key ) <EOL> return key , value <EOL> def __repr__ ( self , _repr_running = { } ) : <EOL> '<STR_LIT>' <EOL> call_key = id ( self ) , _get_ident ( ) <EOL> if call_key in _repr_running : <EOL> return '<STR_LIT>' <EOL> _repr_running [ call_key ] = <NUM_LIT:1> <EOL> try : <EOL> if not self : <EOL> return '<STR_LIT>' % ( self . __class__ . __name__ , ) <EOL> return '<STR_LIT>' % ( self . __class__ . __name__ , self . items ( ) ) <EOL> finally : <EOL> del _repr_running [ call_key ] <EOL> def __reduce__ ( self ) : <EOL> '<STR_LIT>' <EOL> items = [ [ k , self [ k ] ] for k in self ] <EOL> inst_dict = vars ( self ) . copy ( ) <EOL> for k in vars ( OrderedDict ( ) ) : <EOL> inst_dict . pop ( k , None ) <EOL> if inst_dict : <EOL> return ( self . __class__ , ( items , ) , inst_dict ) <EOL> return self . __class__ , ( items , ) <EOL> def copy ( self ) : <EOL> '<STR_LIT>' <EOL> return self . __class__ ( self ) <EOL> @ classmethod <EOL> def fromkeys ( cls , iterable , value = None ) : <EOL> '''<STR_LIT>''' <EOL> self = cls ( ) <EOL> for key in iterable : <EOL> self [ key ] = value <EOL> return self <EOL> def __eq__ ( self , other ) : <EOL> '''<STR_LIT>''' <EOL> if isinstance ( other , OrderedDict ) : <EOL> return len ( self ) == len ( other ) and self . items ( ) == other . items ( ) <EOL> return dict . __eq__ ( self , other ) <EOL> def __ne__ ( self , other ) : <EOL> '<STR_LIT>' <EOL> return not self == other <EOL> def viewkeys ( self ) : <EOL> "<STR_LIT>" <EOL> return KeysView ( self ) <EOL> def viewvalues ( self ) : <EOL> "<STR_LIT>" <EOL> return ValuesView ( self ) <EOL> def viewitems ( self ) : <EOL> "<STR_LIT>" <EOL> return ItemsView ( self ) <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 all ( c . isalnum ( ) or c == '<STR_LIT:_>' for c in name ) or _iskeyword ( name ) <EOL> or not name or name [ <NUM_LIT:0> ] . isdigit ( ) 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 all ( 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 , i ) <EOL> if verbose : <EOL> print template <EOL> namespace = dict ( _itemgetter = _itemgetter , __name__ = '<STR_LIT>' % typename , <EOL> OrderedDict = OrderedDict , _property = property , _tuple = tuple ) <EOL> try : <EOL> exec template in namespace <EOL> except SyntaxError , e : <EOL> raise SyntaxError ( e . message + '<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> class Counter ( dict ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , iterable = None , ** kwds ) : <EOL> '''<STR_LIT>''' <EOL> super ( Counter , self ) . __init__ ( ) <EOL> self . update ( iterable , ** kwds ) <EOL> def __missing__ ( self , key ) : <EOL> '<STR_LIT>' <EOL> return <NUM_LIT:0> <EOL> def most_common ( self , n = None ) : <EOL> '''<STR_LIT>''' <EOL> if n is None : <EOL> return sorted ( self . iteritems ( ) , key = _itemgetter ( <NUM_LIT:1> ) , reverse = True ) <EOL> return _heapq . nlargest ( n , self . iteritems ( ) , key = _itemgetter ( <NUM_LIT:1> ) ) <EOL> def elements ( self ) : <EOL> '''<STR_LIT>''' <EOL> return _chain . from_iterable ( _starmap ( _repeat , self . iteritems ( ) ) ) <EOL> @ classmethod <EOL> def fromkeys ( cls , iterable , v = None ) : <EOL> raise NotImplementedError ( <EOL> '<STR_LIT>' ) <EOL> def update ( self , iterable = None , ** kwds ) : <EOL> '''<STR_LIT>''' <EOL> if iterable is not None : <EOL> if isinstance ( iterable , Mapping ) : <EOL> if self : <EOL> self_get = self . get <EOL> for elem , count in iterable . iteritems ( ) : <EOL> self [ elem ] = self_get ( elem , <NUM_LIT:0> ) + count <EOL> else : <EOL> super ( Counter , self ) . update ( iterable ) <EOL> else : <EOL> self_get = self . get <EOL> for elem in iterable : <EOL> self [ elem ] = self_get ( elem , <NUM_LIT:0> ) + <NUM_LIT:1> <EOL> if kwds : <EOL> self . update ( kwds ) <EOL> def subtract ( self , iterable = None , ** kwds ) : <EOL> '''<STR_LIT>''' <EOL> if iterable is not None : <EOL> self_get = self . get <EOL> if isinstance ( iterable , Mapping ) : <EOL> for elem , count in iterable . items ( ) : <EOL> self [ elem ] = self_get ( elem , <NUM_LIT:0> ) - count <EOL> else : <EOL> for elem in iterable : <EOL> self [ elem ] = self_get ( elem , <NUM_LIT:0> ) - <NUM_LIT:1> <EOL> if kwds : <EOL> self . subtract ( kwds ) <EOL> def copy ( self ) : <EOL> '<STR_LIT>' <EOL> return self . __class__ ( self ) <EOL> def __reduce__ ( self ) : <EOL> return self . __class__ , ( dict ( self ) , ) <EOL> def __delitem__ ( self , elem ) : <EOL> '<STR_LIT>' <EOL> if elem in self : <EOL> super ( Counter , self ) . __delitem__ ( elem ) <EOL> def __repr__ ( self ) : <EOL> if not self : <EOL> return '<STR_LIT>' % self . __class__ . __name__ <EOL> items = '<STR_LIT:U+002CU+0020>' . join ( map ( '<STR_LIT>' . __mod__ , self . most_common ( ) ) ) <EOL> return '<STR_LIT>' % ( self . __class__ . __name__ , items ) <EOL> def __add__ ( self , other ) : <EOL> '''<STR_LIT>''' <EOL> if not isinstance ( other , Counter ) : <EOL> return NotImplemented <EOL> result = Counter ( ) <EOL> for elem , count in self . items ( ) : <EOL> newcount = count + other [ elem ] <EOL> if newcount > <NUM_LIT:0> : <EOL> result [ elem ] = newcount <EOL> for elem , count in other . items ( ) : <EOL> if elem not in self and count > <NUM_LIT:0> : <EOL> result [ elem ] = count <EOL> return result <EOL> def __sub__ ( self , other ) : <EOL> '''<STR_LIT>''' <EOL> if not isinstance ( other , Counter ) : <EOL> return NotImplemented <EOL> result = Counter ( ) <EOL> for elem , count in self . items ( ) : <EOL> newcount = count - other [ elem ] <EOL> if newcount > <NUM_LIT:0> : <EOL> result [ elem ] = newcount <EOL> for elem , count in other . items ( ) : <EOL> if elem not in self and count < <NUM_LIT:0> : <EOL> result [ elem ] = <NUM_LIT:0> - count <EOL> return result <EOL> def __or__ ( self , other ) : <EOL> '''<STR_LIT>''' <EOL> if not isinstance ( other , Counter ) : <EOL> return NotImplemented <EOL> result = Counter ( ) <EOL> for elem , count in self . items ( ) : <EOL> other_count = other [ elem ] <EOL> newcount = other_count if count < other_count else count <EOL> if newcount > <NUM_LIT:0> : <EOL> result [ elem ] = newcount <EOL> for elem , count in other . items ( ) : <EOL> if elem not in self and count > <NUM_LIT:0> : <EOL> result [ elem ] = count <EOL> return result <EOL> def __and__ ( self , other ) : <EOL> '''<STR_LIT>''' <EOL> if not isinstance ( other , Counter ) : <EOL> return NotImplemented <EOL> result = Counter ( ) <EOL> for elem , count in self . items ( ) : <EOL> other_count = other [ elem ] <EOL> newcount = count if count < other_count else other_count <EOL> if newcount > <NUM_LIT:0> : <EOL> result [ elem ] = newcount <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 ) ) <EOL> class Point ( namedtuple ( '<STR_LIT>' , '<STR_LIT>' ) ) : <EOL> __slots__ = ( ) <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> / <NUM_LIT> ) : <EOL> print p <EOL> class Point ( namedtuple ( '<STR_LIT>' , '<STR_LIT>' ) ) : <EOL> '<STR_LIT>' <EOL> __slots__ = ( ) <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> Point3D = namedtuple ( '<STR_LIT>' , Point . _fields + ( '<STR_LIT:z>' , ) ) <EOL> print Point3D . __doc__ <EOL> import doctest <EOL> TestResults = namedtuple ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> print TestResults ( * doctest . testmod ( ) ) </s>
