local-migrator


Namelocal-migrator JSON
Version 0.1.10 PyPI version JSON
download
home_page
SummaryPackage for simplify data structures migrations
upload_time2024-02-06 13:46:58
maintainer
docs_urlNone
authorGrzegorz Bokota
requires_python>=3.8
licenseMIT
keywords migration persistance
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            **************
Local Migrator
**************

.. image:: https://codecov.io/gh/Czaki/local-migrator/branch/main/graph/badge.svg?token=KGEGEQYYRH
  :target: https://codecov.io/gh/Czaki/local-migrator
  :alt: Codecov

.. image:: https://github.com/Czaki/local-migrator/actions/workflows/tests.yml/badge.svg
  :target: https://github.com/Czaki/local-migrator/actions/workflows/tests.yml
  :alt: Test

.. image:: https://img.shields.io/badge/code%20style-black-000000.svg
  :target: https://github.com/psf/black
  :alt: Code Style

.. image:: https://readthedocs.org/projects/local-migrator/badge/?version=latest
  :target: https://local-migrator.readthedocs.io/en/latest/?badge=latest
  :alt: Documentation Status

.. image:: https://badge.fury.io/py/local-migrator.svg
  :target: https://badge.fury.io/py/local-migrator
  :alt: PyPI version

.. image:: https://anaconda.org/conda-forge/local-migrator/badges/version.svg
   :target: https://anaconda.org/conda-forge/local-migrator
   :alt: Conda-forge version


This support package simplifies data persistence between user sessions
and software version updates.

The main idea of this package is simplify data migration between versions,
and allow to define migration information next to data structure definition.


Basic usage (data serialization)
################################

If You only need to serialize data, then you could use only JSON hooks

.. code-block:: python

    import json

    from pydantic import BaseModel
    from local_migrator import Encoder, object_hook


    class SampleModel(BaseModel):
        field1: int
        field2: str


    data = SampleModel(field1=4, field2="abc")

    with open("sample.json", "w") as f_p:
        json.dump(data, f_p, cls=Encoder)

    with open("sample.json") as f_p:
        data2 = json.load(f_p, object_hook=object_hook)

    assert data == data2


Migrations
##########

To register this information there is ``register_class`` decorator.
It has 4 parameters:

* ``version`` - version of data structure
* ``migration_list`` - list of tuple (``version``. ``migration_function``).
* ``old_paths`` - list of fully qualified python paths to previous class
  definitions. This is to allow move class during code refactoring.
* ``use_parent_migrations`` - if True, then parent class migrations
  will be used.


Lets imagine that we have such code

.. code-block:: python

    from local_migrator import Encoder, object_hook

    class SampleModel(BaseModel):
        field1: int
        field_ca_1: str
        field_ca_2: float

    with open("sample.json", "w") as f_p:
        json.dump(data, f_p, cls=Encoder)

But there is decision to move both ``ca`` field to sub structure:

.. code-block:: python

    class CaModel(BaseModel):
        field_1: str
        field_2: float

    class SampleModel(BaseModel):
        field1: int
        field_ca: CaModel


Then with ``local_migrator`` code may look:

.. code-block:: python

    from local_migrator import object_hook, register_class

    class CaModel(BaseModel):
        field_1: str
        field_2: float

    def ca_migration_function(dkt):
        dkt["field_ca"] = CaModel(field1=dkt.pop("field_ca_1"),
                                  field2=dkt.pop("field_ca_2"))
        return dkt

    @register_class("0.0.1", [("0.0.1", ca_migration_function)])
    class SampleModel(BaseModel):
        field1: int
        field_ca: CaModel

    with open("sample.json") as f_p:
        data = json.load(f_p, object_hook=object_hook)

Assume that there is decision to rename ``field1`` to ``id``.
Then code may look:

.. code-block:: python

    from local_migrator import object_hook, register_class, rename_key

    class CaModel(BaseModel):
        field_1: str
        field_2: float

    def ca_migration_function(dkt):
        dkt["field_ca"] = CaModel(field1=dkt.pop("field_ca_1"),
                                  field2=dkt.pop("field_ca_2"))
        return dkt

    @register_class("0.0.2", [("0.0.1", ca_migration_function), ("0.0.2", rename_key("field1", "id"))])
    class SampleModel(BaseModel):
        id: int
        field_ca: CaModel

    with open("sample.json") as f_p:
        data = json.load(f_p, object_hook=object_hook)


More examples could be found in `examples`_ section of documentation

Additional functions
####################

