[IPython-user] WRT %pdef and %pdoc: shortcut proposal - is there any way to get them on some 1 char shortcut?

Fernando Perez Fernando.Perez at colorado.edu
Tue Dec 27 12:44:10 CST 2005


[delayed reply, usual_apology()]

Tom Popovich wrote:
> import re
> 
> ( you do this first but you are familar with all the details of match)
> ( then we want to use the %pdef and %pdoc on re.match to look up how to use
> it... )
> ( but pdef and pdoc are a little hard to type.)
> 
> 
> How about a new: / (or maybe ?- ) operator that does the ? but throws away
> all but the pdef and pdoc parts of the ? output.
> thinking ?- means ? with a bunch thrown out as we have ? and ?? and ??? to
> give successively more detail.
> 
> I'd suggest either:
> 
> / re.match -or-
> ?- re.match
> 
> That would do both %pdef and %pdoc on re.match in our example.
> If a combo / action is bad, maybe some new 1 char shortcuts for the
> individual %pdef and %pdoc output.
> In that case, user could use either of those (to get enter the pdef or pdoc
> part he/she wanted)?

Mmh, no.  The reason is one of design simplicity for a wide user-base: ipython 
already has enough funny syntactic quirks and special chars that I know (from 
experience at conferences) that only a handful of its users even know they 
exist.  I don't want to introduce any more sub-cases of the same.

On the other hand, while I don't want to clutter the default ipython too much, 
there is a complementary aspect to ipython's design: it should be nearly 
infinitely customizable _by the users_.  I am nobody to tell you that this 
particular extension, or any other, is not the right thing for _you_, so I've 
tried to make sure that customizations of this kind are easy to implement by 
any user.

In this particular case, you could either add your own prefilter routine which 
special-cases any first (or two) chars you like and calls these functions, or 
write a simple one-letter magic of your own that does the same.

Here are some examples you can use: pysh

http://projects.scipy.org/ipython/ipython/file/ipython/trunk/IPython/Extensions/InterpreterExec.py

implements a prefilter for shell usage, and is enabled by the pysh profile 
having this line in the rc file:

import_all IPython.Extensions.InterpreterExec

If you want to go the magic function route, here's an example of how to define 
your own magics:

http://projects.scipy.org/ipython/ipython/file/ipython/trunk/doc/examples/example-magic.py

Here's what the trivial implementation would look like, I named it just 'i':
def magic_i(self, parameter_s=''):
     """ i magic command.
     """
     self.shell.magic_pdef(parameter_s)
     self.shell.magic_pdoc(parameter_s)

InteractiveShell.magic_i = magic_i
del magic_i


In use, you get this:

In [1]: import re

In [2]: i re
Object is not callable.
Minimal "re" compatibility wrapper.  See "sre" for documentation.

In [3]: i re.compile
  re.compile(pattern, flags=0)
Compile a regular expression pattern, returning a pattern object.


If you declare 'i' as a variable, you'll need to disambiguate the magic:

In [4]: i=1

In [5]: i re.compile
------------------------------------------------------------
    File "<ipython console>", line 1
      i re.compile
         ^
SyntaxError: invalid syntax


In [6]: %i re.compile
  re.compile(pattern, flags=0)
Compile a regular expression pattern, returning a pattern object.


IPython is really very, very customizable, but that doesn't mean that we 
should ship every concievable option by default, _especially_ at the level of 
special sytnax handlers.  The barrier for new magics is lower, since those are 
just functions, but I really want to keep the kinds of syntactic tricks that 
ipython plays to an absolute minimum on top of the default python language.

I hope this helps.

Cheers,


f




More information about the IPython-user mailing list