[IPython-dev] what is the best way of doing pretty print with ipython

Ondrej Certik ondrej@certik...
Mon Jan 7 08:05:48 CST 2008


On Jan 7, 2008 3:02 PM, Ondrej Certik <ondrej@certik.cz> wrote:
> Hi,
>
> in SymPy, we use ipython as an easy to use shell and generally it
> works incredibly well:
>
> $ bin/isympy
> Python 2.4.4 console for SymPy 0.5.11-hg. These commands were executed:
> >>> from __future__ import division
> >>> from sympy import *
> >>> x, y, z = symbols('xyz')
> >>> k, m, n = symbols('kmn', integer=True)
> >>> f = Function("f")
> >>> Basic.set_repr_level(2)     # pretty print output; Use "1" for python output
> >>> pprint_try_use_unicode()    # use unicode pretty print when available
>
> Documentation can be found at http://sympy.org/
>
>
> In [1]: integr
> integrals  integrate
>
> In [1]: integrate(x**2 * cos(x), x)
> Out[1]:
>              2
> -2*sin(x) + x *sin(x) + 2*x*cos(x)
>
> In [2]: x**2
> Out[2]:
>  2
> x
>
> In [3]:
>
>
> However, we are abusing python repr() function as shown here:
>
> In [3]: repr(x**2)
> Out[3]: ' 2\nx '
>
> In [4]: str(x**2)
> Out[4]: 'x**2'
>
> The repr() should return just one line and be something that can be
> "almost" parsed back to python.
>
> We can control this behavior:
>
> In [1]: x**2
> Out[1]:
>  2
> x
>
> In [2]: Basic.set_repr_level(1)
> Out[2]: 2
>
> In [3]: x**2
> Out[3]: x**2
>
>
> But it doesn't help always, consider:
>
> In [1]: (x**2, x**3, y)
> Out[1]:
> ( 2
> x ,  3
> x , y)
>
> In [2]: Basic.set_repr_level(1)
> Out[2]: 2
>
> In [3]: (x**2, x**3, y)
> Out[3]: (x**2, x**3, y)
>
>
> the [3] is fine, but [1] is completely messed up.

BTW, here we discovered a more serious issue with ipython and repr
returning several lines with unicode:

http://code.google.com/p/sympy/issues/detail?id=484

Is the problem in ipython or sympy? I guess there is some bug in
ipython too, because it messes up the terminal when I return from
ipython.
So whatever broken program ipython executes (sympy), it should return
the terminal in a sane state.

Ondrej


More information about the IPython-dev mailing list