[IPython-dev] IPyAutocall-like dispatching with simplegeneric

Ville M. Vainio vivainio at gmail.com
Wed May 16 11:24:13 EDT 2007


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')

@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.

-- 
Ville M. Vainio - vivainio.googlepages.com
blog=360.yahoo.com/villevainio - g[mail | talk]='vivainio'



More information about the IPython-dev mailing list