Skip to main content

Package for simplify data structures migrations

Project description

Codecov Test Code Style Documentation Status PyPI version

This is support package for simplify data serialization and persistance data between sessions and versions.

Basic usage

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

import json

from pydantic import BaseModel
from nme import NMEEncoder, nme_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=NMEEncoder)

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

assert data == data2

Migrations

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

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

from nme import NMEEncoder, nme_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=NMEEncoder)

But there is decision to mov both ca field to sub structure:

class CaModel(BaseModel)
    field_1: str
    field_2: float

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

Then with nme code may look:

from nme import nme_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=nme_object_hook)

CBOR support

Also cbor2 encoder (nme_object_encoder) and object hook (nme_cbor_decoder) are available.

import cbor2
from pydantic import BaseModel
from nme import nme_cbor_encoder, nme_cbor_decoder


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


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

with open("sample.cbor", "wb") as f_p:
    cbor2.dump(data, f_p, default=nme_cbor_encoder)

with open("sample.cbor", "rb") as f_p:
    data2 = cbor2.load(f_p, object_hook=nme_cbor_decoder)

assert data == data2

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

Additional notes

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nme-0.1.1.tar.gz (20.7 kB view details)

Uploaded Source

Built Distribution

nme-0.1.1-py3-none-any.whl (14.6 kB view details)

Uploaded Python 3

File details

Details for the file nme-0.1.1.tar.gz.

File metadata

  • Download URL: nme-0.1.1.tar.gz
  • Upload date:
  • Size: 20.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for nme-0.1.1.tar.gz
Algorithm Hash digest
SHA256 9064c6f12c62c237d8ced4a685c7b4e14211ece1ff7f7c0ee9435743e0facf74
MD5 e603af755335fee6c106e9f9b838068d
BLAKE2b-256 8acc8d55edbbe26d93f7ef53cdee69757e8f160d90771eedd9c8ec4cfc8e8e4c

See more details on using hashes here.

Provenance

File details

Details for the file nme-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: nme-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 14.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for nme-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 fabf743d084e768fb4d4b38f3a3cc286fa9481272780ff021266b0659b7fa537
MD5 163332d71e98c5ecfb4ea38250ae8115
BLAKE2b-256 3fc6a144f6bebafd44c78d235aa4c738dfc8a6c9b9788f5fd7df28c00b0ec5aa

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page