Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use srepr instead of sstr for __repr__ printing #39

Merged
merged 9 commits into from
Jan 17, 2016
13 changes: 13 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,16 @@ def check_disabled(request):
if (V(pytest.__version__) < '2.6.3' and
pytest.config.getvalue('-s') != 'no'):
pytest.skip("run py.test with -s or upgrade to newer version.")


@pytest.fixture(autouse=True, scope='session')
def set_displayhook():
import sys
from sympy import init_printing

# hook our nice, hash-stable strprinter
init_printing(pretty_print=False, use_unicode=False)

# doctest restore sys.displayhook from __displayhook__,
# see https://bugs.python.org/issue26092.
sys.__displayhook__ = sys.displayhook
2 changes: 1 addition & 1 deletion docs/modules/logic.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Like most types in SymPy, Boolean expressions inherit from
:class:`~sympy.core.basic.Basic`::

>>> (y & x).subs({x: True, y: True})
True
true
>>> (x | y).atoms() == {x, y}
True

Expand Down
4 changes: 2 additions & 2 deletions docs/tutorial/gotchas.rst
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ you don't have to worry about this problem:
Rational.

>>> x = Symbol('x')
>>> print(solve(7*x - 22, x))
>>> solve(7*x - 22, x)
[22/7]
>>> 22/7 # After copy and paste we get a float
3.142857142857143
Expand Down Expand Up @@ -449,7 +449,7 @@ unsimplified trig identity, multiplied by a big number:
>>> big = 12345678901234567890
>>> big_trig_identity = big*cos(x)**2 + big*sin(x)**2 - big*1
>>> abs(big_trig_identity.subs(x, .1).n(2)) > 1000
True
true

When the `\cos` and `\sin` terms were evaluated to 15 digits of precision and
multiplied by the big number, they gave a large number that was only
Expand Down
28 changes: 14 additions & 14 deletions docs/tutorial/manipulation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@ Understanding Expression Trees
Before we can do this, we need to understand how expressions are represented
in SymPy. A mathematical expression is represented as a tree. Let us take
the expression `x^2 + xy`, i.e., ``x**2 + x*y``. We can see what this
expression looks like internally by using ``srepr``
expression looks like internally by using ``repr``

>>> from sympy import *
>>> x, y, z = symbols('x y z')

>>> expr = x**2 + x*y
>>> srepr(expr)
>>> repr(expr)
"Add(Pow(Symbol('x'), Integer(2)), Mul(Symbol('x'), Symbol('y')))"

The easiest way to tear this apart is to look at a diagram of the expression
tree:

.. This comes from dotprint(x**2 + x*y, labelfunc=srepr)
.. This comes from dotprint(x**2 + x*y, labelfunc=repr)

.. graphviz::

Expand Down Expand Up @@ -81,15 +81,15 @@ integers. It is similar to the Python built-in type ``int``, except that
When we write ``x**2``, this creates a ``Pow`` object. ``Pow`` is short for
"power".

>>> srepr(x**2)
>>> repr(x**2)
"Pow(Symbol('x'), Integer(2))"

We could have created the same object by calling ``Pow(x, 2)``

>>> Pow(x, 2)
x**2

Note that in the ``srepr`` output, we see ``Integer(2)``, the SymPy version of
Note that in the ``repr`` output, we see ``Integer(2)``, the SymPy version of
integers, even though technically, we input ``2``, a Python int. In general,
whenever you combine a SymPy object with a non-SymPy object via some function
or operation, the non-SymPy object will be converted into a SymPy object. The
Expand All @@ -104,7 +104,7 @@ We have seen that ``x**2`` is represented as ``Pow(x, 2)``. What about
``x*y``? As we might expect, this is the multiplication of ``x`` and ``y``.
The SymPy class for multiplication is ``Mul``.

>>> srepr(x*y)
>>> repr(x*y)
"Mul(Symbol('x'), Symbol('y'))"

Thus, we could have created the same object by writing ``Mul(x, y)``.
Expand All @@ -124,13 +124,13 @@ SymPy expression trees can have many branches, and can be quite deep or quite
broad. Here is a more complicated example

>>> expr = sin(x*y)/2 - x**2 + 1/y
>>> srepr(expr)
>>> repr(expr)
"Add(Mul(Integer(-1), Pow(Symbol('x'), Integer(2))), Mul(Rational(1, 2),
sin(Mul(Symbol('x'), Symbol('y')))), Pow(Symbol('y'), Integer(-1)))"

