<s> import copy <EOL> import operator <EOL> from functools import wraps <EOL> import sys <EOL> from django . utils import six <EOL> from django . utils . six . moves import copyreg <EOL> def curry ( _curried_func , * args , ** kwargs ) : <EOL> def _curried ( * moreargs , ** morekwargs ) : <EOL> return _curried_func ( * ( args + moreargs ) , ** dict ( kwargs , ** morekwargs ) ) <EOL> return _curried <EOL> def memoize ( func , cache , num_args ) : <EOL> """<STR_LIT>""" <EOL> @ wraps ( func ) <EOL> def wrapper ( * args ) : <EOL> mem_args = args [ : num_args ] <EOL> if mem_args in cache : <EOL> return cache [ mem_args ] <EOL> result = func ( * args ) <EOL> cache [ mem_args ] = result <EOL> return result <EOL> return wrapper <EOL> class cached_property ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , func ) : <EOL> self . func = func <EOL> def __get__ ( self , instance , type = None ) : <EOL> if instance is None : <EOL> return self <EOL> res = instance . __dict__ [ self . func . __name__ ] = self . func ( instance ) <EOL> return res <EOL> class Promise ( object ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def lazy ( func , * resultclasses ) : <EOL> """<STR_LIT>""" <EOL> @ total_ordering <EOL> class __proxy__ ( Promise ) : <EOL> """<STR_LIT>""" <EOL> __dispatch = None <EOL> def __init__ ( self , args , kw ) : <EOL> self . __args = args <EOL> self . __kw = kw <EOL> if self . __dispatch is None : <EOL> self . __prepare_class__ ( ) <EOL> def __reduce__ ( self ) : <EOL> return ( <EOL> _lazy_proxy_unpickle , <EOL> ( func , self . __args , self . __kw ) + resultclasses <EOL> ) <EOL> def __prepare_class__ ( cls ) : <EOL> cls . __dispatch = { } <EOL> for resultclass in resultclasses : <EOL> cls . __dispatch [ resultclass ] = { } <EOL> for type_ in reversed ( resultclass . mro ( ) ) : <EOL> for ( k , v ) in type_ . __dict__ . items ( ) : <EOL> meth = cls . __promise__ ( resultclass , k , v ) <EOL> if hasattr ( cls , k ) : <EOL> continue <EOL> setattr ( cls , k , meth ) <EOL> cls . _delegate_bytes = bytes in resultclasses <EOL> cls . _delegate_text = six . text_type in resultclasses <EOL> assert not ( cls . _delegate_bytes and cls . _delegate_text ) , "<STR_LIT>" <EOL> if cls . _delegate_text : <EOL> if six . PY3 : <EOL> cls . __str__ = cls . __text_cast <EOL> else : <EOL> cls . __unicode__ = cls . __text_cast <EOL> elif cls . _delegate_bytes : <EOL> if six . PY3 : <EOL> cls . __bytes__ = cls . __bytes_cast <EOL> else : <EOL> cls . __str__ = cls . __bytes_cast <EOL> __prepare_class__ = classmethod ( __prepare_class__ ) <EOL> def __promise__ ( cls , klass , funcname , method ) : <EOL> def __wrapper__ ( self , * args , ** kw ) : <EOL> res = func ( * self . __args , ** self . __kw ) <EOL> for t in type ( res ) . mro ( ) : <EOL> if t in self . __dispatch : <EOL> return self . __dispatch [ t ] [ funcname ] ( res , * args , ** kw ) <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> if klass not in cls . __dispatch : <EOL> cls . __dispatch [ klass ] = { } <EOL> cls . __dispatch [ klass ] [ funcname ] = method <EOL> return __wrapper__ <EOL> __promise__ = classmethod ( __promise__ ) <EOL> def __text_cast ( self ) : <EOL> return func ( * self . __args , ** self . __kw ) <EOL> def __bytes_cast ( self ) : <EOL> return bytes ( func ( * self . __args , ** self . __kw ) ) <EOL> def __cast ( self ) : <EOL> if self . _delegate_bytes : <EOL> return self . __bytes_cast ( ) <EOL> elif self . _delegate_text : <EOL> return self . __text_cast ( ) <EOL> else : <EOL> return func ( * self . __args , ** self . __kw ) <EOL> def __eq__ ( self , other ) : <EOL> if isinstance ( other , Promise ) : <EOL> other = other . __cast ( ) <EOL> return self . __cast ( ) == other <EOL> def __lt__ ( self , other ) : <EOL> if isinstance ( other , Promise ) : <EOL> other = other . __cast ( ) <EOL> return self . __cast ( ) < other <EOL> def __hash__ ( self ) : <EOL> return hash ( self . __cast ( ) ) <EOL> def __mod__ ( self , rhs ) : <EOL> if self . _delegate_bytes and six . PY2 : <EOL> return bytes ( self ) % rhs <EOL> elif self . _delegate_text : <EOL> return six . text_type ( self ) % rhs <EOL> return self . __cast ( ) % rhs <EOL> def __deepcopy__ ( self , memo ) : <EOL> memo [ id ( self ) ] = self <EOL> return self <EOL> @ wraps ( func ) <EOL> def __wrapper__ ( * args , ** kw ) : <EOL> return __proxy__ ( args , kw ) <EOL> return __wrapper__ <EOL> def _lazy_proxy_unpickle ( func , args , kwargs , * resultclasses ) : <EOL> return lazy ( func , * resultclasses ) ( * args , ** kwargs ) <EOL> def allow_lazy ( func , * resultclasses ) : <EOL> """<STR_LIT>""" <EOL> @ wraps ( func ) <EOL> def wrapper ( * args , ** kwargs ) : <EOL> for arg in list ( args ) + list ( six . itervalues ( kwargs ) ) : <EOL> if isinstance ( arg , Promise ) : <EOL> break <EOL> else : <EOL> return func ( * args , ** kwargs ) <EOL> return lazy ( func , * resultclasses ) ( * args , ** kwargs ) <EOL> return wrapper <EOL> empty = object ( ) <EOL> def new_method_proxy ( func ) : <EOL> def inner ( self , * args ) : <EOL> if self . _wrapped is empty : <EOL> self . _setup ( ) <EOL> return func ( self . _wrapped , * args ) <EOL> return inner <EOL> class LazyObject ( object ) : <EOL> """<STR_LIT>""" <EOL> _wrapped = None <EOL> def __init__ ( self ) : <EOL> self . _wrapped = empty <EOL> __getattr__ = new_method_proxy ( getattr ) <EOL> def __setattr__ ( self , name , value ) : <EOL> if name == "<STR_LIT>" : <EOL> self . __dict__ [ "<STR_LIT>" ] = value <EOL> else : <EOL> if self . _wrapped is empty : <EOL> self . _setup ( ) <EOL> setattr ( self . _wrapped , name , value ) <EOL> def __delattr__ ( self , name ) : <EOL> if name == "<STR_LIT>" : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> if self . _wrapped is empty : <EOL> self . _setup ( ) <EOL> delattr ( self . _wrapped , name ) <EOL> def _setup ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> __dir__ = new_method_proxy ( dir ) <EOL> @ new_method_proxy <EOL> def __getitem__ ( self , key ) : <EOL> return self [ key ] <EOL> @ new_method_proxy <EOL> def __setitem__ ( self , key , value ) : <EOL> self [ key ] = value <EOL> @ new_method_proxy <EOL> def __delitem__ ( self , key ) : <EOL> del self [ key ] <EOL> _super = super <EOL> class SimpleLazyObject ( LazyObject ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , func ) : <EOL> """<STR_LIT>""" <EOL> self . __dict__ [ '<STR_LIT>' ] = func <EOL> _super ( SimpleLazyObject , self ) . __init__ ( ) <EOL> def _setup ( self ) : <EOL> self . _wrapped = self . _setupfunc ( ) <EOL> if six . PY3 : <EOL> __bytes__ = new_method_proxy ( bytes ) <EOL> __str__ = new_method_proxy ( str ) <EOL> else : <EOL> __str__ = new_method_proxy ( str ) <EOL> __unicode__ = new_method_proxy ( unicode ) <EOL> def __deepcopy__ ( self , memo ) : <EOL> if self . _wrapped is empty : <EOL> result = SimpleLazyObject ( self . _setupfunc ) <EOL> memo [ id ( self ) ] = result <EOL> return result <EOL> else : <EOL> return copy . deepcopy ( self . _wrapped , memo ) <EOL> def __getstate__ ( self ) : <EOL> if self . _wrapped is empty : <EOL> self . _setup ( ) <EOL> return self . _wrapped . __dict__ <EOL> @ classmethod <EOL> def __newobj__ ( cls , * args ) : <EOL> return cls . __new__ ( cls , * args ) <EOL> def __reduce_ex__ ( self , proto ) : <EOL> if proto >= <NUM_LIT:2> : <EOL> return ( self . __newobj__ , ( self . __class__ , ) , self . __getstate__ ( ) ) <EOL> else : <EOL> return ( copyreg . _reconstructor , ( self . __class__ , object , None ) , self . __getstate__ ( ) ) <EOL> def __repr__ ( self ) : <EOL> if self . _wrapped is empty : <EOL> repr_attr = self . _setupfunc <EOL> else : <EOL> repr_attr = self . _wrapped <EOL> return '<STR_LIT>' % repr_attr <EOL> __class__ = property ( new_method_proxy ( operator . attrgetter ( "<STR_LIT>" ) ) ) <EOL> __eq__ = new_method_proxy ( operator . eq ) <EOL> __ne__ = new_method_proxy ( operator . ne ) <EOL> __hash__ = new_method_proxy ( hash ) <EOL> __bool__ = new_method_proxy ( bool ) <EOL> __nonzero__ = __bool__ <EOL> class lazy_property ( property ) : <EOL> """<STR_LIT>""" <EOL> def __new__ ( cls , fget = None , fset = None , fdel = None , doc = None ) : <EOL> if fget is not None : <EOL> @ wraps ( fget ) <EOL> def fget ( instance , instance_type = None , name = fget . __name__ ) : <EOL> return getattr ( instance , name ) ( ) <EOL> if fset is not None : <EOL> @ wraps ( fset ) <EOL> def fset ( instance , value , name = fset . __name__ ) : <EOL> return getattr ( instance , name ) ( value ) <EOL> if fdel is not None : <EOL> @ wraps ( fdel ) <EOL> def fdel ( instance , name = fdel . __name__ ) : <EOL> return getattr ( instance , name ) ( ) <EOL> return property ( fget , fset , fdel , doc ) <EOL> def partition ( predicate , values ) : <EOL> """<STR_LIT>""" <EOL> results = ( [ ] , [ ] ) <EOL> for item in values : <EOL> results [ predicate ( item ) ] . append ( item ) <EOL> return results <EOL> if sys . version_info >= ( <NUM_LIT:2> , <NUM_LIT:7> , <NUM_LIT:2> ) : <EOL> from functools import total_ordering <EOL> else : <EOL> def total_ordering ( cls ) : <EOL> """<STR_LIT>""" <EOL> convert = { <EOL> '<STR_LIT>' : [ ( '<STR_LIT>' , lambda self , other : not ( self < other or self == other ) ) , <EOL> ( '<STR_LIT>' , lambda self , other : self < other or self == other ) , <EOL> ( '<STR_LIT>' , lambda self , other : not self < other ) ] , <EOL> '<STR_LIT>' : [ ( '<STR_LIT>' , lambda self , other : not self <= other or self == other ) , <EOL> ( '<STR_LIT>' , lambda self , other : self <= other and not self == other ) , <EOL> ( '<STR_LIT>' , lambda self , other : not self <= other ) ] , <EOL> '<STR_LIT>' : [ ( '<STR_LIT>' , lambda self , other : not ( self > other or self == other ) ) , <EOL> ( '<STR_LIT>' , lambda self , other : self > other or self == other ) , <EOL> ( '<STR_LIT>' , lambda self , other : not self > other ) ] , <EOL> '<STR_LIT>' : [ ( '<STR_LIT>' , lambda self , other : ( not self >= other ) or self == other ) , <EOL> ( '<STR_LIT>' , lambda self , other : self >= other and not self == other ) , <EOL> ( '<STR_LIT>' , lambda self , other : not self >= other ) ] <EOL> } <EOL> roots = set ( dir ( cls ) ) & set ( convert ) <EOL> if not roots : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> root = max ( roots ) <EOL> for opname , opfunc in convert [ root ] : <EOL> if opname not in roots : <EOL> opfunc . __name__ = opname <EOL> opfunc . __doc__ = getattr ( int , opname ) . __doc__ <EOL> setattr ( cls , opname , opfunc ) <EOL> return cls </s>
