ó <¿CVc@sdZddlmZdZdddgZddlZejZgejD]ZdekrKe^qKe_ddl Z ee_ye Wn!e k r¬dd l m Z nXd „Zdd „Zd „Zd „Zd„Zd„Zd„Zed„ƒZdS(s Decorator module by Michele Simionato Copyright Michele Simionato, distributed under the terms of the BSD License (see below). http://www.phyast.pitt.edu/~micheles/python/documentation.html Included in NLTK for its support of a nice memoization decorator. iÿÿÿÿ(tprint_functionsrestructuredtext ent decoratort new_wrappertgetinfoNtnltk(tSetc Cs!tj|ƒs$tj|ƒs$t‚tj|ƒ\}}}}t|ƒ}|ra|j|ƒn|rw|j|ƒntj||||dd„ƒdd!}t|dƒrÃ|j }|j }n|j }|j }t d|jd|d|d |jd |jd |jd |jd |d|ƒ S(sg Returns an info dictionary containing: - name (the name of the function : str) - argnames (the names of the arguments : list) - defaults (the values of the default arguments : tuple) - signature (the signature : str) - doc (the docstring : str) - module (the module name : str) - dict (the function __dict__ : str) >>> def f(self, x=1, y=2, *args, **kw): pass >>> info = getinfo(f) >>> info["name"] 'f' >>> info["argnames"] ['self', 'x', 'y', 'args', 'kw'] >>> info["defaults"] (1, 2) >>> info["signature"] 'self, x, y, *args, **kw' t formatvaluecSsdS(Nt((tvalue((sa/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/decorators.pytBsiiÿÿÿÿt __closure__tnametargnamest signaturetdefaultstdoctmoduletdicttglobalstclosure(tinspecttismethodt isfunctiontAssertionErrort getargspectlisttappendt formatargspecthasattrR t __globals__t func_closuret func_globalsRt__name__t __defaults__t__doc__t __module__t__dict__( tfunctregargstvarargst varkwargsRR R t_closuret_globals((sa/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/decorators.pyR s$$     cCsg|pt|ƒ}|d|_|d|_|d|_|jj|dƒ|d|_||_|S(NR RRRR(RR R"R#R$tupdateR!t undecorated(twrappertmodeltinfodict((sa/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/decorators.pytupdate_wrapperRs     cCsrt|tƒr|}n t|ƒ}d|dks@tdƒ‚d|}t|td|ƒƒ}t|||ƒS(sA An improvement over functools.update_wrapper. The wrapper is a generic callable object. It works by generating a copy of the wrapper with the right signature and by updating the copy, not the original. Moreovoer, 'model' can be a dictionary with keys 'name', 'doc', 'module', 'dict', 'defaults'. t _wrapper_R s("_wrapper_" is a reserved argument name!s.lambda %(signature)s: _wrapper_(%(signature)s)(t isinstanceRRRtevalR0(R-R.R/tsrctfuncopy((sa/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/decorators.pyR\s    cst‡‡fd†ˆƒS(Ncsˆjˆ||ŽS(N(tcall(tatk(R%tself(sa/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/decorators.pyR ps(R(R9R%((R%R9sa/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/decorators.pyt__call__oscCsUtt|ƒƒ}d|kr-tdƒ‚nd|krHtdƒ‚nt|_|S(sí Take a class with a ``.caller`` method and return a callable decorator object. It works by adding a suitable __call__ method to the class; it raises a TypeError if the class already has a nontrivial __call__ method. R:s=You cannot decorate a class with a nontrivial __call__ methodR6s2You cannot decorate a class without a .call method(tsettdirt TypeErrorR:(tclstattrs((sa/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/decorators.pytdecorator_factoryrs   cs5tjˆƒrtˆƒS‡fd†}t|ˆƒS(sò General purpose decorator factory: takes a caller function as input and returns a decorator with the same attributes. A caller function is any function like this:: def caller(func, *args, **kw): # do something return func(*args, **kw) Here is an example of usage: >>> @decorator ... def chatty(f, *args, **kw): ... print("Calling %r" % f.__name__) ... return f(*args, **kw) >>> chatty.__name__ 'chatty' >>> @chatty ... def f(): pass ... >>> f() Calling 'f' decorator can also take in input a class with a .caller method; in this case it converts the class into a factory of callable decorator objects. See the documentation for an example. csst|ƒ}|d}d|kp+d|k s;tdƒ‚d|}t|td|dˆƒƒ}t|||ƒS(NR t_call_t_func_s2You cannot use _call_ or _func_ as argument names!s3lambda %(signature)s: _call_(_func_, %(signature)s)(RRR3RR0(R%R/R R4tdec_func(tcaller(sa/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/decorators.pyt _decorator£s    (RtisclassR@R0(RDRE((RDsa/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/decorators.pyRƒs  cCsCyt||ƒSWn+tk r>|ƒ}t|||ƒ|SXdS(s'Similar to .setdefault in dictionaries.N(tgetattrtAttributeErrortsetattr(tobjR t default_thunktdefault((sa/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/decorators.pytgetattr_®s   cGsDt|dtƒ}||kr&||S||Œ}|||<|SdS(Nt memoize_dic(RMR(R%targstdictresult((sa/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/decorators.pytmemoize·s    (R"t __future__Rt __docformat__t__all__tsystpatht old_sys_pathtpRR;t NameErrortsetsRRtNoneR0RR:R@RRMRR(((sa/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/decorators.pyts(  +    2    +