o 3aB7@sddlZddlZddlZddlmZmZGdddZGdddZGdddZd d Z d d Z d dZ ddZ ddZ eZddZGdddZddZGdddeZddZdS)N)total_orderingwrapsc@s<eZdZdZdZeddZd ddZddZd d d Z dS) cached_propertya[ Decorator that converts a method with a single self argument into a property cached on the instance. A cached property can be made out of an existing method: (e.g. ``url = cached_property(get_absolute_url)``). The optional ``name`` argument is obsolete as of Python 3.6 and will be deprecated in Django 4.0 (#30127). NcCtd)NzICannot use cached_property instance without calling __set_name__() on it. TypeError)instancer 9/usr/lib/python3/dist-packages/django/utils/functional.pyfuncszcached_property.funccCs||_t|d|_dS)N__doc__) real_funcgetattrr )selfr namer r r __init__szcached_property.__init__cCs<|jdur||_|j|_dS||jkrtd|j|fdS)NzJCannot assign the same cached_property to two different names (%r and %r).)rr r r)rownerrr r r __set_name__s   zcached_property.__set_name__cCs&|dur|S||}|j|j<|S)z Call the function and put the return value in instance.__dict__ so that subsequent attribute access on the instance returns the cached value instead of calling cached_property.__get__(). N)r __dict__r)rrclsresr r r __get__(szcached_property.__get__N) __name__ __module__ __qualname__r r staticmethodr rrrr r r r rs    rc@s,eZdZdZd ddZd ddZddZdS) classpropertyz Decorator that converts a method with a single cls argument into a property that can be accessed directly from the class. NcCs ||_dSrfgetrmethodr r r r9 zclassproperty.__init__cCs ||Srr)rrrr r r r<r"zclassproperty.__get__cCs ||_|Srrr r r r getter?szclassproperty.getterr)rrrr rrr#r r r r r4s    rc@seZdZdZdS)Promisez Base class for the proxy class created in the closure of the lazy function. It's used to recognize promises in code. N)rrrr r r r r r$Dsr$cs2tGfdddttfdd}|S)a Turn any callable into a lazy evaluated callable. result classes or types is required -- at least one is needed so that the automatic forcing of the lazy evaluation code is triggered. Results are not memoized; the function is evaluated on every access. cseZdZdZdZddZfddZddZefd d Z efd d Z fd dZ fddZ fddZ fddZddZddZddZddZddZdd Zd!d"Zd#d$Zd%S)&zlazy..__proxy__z Encapsulate a function call and act as a proxy for methods that are called on the result of that function. The function is not evaluated until one of the methods on the result is called. FcSs&||_||_|js |d|j_dS)NT)_proxy____args _proxy____kw_proxy____prepared__prepare_class__ __class__)rargskwr r r r]s  z lazy..__proxy__.__init__cst|j|jffSr)_lazy_proxy_unpickler%r&rr resultclassesr r __reduce__dsz"lazy..__proxy__.__reduce__cS t|Sr)repr_proxy____castr-r r r __repr__j z lazy..__proxy__.__repr__csD] }|D]}|jD]}t||rq ||}t|||q qqtv|_tv|_|jr7|jr7Jd|jr@|j |_ dS|jrI|j |_ dSdS)Nz9Cannot call lazy() with both bytes and text return types.) mrorhasattr __promise__setattrbytes_delegate_bytesstr_delegate_text_proxy____text_cast__str___proxy____bytes_cast __bytes__)r resultclasstype_ method_namemethr/r r r(ms&        z)lazy..__proxy__.__prepare_class__csfdd}|S)Ncs&|ji|j}t||i|Sr)r%r&r)rr*r+r)r rDr r __wrapper__sz8lazy..__proxy__.__promise__..__wrapper__r )rrDrGr )rDr r8sz#lazy..__proxy__.__promise__cs|ji|jSr)r%r&r-rHr r __text_castsz#lazy..__proxy__.__text_castcst|ji|jSr)r:r%r&r-rHr r __bytes_castz$lazy..__proxy__.__bytes_castcs|ji|jSr)r%r&encoder-rHr r __bytes_cast_encodedrKz,lazy..__proxy__.__bytes_cast_encodedcs.|jr|S|jr|S|ji|jSr)r;r@r=r>r%r&r-rHr r __casts zlazy..__proxy__.__castcSr1r)r<r3r-r r r r?s zlazy..__proxy__.__str__cSst|tr |}||kSr isinstancer$r3rotherr r r __eq__  zlazy..__proxy__.__eq__cSst|tr |}||kSrrOrQr r r __lt__rTzlazy..__proxy__.__lt__cSr1r)hashr3r-r r r __hash__r5z lazy..__proxy__.__hash__cSs|jr t||S||Sr)r=r<r3)rrhsr r r __mod__s  zlazy..__proxy__.__mod__cSs ||Srr3rQr r r __add__r5zlazy..__proxy__.__add__cSs ||SrrZrQr r r __radd__r5z lazy..__proxy__.__radd__cSs||t|<|Sr)id)rmemor r r __deepcopy__s z$lazy..__proxy__.__deepcopy__N)rrrr r'rr0r4 classmethodr(r8r>r@_proxy____bytes_cast_encodedr3r?rSrUrWrYr[r\r_r r.r r __proxy__Ts,    rbcs ||Srr )r*r+)rbr r rG zlazy..__wrapper__)rr$r)r r/rGr )rbr r/r lazyLs krdcGst|g|R|i|Sr)rd)r r*kwargsr/r r r r,sr,cCsttt|S)zK Shortcut for the common case of a lazy callable that returns str. )rdr<)textr r r lazystrsrgcsstdfdd}|S)z A decorator that allows a function to be called with one or more lazy arguments. If none of the args are lazy, the function is evaluated immediately, otherwise a __proxy__ is returned that will evaluate the function when needed. z3You must pass at least one argument to keep_lazy().cs*tgRtfdd}|S)Ncs:tddt||Dr|i|S|i|S)Ncss|]}t|tVqdSr)rPr$).0argr r r sz@keep_lazy..decorator..wrapper..)any itertoolschainvalues)r*rer lazy_funcr r wrappersz-keep_lazy..decorator..wrapper)rdr)r rqrFror decoratorszkeep_lazy..decoratorr)r/rrr rFr keep_lazys  rscCs tt|S)zO A decorator for functions that accept lazy arguments and return text. )rsr<rHr r r keep_lazy_texts rtcsfdd}|S)Ncs$|jtur ||jg|RSr)_wrappedempty_setup)rr*rHr r inners znew_method_proxy..innerr )r rxr rHr new_method_proxys ryc@seZdZdZdZddZeeZddZ ddZ d d Z d d Z d dZ ddZeeZeeZeeZeeZeeedZeejZeejZeejZ eej!Z"ee#Z$eej%Z&eej'Z(eej)Z*ee+Z,ee-Z.eej/Z0dS) LazyObjectz A wrapper for another class that can be used to delay instantiation of the wrapped class. By subclassing, you have the opportunity to intercept and alter the instantiation. If you don't need to do that, use SimpleLazyObject. NcCs t|_dSr)rvrur-r r r rrczLazyObject.__init__cCs:|dkr ||jd<dS|jtur|t|j||dS)Nru)rrurvrwr9)rrvaluer r r __setattr__s  zLazyObject.__setattr__cCs2|dkrtd|jtur|t|j|dS)Nruzcan't delete _wrapped.)rrurvrwdelattr)rrr r r __delattr__s  zLazyObject.__delattr__cCr)zU Must be implemented by subclasses to initialize the wrapped object. z7subclasses of LazyObject must provide a _setup() method)NotImplementedErrorr-r r r rwszLazyObject._setupcCs|jtur |t|jffSr)rurvrwunpickle_lazyobjectr-r r r r02rTzLazyObject.__reduce__cCs |jtur t|St|jSr)rurvtypecopyr-r r r __copy__7   zLazyObject.__copy__cCs2|jturt|}||t|<|St|j|Sr)rurvrr]rdeepcopyrr^resultr r r r_@   zLazyObject.__deepcopy__r))1rrrr rurryr __getattr__r|r~rwr0rr_r:rAr<r?bool__bool__dir__dir__propertyoperator attrgetterr)eqrSltrUgt__gt__ne__ne__rVrWgetitem __getitem__setitem __setitem__delitem __delitem__iter__iter__len__len__contains __contains__r r r r rzs6           rzcCs|S)zh Used to unpickle lazy objects. Just return its argument, which will be the wrapped object. r )wrappedr r r rbsrcs@eZdZdZfddZddZddZdd Zd d ZZ S) SimpleLazyObjectz A lazy object initialized from any function. Designed for compound objects of unknown type. For builtins or objects of known type, use django.utils.functional.lazy. cs||jd<tdS)aC Pass in a callable that returns the object to be wrapped. If copies are made of the resulting SimpleLazyObject, which can happen in various circumstances within Django, then you must ensure that the callable can be safely run more than once and will return the same value. _setupfuncN)rsuperr)rr r)r r rqs zSimpleLazyObject.__init__cCs||_dSr)rrur-r r r rw}szSimpleLazyObject._setupcCs*|jtur |j}n|j}dt|j|fS)Nz<%s: %r>)rurvrrr)r repr_attrr r r r4s zSimpleLazyObject.__repr__cCs |jtur t|jSt|jSr)rurvrrrr-r r r rrzSimpleLazyObject.__copy__cCs2|jturt|j}||t|<|St|j|Sr)rurvrrr]rrrr r r r_rzSimpleLazyObject.__deepcopy__) rrrr rrwr4rr_ __classcell__r r rr rjs   rcCs(ggf}|D] }||||q|S)z Split the values into two sets, based on the return value of the function (True/False). e.g.: >>> partition(lambda x: x > 3, range(5)) [0, 1, 2, 3], [4] )append) predicaternresultsitemr r r partitionsr)rrlr functoolsrrrrr$rdr,rgrsrtobjectrvryrzrrrr r r r s$-|g 2