* ``rename_key(from_key: str, to_key: str, optional=False) -> Callable[[Dict], Dict]`` - helper
  function for rename field migrations.

* ``update_argument(argument_name:str)(func: Callable) -> Callable`` - decorator to keep backward
  compatibility by converting ``dict`` argument to some class base on function type annotation


Contributing
############

Contributions are encouraged! Please create pull request or open issue.
For PR please remember to add tests and documentation.


Additional notes
################

This package is originally named ``nme`` but was rename to clarify its purpose.

This package is extracted from `PartSeg`_
project for simplify reuse it in another projects.


.. _PartSeg: https://github.com/4DNucleome/PartSeg
.. _examples: https://local-migrator.readthedocs.io/en/latest/examples.html

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "local-migrator",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": "",
    "keywords": "migration,persistance",
    "author": "Grzegorz Bokota",
    "author_email": "g.bokota@uw.edu.pl",
    "download_url": "https://files.pythonhosted.org/packages/1e/b4/4d6d5ef31e3de8c5f94e89b41e5c31f31cfaab9fd68bd1ba2a03ec0b67e1/local-migrator-0.1.10.tar.gz",
    "platform": null,
    "description": "**************\nLocal Migrator\n**************\n\n.. image:: https://codecov.io/gh/Czaki/local-migrator/branch/main/graph/badge.svg?token=KGEGEQYYRH\n  :target: https://codecov.io/gh/Czaki/local-migrator\n  :alt: Codecov\n\n.. image:: https://github.com/Czaki/local-migrator/actions/workflows/tests.yml/badge.svg\n  :target: https://github.com/Czaki/local-migrator/actions/workflows/tests.yml\n  :alt: Test\n\n.. image:: https://img.shields.io/badge/code%20style-black-000000.svg\n  :target: https://github.com/psf/black\n  :alt: Code Style\n\n.. image:: https://readthedocs.org/projects/local-migrator/badge/?version=latest\n  :target: https://local-migrator.readthedocs.io/en/latest/?badge=latest\n  :alt: Documentation Status\n\n.. image:: https://badge.fury.io/py/local-migrator.svg\n  :target: https://badge.fury.io/py/local-migrator\n  :alt: PyPI version\n\n.. image:: https://anaconda.org/conda-forge/local-migrator/badges/version.svg\n   :target: https://anaconda.org/conda-forge/local-migrator\n   :alt: Conda-forge version\n\n\nThis support package simplifies data persistence between user sessions\nand software version updates.\n\nThe main idea of this package is simplify data migration between versions,\nand allow to define migration information next to data structure definition.\n\n\nBasic usage (data serialization)\n################################\n\nIf You only need to serialize data, then you could use only JSON hooks\n\n.. code-block:: python\n\n    import json\n\n    from pydantic import BaseModel\n    from local_migrator import Encoder, object_hook\n\n\n    class SampleModel(BaseModel):\n        field1: int\n        field2: str\n\n\n    data = SampleModel(field1=4, field2=\"abc\")\n\n    with open(\"sample.json\", \"w\") as f_p:\n        json.dump(data, f_p, cls=Encoder)\n\n    with open(\"sample.json\") as f_p:\n        data2 = json.load(f_p, object_hook=object_hook)\n\n    assert data == data2\n\n\nMigrations\n##########\n\nTo register this information there is ``register_class`` decorator.\nIt has 4 parameters:\n\n* ``version`` - version of data structure\n* ``migration_list`` - list of tuple (``version``. ``migration_function``).\n* ``old_paths`` - list of fully qualified python paths to previous class\n  definitions. This is to allow move class during code refactoring.\n* ``use_parent_migrations`` - if True, then parent class migrations\n  will be used.\n\n\nLets imagine that we have such code\n\n.. code-block:: python\n\n    from local_migrator import Encoder, object_hook\n\n    class SampleModel(BaseModel):\n        field1: int\n        field_ca_1: str\n        field_ca_2: float\n\n    with open(\"sample.json\", \"w\") as f_p:\n        json.dump(data, f_p, cls=Encoder)\n\nBut there is decision to move both ``ca`` field to sub structure:\n\n.. code-block:: python\n\n    class CaModel(BaseModel):\n        field_1: str\n        field_2: float\n\n    class SampleModel(BaseModel):\n        field1: int\n        field_ca: CaModel\n\n\nThen with ``local_migrator`` code may look:\n\n.. code-block:: python\n\n    from local_migrator import object_hook, register_class\n\n    class CaModel(BaseModel):\n        field_1: str\n        field_2: float\n\n    def ca_migration_function(dkt):\n        dkt[\"field_ca\"] = CaModel(field1=dkt.pop(\"field_ca_1\"),\n                                  field2=dkt.pop(\"field_ca_2\"))\n        return dkt\n\n    @register_class(\"0.0.1\", [(\"0.0.1\", ca_migration_function)])\n    class SampleModel(BaseModel):\n        field1: int\n        field_ca: CaModel\n\n    with open(\"sample.json\") as f_p:\n        data = json.load(f_p, object_hook=object_hook)\n\nAssume that there is decision to rename ``field1`` to ``id``.\nThen code may look:\n\n.. code-block:: python\n\n    from local_migrator import object_hook, register_class, rename_key\n\n    class CaModel(BaseModel):\n        field_1: str\n        field_2: float\n\n    def ca_migration_function(dkt):\n        dkt[\"field_ca\"] = CaModel(field1=dkt.pop(\"field_ca_1\"),\n                                  field2=dkt.pop(\"field_ca_2\"))\n        return dkt\n\n    @register_class(\"0.0.2\", [(\"0.0.1\", ca_migration_function), (\"0.0.2\", rename_key(\"field1\", \"id\"))])\n    class SampleModel(BaseModel):\n        id: int\n        field_ca: CaModel\n\n    with open(\"sample.json\") as f_p:\n        data = json.load(f_p, object_hook=object_hook)\n\n\nMore examples could be found in `examples`_ section of documentation\n\nAdditional functions\n####################\n\n* ``rename_key(from_key: str, to_key: str, optional=False) -> Callable[[Dict], Dict]`` - helper\n  function for rename field migrations.\n\n* ``update_argument(argument_name:str)(func: Callable) -> Callable`` - decorator to keep backward\n  compatibility by converting ``dict`` argument to some class base on function type annotation\n\n\nContributing\n############\n\nContributions are encouraged! Please create pull request or open issue.\nFor PR please remember to add tests and documentation.\n\n\nAdditional notes\n################\n\nThis package is originally named ``nme`` but was rename to clarify its purpose.\n\nThis package is extracted from `PartSeg`_\nproject for simplify reuse it in another projects.\n\n\n.. _PartSeg: https://github.com/4DNucleome/PartSeg\n.. _examples: https://local-migrator.readthedocs.io/en/latest/examples.html\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Package for simplify data structures migrations",
    "version": "0.1.10",
    "project_urls": {
        "Documentation": "https://local-migrator.readthedocs.io/en/latest/",
        "Homepage": "https://github.com/Czaki/local_migrator",
        "Repository": "https://github.com/Czaki/local_migrator"
    },
    "split_keywords": [
        "migration",
        "persistance"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "06369a8f451d012549aa7e1782a95b61f482bd1001f6fc7fa7ac1bad3e399dab",
                "md5": "74532dd5ad3fbd07ca9312274055fd6f",
                "sha256": "e91861ed84b6d35df165371f174f7ad6ae005bc61ce4aee1e87bd1384166339d"
            },
            "downloads": -1,
            "filename": "local_migrator-0.1.10-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "74532dd5ad3fbd07ca9312274055fd6f",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 11729,
            "upload_time": "2024-02-06T13:46:57",
            "upload_time_iso_8601": "2024-02-06T13:46:57.218041Z",
            "url": "https://files.pythonhosted.org/packages/06/36/9a8f451d012549aa7e1782a95b61f482bd1001f6fc7fa7ac1bad3e399dab/local_migrator-0.1.10-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "1eb44d6d5ef31e3de8c5f94e89b41e5c31f31cfaab9fd68bd1ba2a03ec0b67e1",
                "md5": "5b5a62682f0e309f219dcb91918513aa",
                "sha256": "b836adf9ff9c6bacf7922dfd16c26f236df47e5fb492c27c50b9b98ed9ba254c"
            },
            "downloads": -1,
            "filename": "local-migrator-0.1.10.tar.gz",
            "has_sig": false,
            "md5_digest": "5b5a62682f0e309f219dcb91918513aa",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 24579,
            "upload_time": "2024-02-06T13:46:58",
            "upload_time_iso_8601": "2024-02-06T13:46:58.982594Z",
            "url": "https://files.pythonhosted.org/packages/1e/b4/4d6d5ef31e3de8c5f94e89b41e5c31f31cfaab9fd68bd1ba2a03ec0b67e1/local-migrator-0.1.10.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-02-06 13:46:58",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "Czaki",
    "github_project": "local_migrator",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "tox": true,
    "lcname": "local-migrator"
}
        
Elapsed time: 0.22862s