473,396 Members | 2,011 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

MethodChain

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
5 916
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.