pickle-mixin


Namepickle-mixin JSON
Version 1.0.2 PyPI version JSON
download
home_pagehttps://github.com/limix/pickle-mixin
SummaryMakes un-pickle-able objects pick-able.
upload_time2017-04-09 22:30:44
maintainer
docs_urlNone
authorDanilo Horta
requires_python
licenseMIT
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI
coveralls test coverage No coveralls.
            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/
            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/limix/pickle-mixin",
    "name": "pickle-mixin",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "",
    "author": "Danilo Horta",
    "author_email": "horta@ebi.ac.uk",
    "download_url": "https://files.pythonhosted.org/packages/02/77/9d5eb2201bbc130e2a5cc41fc949e4ab0da74b619107eac1c511be3af7a7/pickle-mixin-1.0.2.tar.gz",
    "platform": "UNKNOWN",
    "description": "pickle-mixin\n============\n\n|PyPI-License| |PyPI-Version|\n\nMakes un-pickle-able objects pick-able.\n\nInstall\n-------\n\nYou can install it via pip\n\n::\n\n    pip install pickle-mixin\n\nRunning the tests\n-----------------\n\nAfter installation, you can test it\n\n::\n\n    python -c \"import pickle_mixin; pickle_mixin.test()\"\n\nas long as you have `pytest <http://docs.pytest.org/en/latest/>`__.\n\nUsage\n-----\n\nPickle by initialisation\n~~~~~~~~~~~~~~~~~~~~~~~~\n\nSuppose that you have a class whose objects are un-pickle-able or that\nwould demand a large amount of disk space or memory to be pickle-able.\n``PickleByInit`` class lets you pickle object attributes via object\ninitialization. Consider the following classes:\n\n.. code:: python\n\n    class Foo(PickleByInit):\n        def __init__(self, obj):\n            super(Foo, self).__init__()\n            self.obj = obj\n\n    class Bar(object):\n        def __init__(self, filename):\n            self.filename = filename\n\n        def __getstate__(self):\n            raise PicklingError\n\n        def init_dict(self):\n            return dict(filename=self.filename)\n\nTrying to pickle as follows\n\n.. code:: python\n\n    f = Foo(Bar('file.txt'))\n    pickle.dumps(f)\n\nwould raise a ``PicklingError``. The following on the other hand would\nwork:\n\n.. code:: python\n\n    f = Foo(Bar('file.txt'))\n    f.set_signature_only_attr('obj')\n    pickle.dumps(f)\n\nThe un-pickling process of ``f.obj`` attribute happens via object\ninitialisation, passing the returned dictionary from ``init_dict`` as\nkeyword arguments to ``Bar.__init__``.\n\nMixing classes with and without slots\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\nPickling does not save attributes defined via ``__slots__`` in the\nfollowing case:\n\n.. code:: python\n\n    class Foo(object):\n        __slots__ = ['a']\n\n        def __init__(self):\n            self.a = 4\n\n\n    class Bar(Foo):\n        def __init__(self):\n            pass\n\n``SlotPickleMixin`` fixes it:\n\n.. code:: python\n\n    class FooMixin(object):\n        __slots__ = ['a']\n\n        def __init__(self):\n            self.a = 4\n\n\n    class BarMixin(FooMixin, SlotPickleMixin):\n        def __init__(self):\n            FooMixin.__init__(self)\n            SlotPickleMixin.__init__(self)\n\n    f = BarMixin()\n    o = pickle.dumps(f)\n    f = pickle.loads(o)\n    assert hasattr(f, 'a')\n\nAuthors\n-------\n\n-  **Danilo Horta** - https://github.com/Horta\n\nLicense\n-------\n\nThis project is licensed under the MIT License - see the\n`LICENSE <LICENSE>`__ file for details\n\n.. |PyPI-License| image:: https://img.shields.io/pypi/l/pickle-mixin.svg?style=flat-square\n   :target: https://pypi.python.org/pypi/pickle-mixin/\n.. |PyPI-Version| image:: https://img.shields.io/pypi/v/pickle-mixin.svg?style=flat-square\n   :target: https://pypi.python.org/pypi/pickle-mixin/",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Makes un-pickle-able objects pick-able.",
    "version": "1.0.2",
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "02779d5eb2201bbc130e2a5cc41fc949e4ab0da74b619107eac1c511be3af7a7",
                "md5": "69bb840886237f83f86869a79736d6d4",
                "sha256": "d4983cd09939f6e402c69ba4f4200f2e6ccdfd39c020f6d1091290d78f17625c"
            },
            "downloads": -1,
            "filename": "pickle-mixin-1.0.2.tar.gz",
            "has_sig": false,
            "md5_digest": "69bb840886237f83f86869a79736d6d4",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 5113,
            "upload_time": "2017-04-09T22:30:44",
            "upload_time_iso_8601": "2017-04-09T22:30:44.369133Z",
            "url": "https://files.pythonhosted.org/packages/02/77/9d5eb2201bbc130e2a5cc41fc949e4ab0da74b619107eac1c511be3af7a7/pickle-mixin-1.0.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2017-04-09 22:30:44",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "limix",
    "github_project": "pickle-mixin",
    "travis_ci": true,
    "coveralls": false,
    "github_actions": false,
    "lcname": "pickle-mixin"
}
        
Elapsed time: 0.04310s