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

Problem dumping sympy expressions that contain dynamicsymbols #231

Closed
mitkof6 opened this issue Jul 8, 2017 · 4 comments
Closed

Problem dumping sympy expressions that contain dynamicsymbols #231

mitkof6 opened this issue Jul 8, 2017 · 4 comments

Comments

@mitkof6
Copy link

mitkof6 commented Jul 8, 2017

Hi,

I verified that dill is able to dump sympy symbolic expressions. Unfortunately, there are expressions that contain symbols that may be a function of time (e.g. dynamicsymbols).

import sympy as sp
import dill
from sympy.physics.mechanics import dynamicsymbols

q = dynamicsymbols('q')
t = sp.symbols('t')
dump = dill.dumps(q, -1)

Is there a way to export these kind of expressions.

Best

@mmckerns
Copy link
Member

It seems that there's a simple workaround that will allow these objects to be pickled:

import sympy as sp
import dill
from sympy.physics.mechanics import dynamicsymbols

q = dynamicsymbols('q')
t = sp.symbols('t')
q.__class__.__module__ = '__main__'
_q = dill.dumps(q)
q_ = dill.loads(_q)
assert q_ == q

The question is now: is this a general fix (i.e. it should be applied to all kinds of objects missing the module information) and thus should go into dill -- or is it a sympy-specific workaround, and thus should be applied in sympy? (FYI @asmeurer)

That will take a little further investigation.

@asmeurer
Copy link

Also fixes it for sympy.Function('f')(sympy.Symbol('x')). It is a dynamically created class from a metaclass. It looks like __module__ is set to None before you set it to __main__.

@mmckerns
Copy link
Member

mmckerns commented Apr 27, 2022

This succeeds for python 3.x, and fails for python 2.7. dill will drop support for `2.7 after the upcoming release, and then will close this issue at this time. See #413

@mmckerns
Copy link
Member

no longer relevant

@mmckerns mmckerns added this to the dill-0.3.6 milestone May 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants