Socket
Socket
Sign inDemoInstall

pickle-mixin

Package Overview
Dependencies
0
Maintainers
1
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    pickle-mixin

Makes un-pickle-able objects pick-able.


Maintainers
1

Readme

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 <http://docs.pytest.org/en/latest/>__.

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:

.. code:: python

    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

.. code:: python

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

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

.. code:: python

    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:

.. code:: python

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

    def __init__(self):
        self.a = 4


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

SlotPickleMixin fixes it:

.. code:: python

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

  • Danilo Horta - https://github.com/Horta

License

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

.. |PyPI-License| image:: https://img.shields.io/pypi/l/pickle-mixin.svg?style=flat-square :target: https://pypi.python.org/pypi/pickle-mixin/ .. |PyPI-Version| image:: https://img.shields.io/pypi/v/pickle-mixin.svg?style=flat-square :target: https://pypi.python.org/pypi/pickle-mixin/

FAQs


Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc