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

dump/load an object with the module where its class is defined #594

Open
sdementen opened this issue May 11, 2023 · 0 comments
Open

dump/load an object with the module where its class is defined #594

sdementen opened this issue May 11, 2023 · 0 comments

Comments

@sdementen
Copy link

I have a module defining a class. I have a script instantiating this class to get an object I want to dump.
However, I would like the dump to also contain the class/module so that loading the dump would not require the module to be defined.
(my use case is about a class representing a table in an ORM, a record of the table being an object of the class, and the table schema changing with time ==> I would like the object to be dumped with the class/table schema at the time it was extracted from the DB).

Example:

# today 2023/05/11 ---------------------------------------------
# file mymodule.py
class User:
  name: str

# file script_dump.py
from mymodule import User
u = User(name="Joe")
import dill
dill.dump(u, "user_20230511.pkl")

# in some distant future ---------------------------------------------
# file mymodule.py
class UserExtended: # the class has changed a bit
  first_name: str
  last_name: str

# file script_load.py
import dill
dill.load("user_20230511.pkl") # I would like to recover the User I dumped even though the User class has changed/disappeared or the module mymodule has changed/disappeared
# ==> will raise an Exception as the class User is not defined anymore in the mymodule.py
# ==> will raise an Exception also if the file mymodule.py is not there anymore
# Could dill dump the original mymodule & User objects at the same time as the object u to have a standalone pickle ?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant