Skip to main content

pickle-mixin

PyPI-License PyPI-Version

Makes un-pickle-able objects pick-able.

Install

You can install it via pip

pip install pickle-mixin

Running the tests

After installation, you can test it

python -c "import pickle_mixin; pickle_mixin.test()"

as long as you have pytest.

Usage

Pickle by initialisation

Suppose that you have a class whose objects are un-pickle-able or that would demand a large amount of disk space or memory to be pickle-able. PickleByInit class lets you pickle object attributes via object initialization. Consider the following classes:

class Foo(PickleByInit):
    def __init__(self, obj):
        super(Foo, self).__init__()
        self.obj = obj

class Bar(object):
    def __init__(self, filename):
        self.filename = filename

    def __getstate__(self):
        raise PicklingError

    def init_dict(self):
        return dict(filename=self.filename)

Trying to pickle as follows

f = Foo(Bar('file.txt'))
pickle.dumps(f)

would raise a PicklingError. The following on the other hand would work:

f = Foo(Bar('file.txt'))
f.set_signature_only_attr('obj')
pickle.dumps(f)

The un-pickling process of f.obj attribute happens via object initialisation, passing the returned dictionary from init_dict as keyword arguments to Bar.__init__.

Mixing classes with and without slots

Pickling does not save attributes defined via __slots__ in the following case:

class Foo(object):
    __slots__ = ['a']

    def __init__(self):
        self.a = 4


class Bar(Foo):
    def __init__(self):
        pass

SlotPickleMixin fixes it:

class FooMixin(object):
    __slots__ = ['a']

    def __init__(self):
        self.a = 4


class BarMixin(FooMixin, SlotPickleMixin):
    def __init__(self):
        FooMixin.__init__(self)
        SlotPickleMixin.__init__(self)

f = BarMixin()
o = pickle.dumps(f)
f = pickle.loads(o)
assert hasattr(f, 'a')

Authors

License

This project is licensed under the MIT License - see the LICENSE file for details

Metadata

Release files for pickle-mixin 1.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pickle-mixin 1.0.2
File Size Uploaded
pickle-mixin-1.0.2.tar.gz 5.1 kB Details

Release files / pickle-mixin-1.0.2.tar.gz

Download URL pickle-mixin-1.0.2.tar.gz
Size 5.1 kB
Tags Source
SHA-256 checksum
How to use checksums
d4983cd09939f6e402c69ba4f4200f2e6ccdfd39c020f6d1091290d78f17625c
BLAKE2b-256 checksum
How to use checksums
02779d5eb2201bbc130e2a5cc41fc949e4ab0da74b619107eac1c511be3af7a7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

1.0.2 This release

1 release file

1.0.1

1 release file

1.0.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page