Here is a diagram

.. dotprint(sin(x*y)/2 - x**2 + 1/y, labelfunc=srepr)
.. dotprint(sin(x*y)/2 - x**2 + 1/y, labelfunc=repr)

.. graphviz::

Expand Down Expand Up @@ -189,10 +189,10 @@ class in SymPy. ``x - y`` is represented as ``x + -y``, or, more completely,
``x + -1*y``, i.e., ``Add(x, Mul(-1, y))``.

>>> expr = x - y
>>> srepr(x - y)
>>> repr(x - y)
"Add(Symbol('x'), Mul(Integer(-1), Symbol('y')))"

.. dotprint(x - y, labelfunc=srepr)
.. dotprint(x - y, labelfunc=repr)

.. graphviz::

Expand Down Expand Up @@ -229,10 +229,10 @@ What if we had divided something other than 1 by ``y``, like ``x/y``? Let's
see.

>>> expr = x/y
>>> srepr(expr)
>>> repr(expr)
"Mul(Symbol('x'), Pow(Symbol('y'), Integer(-1)))"

.. dotprint(x/y, labelfunc=srepr)
.. dotprint(x/y, labelfunc=repr)

.. graphviz::

Expand Down Expand Up @@ -272,7 +272,7 @@ numbers are always combined into a single term in a multiplication, so that
when we divide by 2, it is represented as multiplying by 1/2.

Finally, one last note. You may have noticed that the order we entered our
expression and the order that it came out from ``srepr`` or in the graph were
expression and the order that it came out from ``repr`` or in the graph were
different. You may have also noticed this phenonemon earlier in the
tutorial. For example

Expand Down Expand Up @@ -381,7 +381,7 @@ Mul's ``args`` are sorted, so that the same ``Mul`` will have the same
``args``. But the sorting is based on some criteria designed to make the
sorting unique and efficient that has no mathematical significance.

The ``srepr`` form of our ``expr`` is ``Mul(3, x, Pow(y, 2))``. What if we
The ``repr`` form of our ``expr`` is ``Mul(3, x, Pow(y, 2))``. What if we
want to get at the ``args`` of ``Pow(y, 2)``. Notice that the ``y**2`` is in
the third slot of ``expr.args``, i.e., ``expr.args[2]``.

Expand Down
12 changes: 2 additions & 10 deletions docs/tutorial/printing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ repr

