Serialize


NameSerialize JSON
Version 0.2.1 PyPI version JSON
download
home_pagehttps://github.com/hgrecco/serialize
SummaryA common API for multiple serialization formats with support for custom classes
upload_time2022-01-12 17:17:01
maintainer
docs_urlNone
authorHernan E. Grecco
requires_python>=3.6
licenseBSD
keywords serialization deserialization packing unpacking
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage
            .. image:: https://img.shields.io/pypi/v/serialize.svg
    :target: https://pypi.python.org/pypi/serialize
    :alt: Latest Version

.. image:: https://img.shields.io/pypi/l/serialize.svg
    :target: https://pypi.python.org/pypi/serialize
    :alt: License

.. image:: https://img.shields.io/pypi/pyversions/serialize.svg
    :target: https://pypi.python.org/pypi/serialize
    :alt: Python Versions

.. image:: https://github.com/hgrecco/serialize/workflows/CI/badge.svg
    :target: https://github.com/hgrecco/serialize/actions?query=workflow%3ACI
    :alt: CI

.. image:: https://github.com/hgrecco/serialize/workflows/Lint/badge.svg
    :target: https://github.com/hgrecco/serialize/actions?query=workflow%3ALint
    :alt: LINTER


.. image:: https://coveralls.io/repos/github/hgrecco/serialize/badge.svg?branch=master
    :target: https://coveralls.io/github/hgrecco/serialize?branch=master
    :alt: Coverage


Serialize: A common Python API for multiple serialization formats
=================================================================

::

    There are multiple serialization formats out there ...
        ... and great packages to use them.

But they all have a different API and switching among them is not so simple
as it should be. Serialize helps you to do it, including dealing with custom
classes. Let's dump a dict using the `pickle` format:

.. code-block:: python

    >>> from serialize import dumps, loads
    >>> dumps(dict(answer=42), fmt='pickle')
    b'\x80\x03}q\x00X\x06\x00\x00\x00answerq\x01K*s.'
    >>> loads(_, fmt='pickle')
    {'answer': 42}

And here comes the cool thing, you can just change the serialization format
without having to learn a new API. Let's now dump it using msgpack:

.. code-block:: python

    >>> dumps(dict(answer=42), fmt='msgpack')
    b'\x81\xa6answer*'
    >>> loads(_, fmt='msgpack')
    {'answer': 42}

Serialize currently support 8 different formats: `bson`, `dill`, `json` (builtin or with simplejson package), `msgpack`,
`phpserialize`, `pickle`, `serpent` and `yaml`. Serialize does not implement these
formats but rather relies on established, well tested packages. If they are installed,
serialize will use them.

::

     ** Serialize allows you to use them all with the same API! **


You can also use the `dump` and `load` to write directly to file-like object:

.. code-block:: python

    >>> from serialize import dump, load
    >>> with open('output.yaml', 'wb') as fp:
    ...     dump(dict(answer=42), fp, fmt='yaml')
    >>> with open('output.yaml', 'rb') as fp:
    ...     load(fp, fmt='yaml')
    {'answer': 42}

or use directly the filename and the format will be inferred:

.. code-block:: python

    >>> dump(dict(answer=42), 'output.yaml')
    >>> load('output.yaml')
    {'answer': 42}

A very common case is to dump and load objects from custom classes such as:

.. code-block:: python

    >>> class User:
    ...     def __init__(self, name, age):
    ...         self.name = name
    ...         self.age = age
    ...
    >>> john = User('John Smith', 27)


But some serialization packages do not support this important feature and the
rest usually have very different API between them. Serialize provides
you a common, simple interface for this. You just need to define a function
that is able to convert the object to an instance of a builtin type and the
converse:

.. code-block:: python

    >>> from serialize import register_class
    >>> def user_to_builtin(u):
    ...     return (u.name, u.age)
    ...
    >>> def user_from_builtin(c):
    ...     return User(c[0], c[1])
    ...

    >>> register_class(User, user_to_builtin, user_from_builtin)


And that's all. You can then use it directly without any hassle:

.. code-block:: python

    >>> dumps(john, fmt='bson')
    b"y\x00\x00\x00\x03__bson_follow__\x00c\x00\x00\x00\x04__dumped_obj__
    \x00\x1e\x00\x00\x00\x020\x00\x0b\x00\x00\x00John Smith\x00\x101\x00
    \x1b\x00\x00\x00\x00\x02__class_name__\x00\x1c\x00\x00\x00<class '__m
    ain__.Username'>\x00\x00\x00"
    >>> v = loads(_, fmt='bson')
    >>> v.name
    'John Smith'
    >>> v.age
    27


