;ò À4;c@s^dZdkTdkZdklZdkZdkZdZeedƒo eiZ n d„Z ydk Z hd„Z WnnXei dd joei d d jo[ e d „Z nd fd „ƒYZdfd„ƒYZd„Zdfd„ƒYZdfd„ƒYZdfd„ƒYZdefd„ƒYZdefd„ƒYZdefd„ƒYZdefd„ƒYZdefd „ƒYZd!efd"„ƒYZd#efd$„ƒYZd%efd&„ƒYZd'efd(„ƒYZd)efd*„ƒYZd+„Zd,efd-„ƒYZd.efd/„ƒYZ d0efd1„ƒYZ!e d2„Z"d3efd4„ƒYZ#d5„Z$d6„Z%d7„Z&e d8„Z'e d9„Z(ee'ƒZ)d:e)_d;„Z*d<„Z+e*Z,e+Z-d=e.fd>„ƒYZ/d?fd@„ƒYZ0dA„Z1dB„Z2dC„Z3dD„Z4dE„Z5e e dF„Z6dG„Z7dHefdI„ƒYZ8dS(JsÜ functional.py provides support for a functional style of Python programming. It includes support for curried functions, and some higher-order functions for composing, sequencing, and otherwise manipulating functions. (s*N(scopys1.1.0s _getframecCs,y t‚WntiƒdiiSnXdS(Ni(s RuntimeErrorssyssexc_infostb_framesf_back(((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pys getStackFrame$s cCsntƒi}| o |i}nh}|i|iƒ|i|ƒt i |i |ƒ}|||i <|SdS(sù Returns a function whose global namespace is a **copy of** *func*'s globals, augmented with the local variables of the calling function. This is a common requirement for functional programming. This can be duplicated in standard Python using default arguments, but it is generally more cumbersome. Note there is little reason for this function under Python 2.1, which provides this capability directly. Example: #A function which returns a function which returns 1 if an item is #in *sequence*, 0 otherwise def contained(sequence): def contained_helper(x): return x in sequence return closure(contained_helper) >>>lst = [1,2,3] >>>is123 = contained(lst) >>>is123(2) 1 >>> #An example of 'static' variables, somewhat like in C: def bindCallCount(func): callCount = ref(0) return closure(func) #Note this function uses a variable, callCount, that is not defined. def printAndIncrement(): print callCount.val callCount.val = callCount.val + 1 #But with closures, we can make this function work: >>>printAndInc = bindCallCount(printAndIncrement) >>>printAndInc() 0 >>>printAndInc() 1 >>> So callCount is preserved across function invocations, but is not taking up space in the module namespace. N(s getStackFramesf_backsframeslocssf_localssglobssupdatesfuncs func_globalssnewsfunctions func_codesnewfuncs__name__(sfuncslocssframesglobssnewfunc((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pysclosure+s-    iiicCs|SdS(N(sfunc(sfuncslocs((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pysclosuregssRefcBstZdZd„ZRS(så A simple class to hold a value. Useful when one wishes to share a value between functions, or otherwise access the value from a local scope without using a 'global' statement to avoid rebinding the name locally. cCs ||_dS(N(svalsself(sselfsval((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pys__init__ps(s__name__s __module__s__doc__s__init__(((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pysRefjs sBlankcBstZdZRS(s… When passed in a call to curry, or to a curried function, this is treated as a placeholder for future calls to replace. (s__name__s __module__s__doc__(((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pysBlankss cCsŸd}g}t|ƒ}xT|D]L}||jo ||ƒo|i||ƒ|d}q|i|ƒqW||jo|i t ||ƒƒn|SdS(s] This function returns a list based on *original*, with any elements that *func* returns nonzero for replaced one by one with items from *mix*. Any unused items in *mix* appear at the end of the new list. If there are items which should be replaced, but there are no more items in *mix*, then those items are left unchanged. iiN( smixIndexsbuildslensmixsmixlensoriginalsitemsfuncsappendsextendslist(sfuncsoriginalsmixsmixIndexsitemsmixlensbuild((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pysblendzs  s FuncMethUnioncBs3tZdZdfd„ƒYZd„Zd„ZRS(sP This utility class exposes instance methods and functions identically. sMethCodecBstZd„Zd„ZRS(NcCs ||_dS(N(scodesselfs_code(sselfscode((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pys__init__•scCsR|djo|iidSn0|djo|iidSnt|i|ƒSdS(Ns co_argcountis co_varnames(snamesselfs_codes co_argcounts co_varnamessgetattr(sselfsname((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pys __getattr__˜s   (s__name__s __module__s__init__s __getattr__(((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pysMethCode”s cCs_t|ƒtjo6t|dƒot|dƒ}qId„}|}nt|_||_ dS(Ns__init__cCsdS(N((((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pyshelper¨s( stypesfuncs ClassTypeshasattrsgetattrshelpersNonesselfs _methcodes_func(sselfsfuncshelper((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pys__init__ s   cCs|ddjo|ddfj o t|‚nt|i|ƒot|i|ƒSnt|idƒoŠ|djof|iip|iidjo5|i o|i |ii i ƒ|_n|iSqß|ii i Snt|ii |ƒSnt|‚dS(Nis_s__doc__s__name__sim_funcs func_codes__init__( snamesAttributeErrorshasattrsselfs_funcsgetattrsim_selfs__name__s _methcodesMethCodesim_funcs func_code(sselfsname((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pys __getattr__®s%     (s__name__s __module__s__doc__sMethCodes__init__s __getattr__(((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pys FuncMethUnions  sSimCodecBs tZdZd„Zd„ZRS(sÀ Holds some of the same attributes a code object would have, for the benefit of functions like curry that work better when they have the information that code objects provide. cCs ||_dS(N(sfunctorsselfs_func(sselfsfunctor((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pys__init__ÅscCsó|djo|iiSnÌ|djo|iiƒSn®|djo|iiƒSn|djotSn{|djo|iiƒSn]|djo|iiƒSn?t |ƒdjo|d djot |ii i |ƒSnt |‚dS( Nsco_namesco_namess co_argcountsco_codes co_varnamessco_flagsisco_(snamesselfs_funcs func_namesgetNamess getArgCountsNones getVarNamessgetFlagsslensgetattrs__call__s func_codesAttributeError(sselfsname((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pys __getattr__Ès      $(s__name__s __module__s__doc__s__init__s __getattr__(((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pysSimCode¿s  sFunctorcBsÎtZdZed„Zd„Zd„Zd„Zd„Zd„Z d„Z d„Z d „Z d „Z d „Zd „Zd „Zd„Zd„Zd„Zd„Zd„Zd„Zd„Zd„ZRS(s‘ A class to assist functors (callable class instances) in masquerading as functions, by providing much of the material needed by code which introspects functions for useful information (curry being the main example in this module). A functor can simply inherit from this class, and override any of the methods below to provide more accurate information if it is available. cCs1| o |i}n||_t|ƒ|_dS(N(sfuncsselfs__call__s_funcs FuncMethUnions_significant_func(sselfsfunc((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pys__init__ãs  cCs t‚dS(N(sNotImplementedError(sself((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pys__call__éscCs|iiSdS(N(sselfs __class__s__name__(sself((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pysgetNameìscCs y|iiSWn tSnXdS(N(sselfs_significant_funcs func_defaultssNone(sself((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pys getDefaultsïscCs|iiSdS(N(sselfs __class__s__doc__(sself((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pysgetDocõscCs|iiiSdS(N(sselfs_significant_funcs func_codesco_names(sself((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pysgetNamesøscCs|iiiSdS(N(sselfs_significant_funcs func_codes co_varnames(sself((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pys getVarNamesûscCs|iiiSdS(N(sselfs_significant_funcs func_codes co_argcount(sself((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pys getArgCountþscCs|iiiSdS(N(sselfs_significant_funcs func_codesco_flags(sself((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pysgetFlagsscCs |djot|ƒ|_|iSnm|djo|iƒSnR|djo|iƒSn7|djo|iiSn|djo|iƒSnt |‚dS(Ns func_codesfunc_docs func_defaultss func_globalss func_name( snamesSimCodesselfs func_codesgetDocs getDefaultss__call__s func_globalssgetNamesAttributeError(sselfsname((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pys __getattr__s      cCst||ƒSdS(N(scurrysselfsother(sselfsother((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pys __lshift__scCst||ƒSdS(N(scurrysothersself(sselfsother((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pys __rlshift__scCst||ƒSdS(N(salsosselfsother(sselfsother((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pys__add__scCst||ƒSdS(N(salsosothersself(sselfsother((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pys__radd__scCst||ƒSdS(N(sdisjoinsselfsother(sselfsother((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pys__or__scCst||ƒSdS(N(sdisjoinsothersself(sselfsother((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pys__ror__!scCst||ƒSdS(N(sconjoinsselfsother(sselfsother((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pys__and__$scCst||ƒSdS(N(sconjoinsothersself(sselfsother((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pys__rand__'scCst||ƒSdS(N(scomposesselfsother(sselfsother((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pys__mul__*scCst||ƒSdS(N(scomposesothersself(sselfsother((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pys__rmul__-scCsdGHt|ƒSdS(NsNOT!(s complementsself(sself((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pys__not__0s(s__name__s __module__s__doc__sNones__init__s__call__sgetNames getDefaultssgetDocsgetNamess getVarNamess getArgCountsgetFlagss __getattr__s __lshift__s __rlshift__s__add__s__radd__s__or__s__ror__s__and__s__rand__s__mul__s__rmul__s__not__(((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pysFunctorÚs,                     swrapcBstZdZd„ZRS(s| Convenience class which wraps up a function in a Functor so that it can be used with the functional operators. cOs|i||ŽSdS(N(sselfs_funcsargsskwargs(sselfsargsskwargs((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pys__call__9s(s__name__s __module__s__doc__s__call__(((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pyswrap4s scurrycBs2tZdZd„Zd„Zd„Zd„ZRS(sï A curried function (named after the person who pioneered the idea, Haskell Currry) is one which some arguments are already defined, something like default arguments in Python, but much more general. When you curry a function with some arguments, the result is a function which requires only those arguments that haven't been supplied already. For example: def addTwo(x, y): return x + y addFive = curry(addTwo, 5) Now addFive is a function which takes one argument, and returns that argument added to 5, since x is pre-bound to 5 by the curry(). In functional languages, currying must take place either from the beginning of the argument list, or the end (this is sometimes called an rcurry, or reverse curry). In Python, with keyword arguments, and *args and **kwargs, the situation is a little more complicated, so these are the rules: - All currying starts from the left side of the argument list. - Keyword arguments can be curried. - If you wish to supply an argument that occurs later in the paramter list without supplying the one before it, you can supply the special value Blank in the places you don't want filled. For example: addFive = curry(addTwo, Blank, 5) would result in the 5 argument being bound to y instead of x. If curry() can determine the minimum number of arguments for the function (func.func_code.co_argcount is present), then calls to the curried function with fewer than the remaining required arguments will only return a new curried function with the new arguments bound as well. For example: def threeArgs(x, y, z): return x + y + z twoArgs = curry(threeArgs, 5) Now twoArgs is a function which takes two arguments, and adds them to 5. If we now do: NewThing = twoArgs(20) NewThing is now a function which takes one argument, and returns that value plus 20 + 5. As soon as the function has accumulated enough arguments to reach the required argument count of the original function, the original function is called with all the accumulated arguments. In the case of explicity calling curry, however, if all arguments are supplied, the function is not called, rather a no-arguments function is returned. For example: oneArg = curry(twoArgs, 5) oneArg(5) would result in a call to twoArgs with arguments 5, 5. However: noArgs = curry(twoArgs, 5, 5) does not call twoArgs, until noArgs is called: noArgs() which again calls twoArgs, passing 5, 5 as the arguments. When applied to a function which has default arguments, only a number of arguments equal to the number of basic (non-defaulted) paramters will be required before the function is executed. For example: >>> def defaultTest(x, y, z, q = 4): ... return x, y, z, q ... >>> c = curry(defaultTest, 1, 2, 3) >>> c() (1, 2, 3, 4) >>> Of course, this doesn't prevent default arguments from being supplied either initially or at a later time, so long as that minimum count hasn't been met. Example: >>> c = curry(defaultTest, 1) >>> c(q = 7) >>> c(2,3) (1, 2, 3, 4) >>> cOs&ti||ƒ||_||_dS(N(sFunctors__init__sselfsfuncsargss_argsskwargss_kwargs(sselfsfuncsargsskwargs((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pys__init__˜s cCsjti|ƒ}td„|iƒ}td„|iiƒƒ}|t |ƒt |ƒ}t |dƒSdS(NcCs |tj S(N(sxsBlank(sx((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pysŸsi( sFunctors getArgCountsselfsbasicsfilters_argss filterArgss_kwargssvaluess filterKwargsslensadjustedsmax(sselfsadjusteds filterKwargssbasics filterArgs((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pys getArgCounts cCslti|ƒ}|t|iƒ}g}x3|D]+}|ii |ƒ o|i |ƒq/q/Wt |ƒSdS(N( sFunctors getVarNamessselfsvarnamesslens_argssnamessnames_kwargsshas_keysappendstuple(sselfsnamesvarnamessnames((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pys getVarNames¥sc Ostd„|i|ƒ}|iiƒ}|i|ƒt |jpt |i ƒj}d}y7|iƒ}|iƒ}|o|t|ƒ}nWnnX|t|ƒ}|p |djo)t|iƒ}||_||_|Snt|it|ƒ|ƒSdS(NcCs |tjS(N(sxsBlank(sx((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pys¯si(sblendsselfs_argssargss buildArgss_kwargsscopys buildKwargssupdateskwargssBlanksvaluesshasblanksargcounts getArgCounts getDefaultssdefsslenscurrys_funcscursapplystuple( sselfsargsskwargssdefsshasblankscursargcounts buildArgss buildKwargs((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pys__call__®s&     (s__name__s __module__s__doc__s__init__s getArgCounts getVarNamess__call__(((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pyscurry<s [   srcurrycBs;tZdZd„Zd„Zd„Zd„Zd„ZRS(sõ *rcurry* works much like *curry*, but applies its arguments to the other end of the argument list. Note that rcurry does not work on callables which provide no information about the number of arguments they take (such as builtin functions), nor does it work on ones with default or open ended (*args, **kwargs) arguments. For example: >>> def threeArgs(x, y, z): ... return x, y, z ... >>> rc = rcurry(threeArgs, 2, 3) >>> rc(1) (1, 2, 3) >>> cOsóti||ƒg|_||_y|iƒWntd‚nXti|ƒ}ti |ƒ}|o td‚n|i ƒd@o td‚n|i ƒd@o td‚n|t |ƒ}|djotf|||_n ||_dS(NsDNo info about number of args on func, use curry with Blanks instead.sKUse curry with keyword arguments for callables which take keyword argumentsisAUse curry for callables which take a variable number of argumentsisIUse curry for callables which take a variable number of keyword argumentsi(sFunctors__init__sselfsfuncs_argsskwargss_kwargss getArgCounts RuntimeErrorsargcounts getDefaultssdefssgetFlagsslensargssargdiffsBlank(sselfsfuncsargsskwargssdefssargcountsargdiff((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pys__init__Ôs&       cCsjti|ƒ}td„|iƒ}td„|iiƒƒ}|t |ƒt |ƒ}t |dƒSdS(NcCs |tj S(N(sxsBlank(sx((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pysìsi( sFunctors getArgCountsselfsbasicsfilters_argss filterArgss_kwargssvaluess filterKwargsslensadjustedsmax(sselfsadjusteds filterKwargssbasics filterArgs((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pys getArgCountês cCs‚ti|ƒ}|t|ƒttd„|iƒƒ }g}x3|D]+}|i i |ƒ o|i |ƒqEqEWt |ƒSdS(NcCs |tj S(N(sxsBlank(sx((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pysós( sFunctors getVarNamessselfsvarnamesslensfilters_argssnamessnames_kwargsshas_keysappendstuple(sselfsnamesvarnamessnames((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pys getVarNamesñs)cCs]ti|ƒ}|o-|t|ƒttd„|iƒƒ }n| o t}n|SdS(NcCs |tj S(N(sxsBlank(sx((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pysýs(sFunctors getDefaultssselfsdefsslensfilters_argssNone(sselfsdefs((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pys getDefaultsús - c Ostd„|i|ƒ}|iiƒ}|i|ƒt |jpt |i ƒj}d}y7|iƒ}|iƒ}|o|t|ƒ}nWnnX|t|ƒ}|p |djo)t|iƒ}||_||_|Snt|it|ƒ|ƒSdS(NcCs |tjS(N(sxsBlank(sx((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pyssi(sblendsselfs_argssargss buildArgss_kwargsscopys buildKwargssupdateskwargssBlanksvaluesshasblanksargcounts getArgCounts getDefaultssdefsslenscurrys_funcscursapplystuple( sselfsargsskwargssdefsshasblankscursargcounts buildArgss buildKwargs((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pys__call__s&     (s__name__s __module__s__doc__s__init__s getArgCounts getVarNamess getDefaultss__call__(((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pysrcurryÄs    scomposecBs tZdZd„Zd„ZRS(sË Takes any number of functions, and when called, applies them in reverse order. The first function to be called (the last to be passed in the call to compose()) is called with the arguments, and the other functions are called with the results of the previous function as its arguments. For example: def f(x): return x * 2 def g(x): return x + 5 h = compose(f, g) h(3) will now equal f(g(3)), or 16. cGsUti||dƒt|ƒ}t|tƒ o td‚n|iƒ||_ dS(NiÿÿÿÿsAll arguments must be callable.( sFunctors__init__sselfsargsslistsallscallables TypeErrorsreverses_funcs(sselfsargs((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pys__init__*s    cOsEt|id||ƒ}x!|idD]}||ƒ}q'W|SdS(Nii(sapplysselfs_funcssargsskwargssretsfunc(sselfsargsskwargssretsfunc((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pys__call__2s (s__name__s __module__s__doc__s__init__s__call__(((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pyscomposes  s applycomposecBs#tZd„Zefƒd„ZRS(NcOsNt|id||ƒ}x*|idD]}|i||Œƒ}q'W|SdS(Nii(sapplysselfs_funcssargsskwargssretsfuncs_applycompose__fixret(sselfsargsskwargssretsfunc((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pys__call__9s cCs(t|ƒ|j o |f}n|SdS(N(stypesrets TupleType(sselfsrets TupleType((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pys__fixret?s (s__name__s __module__s__call__stypes_applycompose__fixret(((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pys applycompose8s s joinfuncscBs tZdZd„Zd„ZRS(ss Takes a number of functions, and returns a function which, when called, returns a tuple of all the values the original functions would return if called with those arguments. Example: def timesFive(x): return x * 5 def square(x): return x * X >>>myfunc = joinfuncs(timesFive, square, str) >>>myfunc(2) (10, 4, '2') cGs?ti||dƒt|tƒ o td‚n||_dS(NisAll arguments must be callable.(sFunctors__init__sselfsargssallscallables TypeErrors_funcs(sselfsargs((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pys__init__Us cOsAg}x*|iD]}|it|||ƒƒqWt|ƒSdS(N( slstsselfs_funcssfuncsappendsapplysargsskwargsstuple(sselfsargsskwargsslstsfunc((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pys__call__[s  (s__name__s __module__s__doc__s__init__s__call__(((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pys joinfuncsDs  s complementcBs tZdZd„Zd„ZRS(s_ When called, returns the logical not of a call to *func* with the same arguments. cCsti||ƒdS(N(sFunctors__init__sselfsfunc(sselfsfunc((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pys__init__gscOst|i||ƒ SdS(N(sapplysselfs_funcsargsskwargs(sselfsargsskwargs((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pys__call__js(s__name__s __module__s__doc__s__init__s__call__(((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pys complementbs  sdisjoincBs tZdZd„Zd„ZRS(sq Takes the functions *funcs*, and when called will return 1 if any of *funcs* returns nonzero when applied to the arguments the disjoin was called with. This is basically equivalent to logical or-ing calls to all the functions in *funcs*. Note that if one function returns nonzero, the remaining functions are not evaluated. Example: #We need to find whether something is a sequence, other than a string. isTuple = lambda x:type(x) == type(()) isList = lambda x:type(x) == type([]) isUserSeq = lambda x:hasattr(x, '__getslice__') isSequence = disjoin(isTuple, isList, isUserSeq) cGsBti||dƒt|tƒ otdƒ‚n||_dS(NisAll arguments must be callable.(sFunctors__init__sselfsfuncssallscallables TypeErrors_funcs(sselfsfuncs((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pys__init__|scOs7x,|iD]!}t|||ƒodSq q WdSdS(Nii(sselfs_funcssfuncsapplysargsskwargs(sselfsargsskwargssfunc((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pys__call__‚s   (s__name__s __module__s__doc__s__init__s__call__(((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pysdisjoinms  sconjoincBs tZdZd„Zd„ZRS(s Takes the functions *funcs*, and when called will return 1 if all of *funcs* return nonzero when applied to the arguments the conjoin was called with. This is basically equivalent to logical and-ing all the functions in *funcs*. Note that if a function returns a false (not nonzero) result, the remaining functions are not evaluated. Example: #Function to decide if a number is greater than 5 and less than 10... gt5 = lambda x:x > 5 lt10 = lambda x:x < 10 between_5_and_10 = conjoin(gt5, lt10) cGs?ti||dƒt|tƒ o td‚n||_dS(NisAll arguments must be callable.(sFunctors__init__sselfsfuncssallscallables TypeErrors_funcs(sselfsfuncs((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pys__init__—s cOs8x-|iD]"}t|||ƒ odSq q WdSdS(Nii(sselfs_funcssfuncsapplysargsskwargs(sselfsargsskwargssfunc((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pys__call__œs   (s__name__s __module__s__doc__s__init__s__call__(((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pysconjoinˆs  s sequentialcBs#tZdZed„Zd„ZRS(sà Take a number of functions, and when called, call each of them in turn. The first function in the list is the "main", or significant function, and the others are taken for side-effects only. Their return values do not affect the return value of the also function, and any errors they raise are silently disregarded. If a function is explicity specified via the *main* parameter, that function will be the one whose return value is used, regardless of where in the sequence it appears. Example: >>> def one(): ... print "one" ... return 1 ... >>> def two(): ... print "two" ... return 2 ... >>> def three(): ... print "three" ... return 3 ... >>> x = one() one >>> x 1 >>> x = sequential([one, two, three])() one two three >>> x 1 >>> >>> x = sequential([one, two, three], main = three)() one two three >>> x 3 >>> cCsZt|tƒ o td‚n||_| o|d}nti||ƒ||_ dS(NsAll arguments must be callable.i( sallsfuncsscallables TypeErrorsselfs_funcssmainsFunctors__init__s_main(sselfsfuncssmain((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pys__init__Ìs  cOs`xU|iD]J}||ijot|||ƒ}q yt|||ƒWq q Xq W|SdS(N(sselfs_funcssfuncs_mainsapplysargsskwargssret(sselfsargsskwargssretsfunc((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pys__call__Õs (s__name__s __module__s__doc__sNones__init__s__call__(((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pys sequential£s ( cGst|ƒSdS(sn Handles the common case for **sequential**, in which the first function passed is the significant one. It takes free arguments instead of a single sequence argument. Example: >>> def one(): ... print "one" ... return 1 ... >>> def two(): ... print "two" ... return 2 ... >>> def three(): ... print "three" ... return 3 ... >>> one() one 1 >>> x = one() one >>> x 1 >>> x = also(one, two, three)() one two three >>> x 1 >>> x = also(two, one, three)() two one three N(s sequentialsargs(sargs((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pysalsoàs"salwayscBs tZdZd„Zd„ZRS(s~ Returns a callable which always returns a given object. Example: >>> f = always(5) >>> f() 5 >>> cCsti|ƒ||_dS(N(sFunctors__init__sselfsobjects_object(sselfsobject((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pys__init__s cOs |iSdS(N(sselfs_object(sselfsargsskwargs((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pys__call__s(s__name__s __module__s__doc__s__init__s__call__(((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pysalwayss  sany_argscBstZdZd„ZRS(s Returns a callable which will take any arguments, and ignore them, calling *func* with no arguments. Useful with map(), or in the common case of GUI event handlers which need to accept an event parameter, but do not use it. Example: >>> def return5(): ... return 5 ... >>> return5("unneeded", "unnecessary") Traceback (innermost last): File "", line 1, in ? TypeError: no arguments expected >>> f = any_args(return5) >>> f("unneeded", "unnecessary") 5 >>> cOs|iƒSdS(N(sselfs_func(sselfsargsskwargs((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pys__call__(s(s__name__s __module__s__doc__s__call__(((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pysany_argss s error_handlercBs#tZdZed„Zd„ZRS(s Takes a function, and either a function or some piece of data (or None, by default). This error_handler instance, when called, calls the first function. If any errors are raised, the second function is called, passing it sys.exc_info(). Whatever the second function returns is used as the final return value. If the second argument is not callable, then it is simply returned. If the error-handler function re-raises the exception (or another), the exception will propagate back to the caller. It is possible to chain error-handlers together with **attempt**, below. Example: >>> from operator import div >>> div(1/0) Traceback (innermost last): File "", line 1, in ? ZeroDivisionError: integer division or modulo >>> def recover(exc): ... return sys.maxint ... >>> safediv = error_handler(div, recover) >>> safediv(1, 0) 2147483647 >>> safediv2 = error_handler(div, sys.maxint) >>> safediv2(1, 0) 2147483647 cCs8ti||ƒt|ƒ o td‚n||_dS(Ns0First argument to error_handler must be callable(sFunctors__init__sselfsfuncscallables TypeErrors errorfuncs _errorfunc(sselfsfuncs errorfunc((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pys__init__Hs cOsyt|i||ƒSWnatiƒ}y,t|iƒo|i|ƒSn|iSWq{|d|d|d‚q{XnXdS(Niii( sapplysselfs_funcsargsskwargsssyssexc_infoscallables _errorfunc(sselfsargsskwargssexc_info((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pys__call__Ns  (s__name__s __module__s__doc__sNones__init__s__call__(((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pys error_handler+s  cCs>y |ƒSWn,t|ƒo|tiƒƒSq:|SnXdS(s¯ Where error_handler builds a new functor with built-in error handling capability, this function calls *func* immediately, and calls *on_error* if it is callable, passing it sys.exc_info(), or returns *on_error* if it is not callable. Example: >>> trap_error(lambda:1/0, "Yup") 'Yup' >>> trap_error(lambda:1/0, lambda x: x[1]) >>> N(sfuncscallableson_errorssyssexc_info(sfuncson_error((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pys trap_error[s   sattemptcBs tZdZd„Zd„ZRS(s Given a function sequence, attempt to call and return each in turn. The return value of the first function to successfully return is returned. Errors are silently ignored. This could be used to implement something like the pattern matching techniques used in functional languages. A common use would be attaching error handlers to functions after the fact. Suppose we want a want a version of the div() function that returns sys.maxint when we attempt to divide by zero, and zero if we attempt to divide by something other than a number as in this example: >>> def zeroHandler(exc_info): ... exc = exc_info[1] ... if isinstance(exc, ZeroDivisionError): ... return sys.maxint ... else: ... raise ... >>> safediv = with_error(div, zeroHandler) >>> safediv(1, 0) 2147483647 >>> def typeHandler(exc_info): ... exc = exc_info[1] ... if isinstance(exc, TypeError): ... return 0 ... else: ... raise ... >>> safediv = with_error(div, attempt(zeroHandler, typeHandler)) >>> safediv(1,0) 2147483647 >>> safediv(1, "abc") 0 >>> cGs?ti||dƒt|tƒ o td‚n||_dS(NisAll arguments must be callable.(sFunctors__init__sselfsfuncssallscallables TypeErrors_funcs(sselfsfuncs((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pys__init__“s cOs6x/|iD]$}yt|||ƒSWq q Xq WdS(N(sselfs_funcssfuncsapplysargsskwargs(sselfsargsskwargssfunc((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pys__call__™s  (s__name__s __module__s__doc__s__init__s__call__(((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pysattemptqs ! cCs|ddjSdS(s. Returns 1 if x is even, 0 otherwise. iiN(sx(sx((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pyseven¡scCs|djSdS(s2 Returns 1 if x is positive, 0 otherwise. iN(sx(sx((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pyspositive§scCs|djSdS(s2 Returns 1 if x is negative, 0 otherwise. iN(sx(sx((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pysnegative­scCsHx=|D]5}|o||ƒodSq<q|odSqqWdSdS(sÖ Returns 1 if for any member of a sequence, test_func returns a non-zero result. If test_func is not supplied, returns 1 if any member of the sequence is nonzero (e.g., not one of (), [], None, 0). iiN(ssequencesitems test_func(ssequences test_funcsitem((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pysanyµs   cCsJx?|D]7}|o||ƒ odSq>q| odSqqWdSdS(sÙ Returns 1 if for all members of a sequence, test_func returns a non-zero result. If test_func is not supplied, returns 1 if all members of the sequence are nonzero (i.e., not one of (), [], None, 0). iiN(ssequencesitems test_func(ssequences test_funcsitem((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pysallÃs  sô none_of(sequence, test_func = None) Returns 1 if for every element in *sequence*, test_func returns false. If test_func is not supplied, returns 0 if all members of the sequence are nonzero (i.e., not one of (), [], None, 0), and 1 otherwise. cCs |dSdS(s2 Returns the first element of a sequence. iN(ssequence(ssequence((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pysheadÙscCs |dSdS(sR Returns the slice of *sequence* containing all elements after the first. iN(ssequence(ssequence((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pystailßss BindingErrorcBstZRS(N(s__name__s __module__(((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pys BindingErrorçssBindingscBs tZdZd„Zd„ZRS(s× Binding establishes an immutable mapping between names and values. Functions which rely on normal module (or other) bindings can be accidentally sabotaged if the name is rebound. Since the bindings on a Bindings object cannot be changed, they are a somewhat safer means of providing context. >>> let = Bindings() >>> let.x = 5 >>> let.y = 10 >>> let.add_xyz = lambda z, let = let: let.x + let.y + z >>> let.add_xyz(5) 20 >>> let.x = 2 Traceback (most recent call last): File "", line 1, in ? File "functional.py", line 855, in __setattr__ raise RuntimeError, "Binding cannot be re-bound." RuntimeError: Binding cannot be re-bound. >>> cCs=|iid|ƒotd|‚n||id|sN(smapdictsbindingss__dict__(sbindings((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pys namespace s cCstt||iƒƒƒSdS(sí Much like the builtin function 'map', but works on dictionaries. *itemfunc* should be a function which takes one parameter, a (key, value) pair, and returns a new (or same) (key, value) pair to go in the dictionary. N(sdictsmapsitemfuncs dictionarysitems(sitemfuncs dictionary((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pysmapdictscCstt||iƒƒƒSdS(sÞ Filters a dictionary like 'filter' filters a list. *itemfunc* should be a function which takes two parameter, a(key, value) pair and returns 1 if the pair should be in the new dictionary, 0 otherwise. N(sdictsfiltersitemfuncs dictionarysitems(sitemfuncs dictionary((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pys filterdictscCstd„|ƒSdS(s[ Takes a dictionary and returns a new one, with the keys and values exchanged. cCs|\}}||fS(N(skeysvalue(s.0skeysvalue((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pys-sN(smapdicts dictionary(s dictionary((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pys invertdict(scOsch}t|ƒdjo|d}n|}x|D]\}}||| Takes arguments and builds a dictionary from them. The actual arguments can vary widely. A single list of (key, value) pairs, or an unlimited number of (key, value) pairs may be passed. In addition, any number of keyword arguments may be passed, which will be added to the dictionary as well. iiN(sdictslensargss listOfPairsskeysvaluesupdateskwargs(sargsskwargssdictsvalueskeys listOfPairs((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pysdict/s  cCs5|o|||‚n|o ||‚n|‚dS(s A function which allows an error to be raised within a lambda or other expression. For example: >>> f = lambda x: (x == 3 and "Three!") ... or (x == 5 and "Five!") ... or error(RuntimeError) >>> >>> f(3) 'Three!' >>> f(5) 'Five!' >>> f(6) Traceback (most recent call last): File "", line 1, in ? File "", line 1, in File "functional.py", line 874, in error raise err RuntimeError >>> N(straceserrsarg(serrsargstrace((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pyserrorAs  cGs |dSdS(sß Returns the last argument passed to it. This is useful because arguments are evaluated from left to right before a function call. This can enable "imperative" style in a lambda expression. For example: iÿÿÿÿN(sargs(sargs((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pysdodssdispatchcBs)tZdZd„Zd„Zd„ZRS(sV Given a name, find and call a method with that name on a passed-in argument. cCs||_ti|tƒdS(N(snamesselfs_namesFunctors__init__sNone(sselfsname((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pys__init__qs cCst||iƒ|_dS(N(sgetattrsobjsselfs_names_func(sselfsobj((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pysbinduscOs;|i o|i|dƒnti||d|dS(Nii(sselfs_funcsbindsargssFunctors__call__skwargs(sselfsargsskwargs((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pys__call__xs (s__name__s __module__s__doc__s__init__sbinds__call__(((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pysdispatchms   (9s__doc__stypesssysscopysstringsoperators __version__shasattrs _getframes getStackFramesnewsclosures version_infosNonesRefsBlanksblends FuncMethUnionsSimCodesFunctorswrapscurrysrcurryscomposes applycomposes joinfuncss complementsdisjoinsconjoins sequentialsalsosalwayssany_argss error_handlers trap_errorsattemptsevenspositivesnegativesanysallsnone_ofsheadstailscarscdrs StandardErrors BindingErrorsBindingss namespacesmapdicts filterdicts invertdictsdictserrorsdosdispatch(2sallscdrs filterdictsheadsdispatchsoperators error_handlersany_argssmapdictswraps BindingErrorsRefspositivescarsevenscomposes invertdicts trap_errors namespacesnegativesanyserrorsalsostails sequentialsdictsnews __version__sdosconjoins applycomposesstrings complementsdisjoins FuncMethUnionsnone_ofscopysclosuressyssattempts getStackFramesalwaysscurrys joinfuncssFunctorsSimCodesBlanksBindingssblendsrcurry((s=/mnt/gmirror/ports/devel/py-xoltar-toolkit/work/functional.pys?sx       7(  /ZˆT   = %0 0         "  #