[PATCH] BUG: Allow Piecewise to be pickled.

2 views
Skip to first unread message

Ondrej Certik

unread,
Jan 12, 2009, 1:02:33 AM1/12/09
to sympy-...@googlegroups.com, Robert Kern
From: Robert Kern <rober...@gmail.com>

---
sympy/functions/elementary/piecewise.py | 7 +++++++
sympy/utilities/tests/test_pickling.py | 3 +--
2 files changed, 8 insertions(+), 2 deletions(-)

diff --git a/sympy/functions/elementary/piecewise.py b/sympy/functions/elementary/piecewise.py
index 5a98ce6..db83072 100644
--- a/sympy/functions/elementary/piecewise.py
+++ b/sympy/functions/elementary/piecewise.py
@@ -78,6 +78,13 @@ def __new__(cls, *args, **options):
else:
return r

+ def __getnewargs__(self):
+ # Convert ExprCondPair objects to tuples.
+ args = []
+ for expr, condition in self.args:
+ args.append((expr, condition))
+ return tuple(args)
+
@classmethod
def canonize(cls, *args):
# Check for situations where we can evaluate the Piecewise object.
diff --git a/sympy/utilities/tests/test_pickling.py b/sympy/utilities/tests/test_pickling.py
index 86b55c3..2e08a17 100644
--- a/sympy/utilities/tests/test_pickling.py
+++ b/sympy/utilities/tests/test_pickling.py
@@ -161,8 +161,7 @@ def test_functions():
min_, atan2, polygamma, hermite, legendre, uppergamma)
x, y, z = symbols("x y z")
others = (chebyshevt_root, chebyshevu_root, Eijk(x, y, z),
- # XXX: Piecewise pickling doesn't work:
- #Piecewise( (0, x<-1), (x**2, x<=1), (x**3, True)),
+ Piecewise( (0, x<-1), (x**2, x<=1), (x**3, True)),
assoc_legendre)
for a in zero_var:
check(a)
--
1.6.0.4.1060.g9433b

Fabian Seoane

unread,
Jan 15, 2009, 5:19:19 AM1/15/09
to sympy-...@googlegroups.com
A docstring on __getnewargs__ like """Method used by pickle, protocol 2""" would be useful, since it is not obvious what the use of the method is (i had to google for it)

Otherwise +1
--
Fabian, http://fseoane.net/blog/

Ondrej Certik

unread,
Jan 16, 2009, 2:36:06 PM1/16/09
to sympy-...@googlegroups.com
On Thu, Jan 15, 2009 at 2:19 AM, Fabian Seoane <fabian...@gmail.com> wrote:
> A docstring on __getnewargs__ like """Method used by pickle, protocol 2"""
> would be useful, since it is not obvious what the use of the method is (i
> had to google for it)
>
> Otherwise +1

Thanks for the review. I'll add the docstring for it.

Ondrej

Reply all
Reply to author
Forward
0 new messages