MethodChain
Paul McGuire
ptmcg at austin.rr.com
Sun Jul 20 01:42:04 EDT 2008
On Jul 20, 12:01 am, Marc 'BlackJack' Rintsch <bj_... at gmx.net> wrote:
> The methods are a problem IMHO. You can't add an own method/function with
> the name `fire()` or `toFunction()`. `MethodChain` has to know all
> functions/methods in advance. You can add the methods of whole classes at
> once and there are over 300 pre-added, this begs for name clashes.
>
> Ciao,
> Marc 'BlackJack' Rintsch
If you shift the syntax just a bit, instead of writing a.b.c, pass a,
b, and c as the args to a MethodChain object. Here's a rough stab at
the problem:
class MethodChain(object):
def __init__(self, *fns):
self.fns = fns[:]
def __call__(self,*args):
if self.fns:
for f in self.fns:
args = (f(*args),)
return args[0]
def dncase(s):
return s.lower()
def upcase(s):
return s.upper()
def stripVowels(s):
return "".join( c for c in s if c not in "aeiou" )
def selectItems(items,s):
return "".join(c for i,c in enumerate(s) if i in items)
from functools import partial
chn = MethodChain(
dncase,
stripVowels,
upcase,
partial(selectItems,(0,2))
)
print chn("FoO Bar")
-- Paul
More information about the Python-list
mailing list