[IPython-dev] Magics, aliases, namespaces

Ville Vainio vivainio at kolumbus.fi
Mon Jun 28 21:17:47 EDT 2004


Current ipython seems to "special case" lots of stuff - aliases, magics 
etc. It would seem to yield itself to a simpler and more extensible 
architecture, consisting of multple dictionaries, where just the order 
of search is determined by profile/prefix. All the functionality would 
just be "pushed" to ipython core, i.e. the objects that represent the 
functionality are instantiated on "plugins" and they are registered with 
ipython core. i.e.

def magic_f(options, args):
  pass

m = MyMagic(magic_f)
m.arghandler = stockhandlers.OptParseArgHandler
m.options = .... # optparse options, for easy passing to magic_f and 
help display

# note that arghandler takes a peek at the instance, sees options, and 
proceeds
# accordingly. Various arg handlers can ask for arbitrary options.
# some handler might optionally glob the filenames, search data folders 
for filenames etc.

ipythonregistry.register(category = "magic", name = "mymagic", obj = m)

I bet most of the current functionality could easily be added to such a 
registry by just instantiating e.g. "Magic" class and registering all 
the functions with some sensible default args. Supporting legacy stuff 
in general would be easy - just implement new handlers (arg and 
otherwise) for new stuff, and have the old handlers around forever / as 
long as is deemed appropriate. The 'python' namespace (locals() + 
globals()) should be special, of course.

It should be easy to have arbitrary number of "categories" (like 
'magic', 'shellcommand','projectspecific_myproject', 
'projectspecific_otherproject'). It would only influence the order in 
which the completions are suggested, and in case of name collision which 
one is selected. Also, I think all completions should be shown, even the 
ones in "later" categories in the case of a match in an "earlier" category.

The "functionality object" might also have a callable object 
'expandarg', and ipython would run it (with the arg index) when tab 
completion is attempted after typing the function name. It would return 
the possible completions for that particular command... or None to 
"revert to default file completions". Such functionality could be useful 
in making ipython an "operating console" of a sort. I'm talking about:

 >> lscat reactor   # list commands in category - reactor is "project 
specific" category

r_operate


 >> r_operate <tab>
start   shutdown  notify_meltdown   evacuate

 >> r_operate shutdown <tab>

1 2 5 6     # note that 3 and 4 are already shut down

 >> r_operate shutdown 2

Shutting down..
ok.

 >> r_operate start <tab>

2 3 4      # 2 was shut down, can be started now

 >> r_operate start 2

Starting....
ok.

I'm talking about having a super-configurable man-machine language here! 
Like cmd.Cmd on steroids, as a free addition to ipython!

Sorry for the ramblings - it's 4:15 in Finland, and I had to get some 
codethink out of me. Recently started summer holiday has deprived me of 
the usual dose of code-excercise...

I guess it's time to hit the bed :-).




More information about the IPython-dev mailing list