Enjoy!




            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/hgrecco/serialize",
    "name": "Serialize",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.6",
    "maintainer_email": "",
    "keywords": "serialization,deserialization,packing,unpacking",
    "author": "Hernan E. Grecco",
    "author_email": "hernan.grecco@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/91/62/d0fe3397a276e2586836c52e9c3eadc1bfd1a0641361cd7365bbe6dd612f/Serialize-0.2.1.tar.gz",
    "platform": "",
    "description": ".. image:: https://img.shields.io/pypi/v/serialize.svg\n    :target: https://pypi.python.org/pypi/serialize\n    :alt: Latest Version\n\n.. image:: https://img.shields.io/pypi/l/serialize.svg\n    :target: https://pypi.python.org/pypi/serialize\n    :alt: License\n\n.. image:: https://img.shields.io/pypi/pyversions/serialize.svg\n    :target: https://pypi.python.org/pypi/serialize\n    :alt: Python Versions\n\n.. image:: https://github.com/hgrecco/serialize/workflows/CI/badge.svg\n    :target: https://github.com/hgrecco/serialize/actions?query=workflow%3ACI\n    :alt: CI\n\n.. image:: https://github.com/hgrecco/serialize/workflows/Lint/badge.svg\n    :target: https://github.com/hgrecco/serialize/actions?query=workflow%3ALint\n    :alt: LINTER\n\n\n.. image:: https://coveralls.io/repos/github/hgrecco/serialize/badge.svg?branch=master\n    :target: https://coveralls.io/github/hgrecco/serialize?branch=master\n    :alt: Coverage\n\n\nSerialize: A common Python API for multiple serialization formats\n=================================================================\n\n::\n\n    There are multiple serialization formats out there ...\n        ... and great packages to use them.\n\nBut they all have a different API and switching among them is not so simple\nas it should be. Serialize helps you to do it, including dealing with custom\nclasses. Let's dump a dict using the `pickle` format:\n\n.. code-block:: python\n\n    >>> from serialize import dumps, loads\n    >>> dumps(dict(answer=42), fmt='pickle')\n    b'\\x80\\x03}q\\x00X\\x06\\x00\\x00\\x00answerq\\x01K*s.'\n    >>> loads(_, fmt='pickle')\n    {'answer': 42}\n\nAnd here comes the cool thing, you can just change the serialization format\nwithout having to learn a new API. Let's now dump it using msgpack:\n\n.. code-block:: python\n\n    >>> dumps(dict(answer=42), fmt='msgpack')\n    b'\\x81\\xa6answer*'\n    >>> loads(_, fmt='msgpack')\n    {'answer': 42}\n\nSerialize currently support 8 different formats: `bson`, `dill`, `json` (builtin or with simplejson package), `msgpack`,\n`phpserialize`, `pickle`, `serpent` and `yaml`. Serialize does not implement these\nformats but rather relies on established, well tested packages. If they are installed,\nserialize will use them.\n\n::\n\n     ** Serialize allows you to use them all with the same API! **\n\n\nYou can also use the `dump` and `load` to write directly to file-like object:\n\n.. code-block:: python\n\n    >>> from serialize import dump, load\n    >>> with open('output.yaml', 'wb') as fp:\n    ...     dump(dict(answer=42), fp, fmt='yaml')\n    >>> with open('output.yaml', 'rb') as fp:\n    ...     load(fp, fmt='yaml')\n    {'answer': 42}\n\nor use directly the filename and the format will be inferred:\n\n.. code-block:: python\n\n    >>> dump(dict(answer=42), 'output.yaml')\n    >>> load('output.yaml')\n    {'answer': 42}\n\nA very common case is to dump and load objects from custom classes such as:\n\n.. code-block:: python\n\n    >>> class User:\n    ...     def __init__(self, name, age):\n    ...         self.name = name\n    ...         self.age = age\n    ...\n    >>> john = User('John Smith', 27)\n\n\nBut some serialization packages do not support this important feature and the\nrest usually have very different API between them. Serialize provides\nyou a common, simple interface for this. You just need to define a function\nthat is able to convert the object to an instance of a builtin type and the\nconverse:\n\n.. code-block:: python\n\n    >>> from serialize import register_class\n    >>> def user_to_builtin(u):\n    ...     return (u.name, u.age)\n    ...\n    >>> def user_from_builtin(c):\n    ...     return User(c[0], c[1])\n    ...\n\n    >>> register_class(User, user_to_builtin, user_from_builtin)\n\n\nAnd that's all. You can then use it directly without any hassle:\n\n.. code-block:: python\n\n    >>> dumps(john, fmt='bson')\n    b\"y\\x00\\x00\\x00\\x03__bson_follow__\\x00c\\x00\\x00\\x00\\x04__dumped_obj__\n    \\x00\\x1e\\x00\\x00\\x00\\x020\\x00\\x0b\\x00\\x00\\x00John Smith\\x00\\x101\\x00\n    \\x1b\\x00\\x00\\x00\\x00\\x02__class_name__\\x00\\x1c\\x00\\x00\\x00<class '__m\n    ain__.Username'>\\x00\\x00\\x00\"\n    >>> v = loads(_, fmt='bson')\n    >>> v.name\n    'John Smith'\n    >>> v.age\n    27\n\n\nEnjoy!\n\n\n\n",
    "bugtrack_url": null,
    "license": "BSD",
    "summary": "A common API for multiple serialization formats with support for custom classes",
    "version": "0.2.1",
    "split_keywords": [
        "serialization",
        "deserialization",
        "packing",
        "unpacking"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "md5": "2eb781fa8f1715b5bb71b931162518d8",
                "sha256": "d6610e0f513e87a76dd05947bedb3c85b7cc51bbd78f7775696ea4f55d9112dd"
            },
            "downloads": -1,
            "filename": "Serialize-0.2.1.tar.gz",
            "has_sig": false,
            "md5_digest": "2eb781fa8f1715b5bb71b931162518d8",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.6",
            "size": 14656,
            "upload_time": "2022-01-12T17:17:01",
            "upload_time_iso_8601": "2022-01-12T17:17:01.922220Z",
            "url": "https://files.pythonhosted.org/packages/91/62/d0fe3397a276e2586836c52e9c3eadc1bfd1a0641361cd7365bbe6dd612f/Serialize-0.2.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2022-01-12 17:17:01",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "hgrecco",
    "github_project": "serialize",
    "travis_ci": false,
    "coveralls": true,
    "github_actions": true,
    "lcname": "serialize"
}
        
Elapsed time: 0.01358s