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

Does dill have the register_pickle_by_value cloudpickle equivalent ? #671

Closed
shashwath94 opened this issue Jul 22, 2024 · 1 comment
Closed
Labels
Milestone

Comments

@shashwath94
Copy link

shashwath94 commented Jul 22, 2024

I'm trying to register modules before pickling a class instance object. I used to achieve that with the register_pickle_by_value function, is there an equivalent in dill that I can use?

cloudpickle example

import cloudpickle
import module1
import module2
from module3 import Class

cloudpickle.register_pickle_by_value(module1)
cloudpickle.register_pickle_by_value(module2)

object = Class(**params)

with open("class.pkl", "wb") as f:
    cloudpickle.dump(object, f)

Can I do the same thing but with dill instead of cloudpickle?

@mmckerns mmckerns added this to the dill-0.3.9 milestone Sep 23, 2024
@mmckerns
Copy link
Member

So, yes. Several methods, actually.
Given a module (here module.py), you can use dill.dump -- the result includes search locations and picked objects, or you can use dill.source.importable (or dill.source.getsource) to get the source of the module.

>>> import dill
>>> import module
>>> dill.dumps(module)
b'\x80\x04\x95E\x02\x00\x00\x00\x00\x00\x00\x8c\ndill._dill\x94\x8c\x0e_import_module\x94\x93\x94\x8c\x06module\x94\x85\x94R\x94}\x94(\x8c\x08__name__\x94h\x03\x8c\x07__doc__\x94\x8c\x0f\nsimple module\n\x94\x8c\x0b__package__\x94\x8c\x00\x94\x8c\x08__spec__\x94\x8c\x11_frozen_importlib\x94\x8c\nModuleSpec\x94\x93\x94)\x81\x94}\x94(\x8c\x04name\x94h\x03\x8c\x06loader\x94\x8c\x1a_frozen_importlib_external\x94\x8c\x10SourceFileLoader\x94\x93\x94)\x81\x94}\x94(h\x12h\x03\x8c\x04path\x94\x8c8/Users/mmckerns/tmp/module.py\x94ub\x8c\x06origin\x94h\x1a\x8c\x0cloader_state\x94N\x8c\x1asubmodule_search_locations\x94N\x8c\r_set_fileattr\x94\x88\x8c\x07_cached\x94\x8cP/Users/mmckerns/tmp/__pycache__/module.cpython-38.pyc\x94\x8c\r_initializing\x94\x89ub\x8c\x08__file__\x94h\x1a\x8c\n__cached__\x94h \x8c\x05Power\x94h\x03h$\x93\x94\x8c\x07squared\x94h%)\x81\x94}\x94\x8c\x01n\x94K\x02sb\x8c\x05cubed\x94h%)\x81\x94}\x94h)K\x03sbub.'
>>> dill.source.importable(module)
'import module\n'
>>> dill.source.importable(module, source=True)
"'''\nsimple module\n'''\n\nclass Power(object):\n  def __init__(self, n):\n    self.n = n\n  def __call__(self, x):\n    _type = type(x)\n    import numpy as np\n    return np.power(x, self.n).tolist()\n\nsquared = Power(2)\ncubed = Power(3)\n"

It's also possible to use dill.dump_module for more complex options.

Please reopen if you have further questions with regard to the above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants