[IPython-dev] Re: [IPython-user] Evaluation of arbitrary expressions in the prompt

Fernando Perez Fernando.Perez at colorado.edu
Wed Sep 14 10:59:20 CDT 2005

Frédéric Mantegazza wrote:
> Le Mercredi 14 Septembre 2005 13:13, Michael Scarpa  string a écrit :
>>I just found out that this behavior disappeared when using the most
>>recent version of IPython. Sorry for the fuzz. Still, I would like to
>>know if doing this using the prompt string is the best way, or if there
>>is a more feasible way to achieve what I need.
> If your goal is just to have a method executed, and don't need to display 
> anything in the prompt, then it could be nice to have a special hook for 
> that (even if the prompt way is working).
> Fernando, what about 2 additional hooks : one to execute arbitrary code 
> *before* the input string is executed, and one *after* ?

They already exist :)  They are called:

In [3]: __IP.prefilter?
Type:           instancemethod
Base Class:     <type 'instancemethod'>
String Form:    <bound method InteractiveShell._prefilter of 
<IPython.iplib.InteractiveShell instance at 0x40061e8c>>
Namespace:      Interactive
File:           /usr/local/home/fperez/code/python/IPython/iplib.py
Definition:     __IP.prefilter(self, line, continue_prompt)
     Calls different preprocessors, depending on the form of line.

In [4]: __IP.outputcache.display?
Type:           instancemethod
Base Class:     <type 'instancemethod'>
String Form:    <bound method CachedOutput._display of 
<IPython.Prompts.CachedOutput instance at 0x403c0a2c>>
Namespace:      Interactive
File:           /usr/local/home/fperez/code/python/IPython/Prompts.py
Definition:     __IP.outputcache.display(self, arg)
     Default printer method, uses pprint.

     This can be over-ridden by the users to implement special formatting
     of certain types of output.

Display isn't very well documented (prefilter is, since the pysh and other 
examples show how to use it), but it's there.

However, I think that for Michael's needs, using the dynamic prompt system is 
simpler, since he doesn't need to subclass or override anything.

> An interesting things could be to analyse input code, and maybe modify it 
> before having ipython executing it. The same for output: one could 
> check/modify the answer before ipython prints it (the way it is done for 
> exceptions hook).

Have a look at the pysh, tutorial or physics profiles :)



More information about the IPython-user mailing list