[IPython-dev] getdoc hook
Fernando Perez
Fernando.Perez at colorado.edu
Fri Apr 8 05:24:54 EDT 2005
Frédéric Mantegazza wrote:
> Hello,
>
> I found a way to get Pyro remote object docstrings :o) So I'm trying to
> implement this in IPython. But I have some questions:
>
> 1) (not really a IPython specific question) As I have to modify a little bit
> the OInspect.getdoc() function, I'm trying to rebind this function to mine,
> but it does not work :o( How can I do this ? Here is my code:
>
> import PyMAD.client.ipython.IPython.OInspect
>
> def getdoc(obj):
> ....
>
> PyMAD.client.ipython.IPython.OInspect.getdoc = getdoc
>
> Where am I wrong ?
I'm not really sure why it's not working, what you are doing looks perfectly
reasonable to me.
> Fernando, could it be possible to add a hook to rebind this function in a
> better way ?
Well, this is not really an ipython internal method which is hookable, it's
just a plain function in a library (the OInspect module). So what you are
doing is about as simple a rebind as I can imagine (though I don't understand
why it's failing in your case, sorry).
> It would also be nice to have a hook to change the whole
> getdoc behaviour. I mean, I would like to use my own Inspector class (at
> least to be able to overide some parts of it).
Well, you can use your own inspector simply by setting:
__IPYTHON__.inspector = your_inspector_instance
> 2) Why is the getdoc() function always called, even when I don't use the
> 'obj?' syntax ?
Because in the input evaluation loop, while I find what you typed (to make
auto-execution decisions and the like), once the object is found I fish out in
passing the docstring. A lot of the machinery that does many of the nice
things in ipython needs to know quite a bit about the objects referenced by
names at the command line, so I tried to centralize this collection of
information in one place. The actual routine is _ofind() in Magic.py (that
probably belongs somewhere else like iplib.py, really).
> 3) Is it possible make difference in 'obj.?' and 'obj.??' syntaxes at the
> getdoc() level ? If not, where is it handled ?
No. getdoc() gets passed a true object, not the input line which the user
typed. By the time getdoc is called, we've long forgotten the user input. If
you need to mess with that kind of thing, you need to put traps very early on
in the line processing, that's what custom prefilters are for. Note that
obj.? doesn't work:
In [2]: code.?
Object `code.` not found.
because the dot is taken as part of the name. If you want things to work with
that dot, you'll have to do custom handling at the prefilter level, since
'foo.' can never be a valid python object name.
Best,
f
More information about the IPython-dev
mailing list