[IPython-dev] what is the best way of doing pretty print with ipython
Ondrej Certik
ondrej at certik.cz
Mon Jan 7 09:02:48 EST 2008
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.
OK, how to fix this? My idea is to use repr() just for the 1D
printing, like in [3] and force ipython to use maybe str()? Or our
custom method.
What would you recommend as the best way to approach this in ipython?
Thanks,
Ondrej
More information about the IPython-dev
mailing list