The repr form of an expression is designed to show the exact form of an
expression. It will be discussed more in the :ref:`tutorial-manipulation`
section. To get it, use ``srepr()`` [#srepr-fn]_.
section. To get it, use ``repr()``.

>>> srepr(Integral(sqrt(1/x), x))
>>> repr(Integral(sqrt(1/x), x))
"Integral(Pow(Pow(Symbol('x'), Integer(-1)), Rational(1, 2)), Tuple(Symbol('x')))"

The repr form is mostly useful for understanding how an expression is built
Expand Down Expand Up @@ -177,11 +177,3 @@ The ``dotprint()`` function in ``sympy.printing.dot`` prints output to dot
format, which can be rendered with Graphviz. See the
:ref:`tutorial-manipulation` section for some examples of the output of this
printer.

.. rubric:: Footnotes

.. [#srepr-fn] SymPy does not use the Python builtin ``repr()`` function for
repr printing, because in Python ``str(list)`` calls ``repr()`` on the
elements of the list, and some SymPy functions return lists (such as
``solve()``). Since ``srepr()`` is so verbose, it is unlikely that anyone
would want it called by default on the output of ``solve()``.
2 changes: 1 addition & 1 deletion sympy/calculus/finite_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def finite_diff_weights(order, x_list, x0=Integer(0)):
>>> from sympy.calculus import finite_diff_weights
>>> N, (h, x) = 4, symbols('h x')
>>> x_list = [x+h*cos(i*pi/(N)) for i in range(N,-1,-1)] # chebyshev nodes
>>> print(x_list)
>>> x_list
[-h + x, -sqrt(2)*h/2 + x, x, sqrt(2)*h/2 + x, h + x]
>>> mycoeffs = finite_diff_weights(1, x_list, 0)[1][4]
>>> [simplify(c) for c in mycoeffs] #doctest: +NORMALIZE_WHITESPACE
Expand Down
5 changes: 2 additions & 3 deletions sympy/categories/diagram_drawing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1347,9 +1347,8 @@ def __str__(self):
>>> g = NamedMorphism(B, C, "g")
>>> diagram = Diagram([f, g])
>>> grid = DiagramGrid(diagram)
>>> print(grid)
[[Object("A"), Object("B")],
[None, Object("C")]]
>>> grid
[[Object('A'), Object('B')], [None, Object('C')]]

"""
return repr(self._grid._array)
Expand Down
3 changes: 1 addition & 2 deletions sympy/categories/tests/test_drawing.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,7 @@ def test_DiagramGrid():
assert grid.morphisms == {f: FiniteSet(), g: FiniteSet(), h: FiniteSet(),
k: FiniteSet()}

assert str(grid) == '[[Object("A"), Object("B"), Object("D")], ' \
'[None, Object("C"), None]]'
assert str(grid) == "[[Object('A'), Object('B'), Object('D')], [None, Object('C'), None]]"

# A chain of morphisms.
f = NamedMorphism(A, B, "f")
Expand Down
4 changes: 2 additions & 2 deletions sympy/combinatorics/partitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ def __le__(self, other):
>>> a <= a
True
>>> a <= b
True
true
"""
return self.sort_key() <= sympify(other).sort_key()

Expand All @@ -180,7 +180,7 @@ def __lt__(self, other):
>>> a.rank, b.rank
(9, 34)
>>> a < b
True
true
"""
return self.sort_key() < sympify(other).sort_key()

Expand Down
6 changes: 3 additions & 3 deletions sympy/combinatorics/polyhedron.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,13 +256,13 @@ def __new__(cls, corners, faces=[], pgroup=[]):
represent it in a way that helps to visualize the Rubik's cube.

>>> from sympy.utilities.iterables import flatten, unflatten
>>> from sympy import symbols
>>> from sympy import symbols, sstr
>>> from sympy.combinatorics import RubikGroup
>>> facelets = flatten([symbols(s+'1:5') for s in 'UFRBLD'])
>>> def show():
... pairs = unflatten(r2.corners, 2)
... print(pairs[::2])
... print(pairs[1::2])
... print(sstr(pairs[::2]))
... print(sstr(pairs[1::2]))
...
>>> r2 = Polyhedron(facelets, pgroup=RubikGroup(2))
>>> show()
Expand Down
4 changes: 2 additions & 2 deletions sympy/concrete/delta.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,9 +264,9 @@ def deltasummation(f, limit, no_piecewise=False):
>>> deltasummation(KroneckerDelta(i, k), (k, -oo, oo))
1
>>> deltasummation(KroneckerDelta(i, k), (k, 0, oo))
Piecewise((1, 0 <= i), (0, True))
Piecewise((1, 0 <= i), (0, true))
>>> deltasummation(KroneckerDelta(i, k), (k, 1, 3))
Piecewise((1, And(1 <= i, i <= 3)), (0, True))
Piecewise((1, And(1 <= i, i <= 3)), (0, true))
>>> deltasummation(k*KroneckerDelta(i, j)*KroneckerDelta(j, k), (k, -oo, oo))
j*KroneckerDelta(i, j)
>>> deltasummation(j*KroneckerDelta(i, j), (j, -oo, oo))
Expand Down
2 changes: 1 addition & 1 deletion sympy/concrete/summations.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class Sum(AddWithLimits,ExprWithIntLimits):
>>> Sum(x**k, (k, 0, oo))
Sum(x**k, (k, 0, oo))
>>> Sum(x**k, (k, 0, oo)).doit()
Piecewise((1/(-x + 1), Abs(x) < 1), (Sum(x**k, (k, 0, oo)), True))
Piecewise((1/(-x + 1), Abs(x) < 1), (Sum(x**k, (k, 0, oo)), true))
>>> Sum(x**k/factorial(k), (k, 0, oo)).doit()
E**x

Expand Down
4 changes: 2 additions & 2 deletions sympy/core/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,8 +363,8 @@ def dummy_eq(self, other, symbol=None):
# Note, we always use the default ordering (lex) in __str__ and __repr__,
# regardless of the global setting. See issue 5487.
def __repr__(self):
from sympy.printing import sstr
return sstr(self, order=None)
from sympy.printing import srepr
return srepr(self, order=None)

def __str__(self):
from sympy.printing import sstr
Expand Down
2 changes: 1 addition & 1 deletion sympy/core/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -1047,7 +1047,7 @@ def args_cnc(self, cset=False, warn=True, split_1=True):
>>> (-2*x*y).args_cnc()
[[-1, 2, x, y], []]
>>> (-2.5*x).args_cnc()
[[-1, 2.50000000000000, x], []]
[[-1, 2.5, x], []]
>>> (-2*x*A*B*y).args_cnc()
[[-1, 2, x, y], [A, B]]
>>> (-2*x*A*B*y).args_cnc(split_1=False)
Expand Down
14 changes: 7 additions & 7 deletions sympy/core/numbers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2693,7 +2693,7 @@ class NaN(Number, metaclass=Singleton):
>>> nan + 1
nan
>>> Eq(nan, nan) # mathematical equality
False
false
>>> nan == nan # structural equality
True

Expand Down Expand Up @@ -3099,7 +3099,7 @@ class Pi(NumberSymbol, metaclass=Singleton):
>>> S.Pi
pi
>>> pi > 3
True
true
>>> pi.is_irrational
True
>>> x = Symbol('x')
Expand Down Expand Up @@ -3160,7 +3160,7 @@ class GoldenRatio(NumberSymbol, metaclass=Singleton):

>>> from sympy import S
>>> S.GoldenRatio > 1
True
true
>>> S.GoldenRatio.expand(func=True)
1/2 + sqrt(5)/2
>>> S.GoldenRatio.is_irrational
Expand Down Expand Up @@ -3223,9 +3223,9 @@ class EulerGamma(NumberSymbol, metaclass=Singleton):
>>> from sympy import S
>>> S.EulerGamma.is_irrational
>>> S.EulerGamma > 0
True
true
>>> S.EulerGamma > 1
False
false

References
==========
Expand Down Expand Up @@ -3275,9 +3275,9 @@ class Catalan(NumberSymbol, metaclass=Singleton):
>>> from sympy import S
>>> S.Catalan.is_irrational
>>> S.Catalan > 0
True
true
>>> S.Catalan > 1
False
false

References
==========
Expand Down
6 changes: 3 additions & 3 deletions sympy/core/relational.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,15 +261,15 @@ class Equality(Relational):
>>> Eq(y, x + x**2)
Eq(y, x**2 + x)
>>> Eq(2, 5)
False
false
>>> Eq(2, 5, evaluate=False)
Eq(2, 5)
>>> _.doit()
False
false
>>> Eq(exp(x), exp(x).rewrite(cos))
Eq(E**x, sinh(x) + cosh(x))
>>> simplify(_)
True
true

See Also
========
Expand Down
2 changes: 1 addition & 1 deletion sympy/core/tests/test_containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def test_Dict():
assert set(d.items()) == {Tuple(x, Integer(1)), Tuple(y, Integer(2)), Tuple(z, Integer(3))}
assert set(d) == {x, y, z}
assert str(d) == '{x: 1, y: 2, z: 3}'
assert d.__repr__() == '{x: 1, y: 2, z: 3}'
assert d.__repr__() == "Dict(Tuple(Symbol('x'), Integer(1)), Tuple(Symbol('y'), Integer(2)), Tuple(Symbol('z'), Integer(3)))"

# Test creating a Dict from a Dict.
d = Dict({x: 1, y: 2, z: 3})
Expand Down
4 changes: 2 additions & 2 deletions sympy/core/tests/test_symbol.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from sympy import (Symbol, Wild, GreaterThan, LessThan, StrictGreaterThan,
StrictLessThan, pi, I, Rational, sympify, symbols, Dummy,
Integer, Float)
Integer, Float, sstr)


def test_Symbol():
Expand Down Expand Up @@ -298,7 +298,7 @@ def test_symbols():

# issue 6675
def sym(s):
return str(symbols(s))
return sstr(symbols(s))
assert sym('a0:4') == '(a0, a1, a2, a3)'
assert sym('a2:4,b1:3') == '(a2, a3, b1, b2)'
assert sym('a1(2:4)') == '(a12, a13)'
Expand Down
Loading