[IPython-user] Hooking into the autocall displayer

Tom Denniston tom.denniston@alum.dartmouth....
Mon Sep 17 16:29:27 CDT 2007


My apologies Fernando.  Meant to reply to all.

Jorgen, here is a better description.  Sorry for being unclear in my first post:

I have some functions that are really shortcuts for other functions
and I want this to be painfully obvious to the person using IPython.
IPython achieves a similar effect internally when you do something
like this:

In [1]: map str, [1,2,3]
------> map(str, [1,2,3])
Out[1]: ['1', '2', '3']

It tells you, yah i'll execute map str, [1,2,3] but I am translating
it into map(str, [1,2,3]).

I like the way the green arrow functions and want to match it for consistency.

So Fernando told me how it is generated on linux.  You simply call
auto_rewrite on a Prompt1 object.  I found from a little grepping that
the sys.displayhook in ipython is actually the CachedOutput that owns
that Prompt1 instance that I want to use.  There is probably a better
way to reference it, but this worked.

When I use this and append my string which is the rewritten command
it, on linux I simply send it to standard out and all is well:

In [43]: def shortCutFunc(s):
   ....:
sys.stdout.write(sys.displayhook.prompt1.auto_rewrite()+ 'print
%r\n'%s)
   ....:     print s
   ....:     sys.stdout.write(sys.displayhook.prompt1.auto_rewrite()+
'print repr(%r)\n'%s)
   ....:     print repr(s)
   ....:

In [44]: shortCutFunc('string')
-------> print 'string'
string
-------> print repr('string')
'string'


On windows this same code fails miserably because the escape sequences
don't get translated properly to colors:

In [10]: def shortCutFunc(s):
   ....:      sys.stdout.write(sys.displayhook.prompt1.auto_rewrite()+ 'print %r
\n'%s)
   ....:  print s
   ....:  sys.stdout.write(sys.displayhook.prompt1.auto_rewrite()+ 'print repr(%
r)\n'%s)
   ....:  print repr(s)
   ....:

In [11]: shortCutFunc('s')
←[0;32m-------> ←[0mprint 's'
s
←[0;32m-------> ←[0mprint repr('s')
's'



I suspect my sys.stdout.write is the problem but I don't know the
correct cross platform alternative.


Is this clear?


On 9/17/07, Jörgen Stenarson <jorgen.stenarson@bostream.nu> wrote:
> Tom,
>
> can you provide a complete self contained example of what you are trying
> to do? With a description of desired results. It would be helpful in
> figuring out what goes wrong.
>
> /Jörgen
>
> > In particular, in this case I don't really know what the problem is on
> > Win32, since I know very little about the handling of color escapes on
> > that platform.  I hope our resident win32 experts may be able to help.
> >
> > Cheers,
> >
> >
> > f
> > _______________________________________________
> > IPython-user mailing list
> > IPython-user@scipy.org
> > http://lists.ipython.scipy.org/mailman/listinfo/ipython-user
> >
>
> _______________________________________________
> IPython-user mailing list
> IPython-user@scipy.org
> http://lists.ipython.scipy.org/mailman/listinfo/ipython-user
>


More information about the IPython-user mailing list