[IPython-dev] IPyAutocall-like dispatching with simplegeneric

Walter Dörwald walter at livinglogic.de
Thu May 17 05:25:59 EDT 2007


Ville M. Vainio wrote:

> Introduction of IPyAutocall made me think that we could make the
> 'command dispatching' more 'generic' (pun not intended) by harnessing
> simplegeneric, already introduced to IPython by Walter
> (external/simplegeneric.py).
> 
> http://cheeseshop.python.org/pypi/simplegeneric
> 
> That is, if the command part (iFun) is an object in user_ns that has
> association created to a generic function, e.g. ipycmd, it would
> always be called with specified arguments. This would avoid the need
> to subclass IPyAutocall and override __call__, just tag the function
> as generic and be done with it.
> 
> Something like (liberally copy-pasting from simplegeneric home page):
> 
>> @generic
> ... def ipycmd(item, ip, iFun, rest):
> ...     """Default implementation goes here"""
> ...     print "we don't really want a default implementation..."
> 
> 
> OScmdmarker = object()
> 
> @ipycmd.when_object(OScmdmarker)
> def callsyscmd(item, ip,iFun, rest):
>   ip.system(iFun+' ' + rest)
> 
> cp = OScmdmarker
> mv = OScmdmarker
> ip.to_user_ns('cp mv')

I don't see how callsyscmd() is supposed to distinguish between cp and 
mv commands. Both point to the same object.

> @ipycmd.when_type(Macro):
> def callmacro(item, ip, iFun, rest):
>   ip.user_ns['_margv'] = rest
>   ip.runlines(item.value)
> 
> I guess at least Walter might want to chip in, I have never actually
> used this simplegeneric myself.

As I see it simplegeneric is especially useful when the code is 
developed by three different parties: The one that develops the generic 
function, the one that develops the application classes and the one that 
develops the special implementation of the generic function for the 
application classes. However that's not the case here with IPython. I 
don't think that e.g. the Macro class is developed independently from 
IPython.

Servus,
    Walter




More information about the IPython-dev mailing list