<s> import sys <EOL> py3k = getattr ( sys , '<STR_LIT>' , False ) or sys . version_info >= ( <NUM_LIT:3> , <NUM_LIT:0> ) <EOL> py24 = sys . version_info >= ( <NUM_LIT:2> , <NUM_LIT:4> ) and sys . version_info < ( <NUM_LIT:2> , <NUM_LIT:5> ) <EOL> jython = sys . platform . startswith ( '<STR_LIT>' ) <EOL> win32 = sys . platform . startswith ( '<STR_LIT>' ) <EOL> if py3k : <EOL> from io import StringIO <EOL> else : <EOL> try : <EOL> from io import StringIO <EOL> except : <EOL> from io import StringIO <EOL> import codecs , re , weakref , os , time , operator <EOL> import collections <EOL> try : <EOL> import threading <EOL> import _thread <EOL> except ImportError : <EOL> import dummy_threading as threading <EOL> import _dummy_thread as thread <EOL> if win32 or jython : <EOL> time_func = time . clock <EOL> else : <EOL> time_func = time . time <EOL> def function_named ( fn , name ) : <EOL> """<STR_LIT>""" <EOL> fn . __name__ = name <EOL> return fn <EOL> try : <EOL> from functools import partial <EOL> except : <EOL> def partial ( func , * args , ** keywords ) : <EOL> def newfunc ( * fargs , ** fkeywords ) : <EOL> newkeywords = keywords . copy ( ) <EOL> newkeywords . update ( fkeywords ) <EOL> return func ( * ( args + fargs ) , ** newkeywords ) <EOL> return newfunc <EOL> if py24 : <EOL> def exception_name ( exc ) : <EOL> try : <EOL> return exc . __class__ . __name__ <EOL> except AttributeError : <EOL> return exc . __name__ <EOL> else : <EOL> def exception_name ( exc ) : <EOL> return exc . __class__ . __name__ <EOL> def verify_directory ( dir ) : <EOL> """<STR_LIT>""" <EOL> tries = <NUM_LIT:0> <EOL> while not os . path . exists ( dir ) : <EOL> try : <EOL> tries += <NUM_LIT:1> <EOL> os . makedirs ( dir , <NUM_LIT> ) <EOL> except : <EOL> if tries > <NUM_LIT:5> : <EOL> raise <EOL> def to_list ( x , default = None ) : <EOL> if x is None : <EOL> return default <EOL> if not isinstance ( x , ( list , tuple ) ) : <EOL> return [ x ] <EOL> else : <EOL> return x <EOL> class memoized_property ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , fget , doc = None ) : <EOL> self . fget = fget <EOL> self . __doc__ = doc or fget . __doc__ <EOL> self . __name__ = fget . __name__ <EOL> def __get__ ( self , obj , cls ) : <EOL> if obj is None : <EOL> return self <EOL> obj . __dict__ [ self . __name__ ] = result = self . fget ( obj ) <EOL> return result <EOL> class SetLikeDict ( dict ) : <EOL> """<STR_LIT>""" <EOL> def union ( self , other ) : <EOL> """<STR_LIT>""" <EOL> x = SetLikeDict ( ** self ) <EOL> x . update ( other ) <EOL> return x <EOL> class FastEncodingBuffer ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , encoding = None , errors = '<STR_LIT:strict>' , str = False ) : <EOL> self . data = collections . deque ( ) <EOL> self . encoding = encoding <EOL> if str : <EOL> self . delim = '<STR_LIT>' <EOL> else : <EOL> self . delim = '<STR_LIT>' <EOL> self . str = str <EOL> self . errors = errors <EOL> self . write = self . data . append <EOL> def truncate ( self ) : <EOL> self . data = collections . deque ( ) <EOL> self . write = self . data . append <EOL> def getvalue ( self ) : <EOL> if self . encoding : <EOL> return self . delim . join ( self . data ) . encode ( self . encoding , self . errors ) <EOL> else : <EOL> return self . delim . join ( self . data ) <EOL> class LRUCache ( dict ) : <EOL> """<STR_LIT>""" <EOL> class _Item ( object ) : <EOL> def __init__ ( self , key , value ) : <EOL> self . key = key <EOL> self . value = value <EOL> self . timestamp = time_func ( ) <EOL> def __repr__ ( self ) : <EOL> return repr ( self . value ) <EOL> def __init__ ( self , capacity , threshold = <NUM_LIT> ) : <EOL> self . capacity = capacity <EOL> self . threshold = threshold <EOL> def __getitem__ ( self , key ) : <EOL> item = dict . __getitem__ ( self , key ) <EOL> item . timestamp = time_func ( ) <EOL> return item . value <EOL> def values ( self ) : <EOL> return [ i . value for i in dict . values ( self ) ] <EOL> def setdefault ( self , key , value ) : <EOL> if key in self : <EOL> return self [ key ] <EOL> else : <EOL> self [ key ] = value <EOL> return value <EOL> def __setitem__ ( self , key , value ) : <EOL> item = dict . get ( self , key ) <EOL> if item is None : <EOL> item = self . _Item ( key , value ) <EOL> dict . __setitem__ ( self , key , item ) <EOL> else : <EOL> item . value = value <EOL> self . _manage_size ( ) <EOL> def _manage_size ( self ) : <EOL> while len ( self ) > self . capacity + self . capacity * self . threshold : <EOL> bytime = sorted ( dict . values ( self ) , <EOL> key = operator . attrgetter ( '<STR_LIT>' ) , reverse = True ) <EOL> for item in bytime [ self . capacity : ] : <EOL> try : <EOL> del self [ item . key ] <EOL> except KeyError : <EOL> break <EOL> _PYTHON_MAGIC_COMMENT_re = re . compile ( <EOL> r'<STR_LIT>' , <EOL> re . VERBOSE ) <EOL> def parse_encoding ( fp ) : <EOL> """<STR_LIT>""" <EOL> pos = fp . tell ( ) <EOL> fp . seek ( <NUM_LIT:0> ) <EOL> try : <EOL> line1 = fp . readline ( ) <EOL> has_bom = line1 . startswith ( codecs . BOM_UTF8 ) <EOL> if has_bom : <EOL> line1 = line1 [ len ( codecs . BOM_UTF8 ) : ] <EOL> m = _PYTHON_MAGIC_COMMENT_re . match ( line1 . decode ( '<STR_LIT:ascii>' , '<STR_LIT:ignore>' ) ) <EOL> if not m : <EOL> try : <EOL> import parser <EOL> parser . suite ( line1 . decode ( '<STR_LIT:ascii>' , '<STR_LIT:ignore>' ) ) <EOL> except ( ImportError , SyntaxError ) : <EOL> pass <EOL> else : <EOL> line2 = fp . readline ( ) <EOL> m = _PYTHON_MAGIC_COMMENT_re . match ( line2 . decode ( '<STR_LIT:ascii>' , '<STR_LIT:ignore>' ) ) <EOL> if has_bom : <EOL> if m : <EOL> raise SyntaxError ( "<STR_LIT>" "<STR_LIT>" ) <EOL> return '<STR_LIT>' <EOL> elif m : <EOL> return m . group ( <NUM_LIT:1> ) <EOL> else : <EOL> return None <EOL> finally : <EOL> fp . seek ( pos ) <EOL> def sorted_dict_repr ( d ) : <EOL> """<STR_LIT>""" <EOL> keys = list ( d . keys ( ) ) <EOL> keys . sort ( ) <EOL> return "<STR_LIT:{>" + "<STR_LIT:U+002CU+0020>" . join ( [ "<STR_LIT>" % ( k , d [ k ] ) for k in keys ] ) + "<STR_LIT:}>" <EOL> def restore__ast ( _ast ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( _ast , '<STR_LIT>' ) : <EOL> return <EOL> _ast . PyCF_ONLY_AST = <NUM_LIT:2> << <NUM_LIT:9> <EOL> m = compile ( """<STR_LIT>""" , '<STR_LIT>' , '<STR_LIT>' , _ast . PyCF_ONLY_AST ) <EOL> _ast . Module = type ( m ) <EOL> for cls in _ast . Module . __mro__ : <EOL> if cls . __name__ == '<STR_LIT>' : <EOL> _ast . mod = cls <EOL> elif cls . __name__ == '<STR_LIT>' : <EOL> _ast . AST = cls <EOL> _ast . FunctionDef = type ( m . body [ <NUM_LIT:0> ] ) <EOL> _ast . ClassDef = type ( m . body [ <NUM_LIT:1> ] ) <EOL> _ast . If = type ( m . body [ <NUM_LIT:2> ] ) <EOL> _ast . Name = type ( m . body [ <NUM_LIT:3> ] . targets [ <NUM_LIT:0> ] ) <EOL> _ast . Store = type ( m . body [ <NUM_LIT:3> ] . targets [ <NUM_LIT:0> ] . ctx ) <EOL> _ast . Str = type ( m . body [ <NUM_LIT:3> ] . value ) <EOL> _ast . Sub = type ( m . body [ <NUM_LIT:4> ] . value . op ) <EOL> _ast . Add = type ( m . body [ <NUM_LIT:4> ] . value . left . op ) <EOL> _ast . Div = type ( m . body [ <NUM_LIT:4> ] . value . right . op ) <EOL> _ast . Mult = type ( m . body [ <NUM_LIT:4> ] . value . right . left . op ) <EOL> _ast . RShift = type ( m . body [ <NUM_LIT:5> ] . value . op ) <EOL> _ast . LShift = type ( m . body [ <NUM_LIT:5> ] . value . left . op ) <EOL> _ast . Mod = type ( m . body [ <NUM_LIT:5> ] . value . left . left . op ) <EOL> _ast . FloorDiv = type ( m . body [ <NUM_LIT:5> ] . value . left . left . left . op ) <EOL> _ast . BitOr = type ( m . body [ <NUM_LIT:6> ] . value . op ) <EOL> _ast . BitXor = type ( m . body [ <NUM_LIT:6> ] . value . left . op ) <EOL> _ast . BitAnd = type ( m . body [ <NUM_LIT:6> ] . value . left . left . op ) <EOL> _ast . Or = type ( m . body [ <NUM_LIT:7> ] . value . op ) <EOL> _ast . And = type ( m . body [ <NUM_LIT:7> ] . value . values [ <NUM_LIT:0> ] . op ) <EOL> _ast . Invert = type ( m . body [ <NUM_LIT:8> ] . value . right . op ) <EOL> _ast . Not = type ( m . body [ <NUM_LIT:8> ] . value . left . right . op ) <EOL> _ast . UAdd = type ( m . body [ <NUM_LIT:8> ] . value . left . right . operand . op ) <EOL> _ast . USub = type ( m . body [ <NUM_LIT:8> ] . value . left . left . op ) <EOL> _ast . Or = type ( m . body [ <NUM_LIT:9> ] . value . op ) <EOL> _ast . And = type ( m . body [ <NUM_LIT:9> ] . value . values [ <NUM_LIT:0> ] . op ) <EOL> _ast . IsNot = type ( m . body [ <NUM_LIT:10> ] . value . ops [ <NUM_LIT:0> ] ) <EOL> _ast . NotEq = type ( m . body [ <NUM_LIT:10> ] . value . ops [ <NUM_LIT:1> ] ) <EOL> _ast . Is = type ( m . body [ <NUM_LIT:10> ] . value . left . ops [ <NUM_LIT:0> ] ) <EOL> _ast . Eq = type ( m . body [ <NUM_LIT:10> ] . value . left . ops [ <NUM_LIT:1> ] ) <EOL> _ast . Gt = type ( m . body [ <NUM_LIT:11> ] . value . ops [ <NUM_LIT:0> ] ) <EOL> _ast . Lt = type ( m . body [ <NUM_LIT:11> ] . value . ops [ <NUM_LIT:1> ] ) <EOL> _ast . GtE = type ( m . body [ <NUM_LIT:11> ] . value . ops [ <NUM_LIT:2> ] ) <EOL> _ast . LtE = type ( m . body [ <NUM_LIT:11> ] . value . ops [ <NUM_LIT:3> ] ) <EOL> _ast . In = type ( m . body [ <NUM_LIT:12> ] . value . ops [ <NUM_LIT:0> ] ) <EOL> _ast . NotIn = type ( m . body [ <NUM_LIT:12> ] . value . ops [ <NUM_LIT:1> ] ) <EOL> try : <EOL> from inspect import CO_VARKEYWORDS , CO_VARARGS <EOL> def inspect_func_args ( fn ) : <EOL> co = fn . __code__ <EOL> nargs = co . co_argcount <EOL> names = co . co_varnames <EOL> args = list ( names [ : nargs ] ) <EOL> varargs = None <EOL> if co . co_flags & CO_VARARGS : <EOL> varargs = co . co_varnames [ nargs ] <EOL> nargs = nargs + <NUM_LIT:1> <EOL> varkw = None <EOL> if co . co_flags & CO_VARKEYWORDS : <EOL> varkw = co . co_varnames [ nargs ] <EOL> return args , varargs , varkw , fn . __defaults__ <EOL> except ImportError : <EOL> import inspect <EOL> def inspect_func_args ( fn ) : <EOL> return inspect . getargspec ( fn ) </s>
