[IPython-dev] Towards IPython 1.0, the famous big cleanup

Ville Vainio vivainio at kolumbus.fi
Mon May 16 16:12:38 CDT 2005


On Mon, 2005-05-16 at 12:17 -0600, Fernando Perez wrote:

> If I stick all that into a module and statically load that, how do I allow 
> users to define their own, add them at runtime, load them via profiles, etc? 
> It seems far easier to expose a single make_magic() function (or some similar 
> simple API) which takes care of stuffing whatever is needed into a dict.

make_magic can inject the name in the magic module in a similar fashion.

> exception.  I can't think of any better data structure than a dict for this 
> kind of efficient runtime search (esp. because the C implementation of dicts 
> has a bunch of nifty fastpaths for string-keyed dicts, which mean this 
> approach will work fast and clean).

Module lookup is as fast because, well, it *is* a dict lookup, even if
it happens via getattr :-).

> I think we're basically saying the same thing (or almost)...  I'm just 
> thinking of a particular implementation strategy which seems simple to the 
> user, yet provides the efficiency needed for this component.

IMO it's more natural for the advanced user to be able to attach new
modules to the search order. Modules have the advantage that they quite
naturally have a 'name', which is an artificial addition to the dicts,
if done. 

The naive user will know nothing about this, of course. He will just
inject stuff into the 'user_magics' module through 'make_magic'
function/decorator. But there would not be specific place where all the
magics/aliases reside, just a bunch of modules in a list representing
the search order (and subsequently precedence). For example it would be
possible to "unload" a module this way by removing in from the search
list, exposing names from subsequent modules that had a name collision
before.

It's my general hunch that the module approach might be simpler and more
flexible/extensible in the long run. It's slightly slower if the search
list is long, but that's going to be rare and it's a minimal performance
it, considering that every global variable lookup is a dict lookup as
well.




More information about the IPython-dev mailing list