By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,767 Members | 1,030 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,767 IT Pros & Developers. It's quick & easy.

MethodChain

P: n/a
Found from Reddit, it's for e ECMA(Java)Script, but something similar
may be useful for Python too:

http://jsclass.jcoglan.com/methodchain.html
http://blog.jcoglan.com/2008/07/16/w...rself-clearly/

Bye,
bearophile
Jul 19 '08 #1
Share this Question
Share on Google+
5 Replies

P: n/a
On Sat, 19 Jul 2008 08:55:23 -0700, bearophileHUGS wrote:
Found from Reddit, it's for e ECMA(Java)Script, but something similar
may be useful for Python too:

http://jsclass.jcoglan.com/methodchain.html
http://blog.jcoglan.com/2008/07/16/w...rself-clearly/
What's called `MethodChain` there seems to be function composition in
functional languages. Maybe `functools` could grow a `compose()` function.

Ciao,
Marc 'BlackJack' Rintsch
Jul 19 '08 #2

P: n/a
Marc 'BlackJack' Rintsch:
What's called `MethodChain` there seems to be function composition in
functional languages. Maybe `functools` could grow a `compose()` function.
To me it looks like a quite more "refined" thing, it's an object, it
has some special methods, etc. I think it's not too much difficult to
implement it with Python.

Bye,
bearophile
Jul 19 '08 #3

P: n/a
On Sat, 19 Jul 2008 13:57:33 -0700, bearophileHUGS wrote:
Marc 'BlackJack' Rintsch:
>What's called `MethodChain` there seems to be function composition in
functional languages. Maybe `functools` could grow a `compose()` function.

To me it looks like a quite more "refined" thing, it's an object, it
has some special methods, etc. I think it's not too much difficult to
implement it with Python.
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
Jul 20 '08 #4

P: n/a
On Jul 20, 12:01*am, Marc 'BlackJack' Rintsch <bj_...@gmx.netwrote:
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 classesat
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
Jul 20 '08 #5

P: n/a
Name clashes aren't an issue, since MethodChain doesn't apply any
special meaning to the method names it knows; the limitation is
because JavaScript doesn't allow you to modify property lookup
behavior. *And since we can make the chain object callable, we don't
need "fire" or "toFunction" methods.
I'm the author of MethodChain, so just thought I'd confirm the above
statement. All MethodChain does is store method calls so they can
later be replayed on any object. All methods in MethodChain simply add
their name and arguments to an array inside the MethodChain instance,
they don't implement any concrete functionality. All that's important
is the names of the methods -- the object the chain is fired on will
decide how to handle those calls itself, so naming clashes aren't a
problem. For example:

var chain = it().toLowerCase().split('-').map(function() {...});
chain.fire('my-String');

is the same as

'my-String'.toLowerCase().split('-').map(function() {...});

So split() gets called on 'my-string', map() gets called on ['my',
'string']. The methods 'fire' and 'toFunction' are a problem but I
can't see any way around having them in JavaScript -- you need some
way of getting the method list out of the chain object. if JavaScript
had method_missing, we wouldn't need to tell MethodChain about names
in advance either.
Jul 20 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.