Skip to main content

base models, metadata, and how to dump them to json|yaml

Project description

Model-lib - pydantic base models with convenient dump methods

Model-lib tutorial: What classes to use as base classes, how to serialize them, and add metadata

  • The models: Event and Entity are subclassing pydantic.BaseModel
  • The only specific configuration are:
    • Automatic registering for dumping
    • Support different serializers for yaml/json/pretty_json
    • use_enum_values
    • +++
from datetime import datetime

from freezegun import freeze_time
from pydantic import Field

from model_lib import Entity, Event, dump, parse_model, dump_with_metadata


class Birthday(Event):
    """
    >>> birthday = Birthday()
    >>> birthday.date = datetime.utcnow()
    Traceback (most recent call last):
    ...
    TypeError: "Birthday" is immutable and does not support item assignment
    """
    date: datetime = Field(default_factory=datetime.utcnow)


class Person(Entity):
    """
    >>> person = Person(name="espen", age=99)
    >>> person.age += 1 # mutable
    >>> person.age
    100
    """
    name: str
    age: int


_pretty_person = """{
  "age": 99,
  "name": "espen"
}"""


def test_show_dumping():
    with freeze_time("2020-01-01"):
        birthday = Birthday(date=datetime.utcnow())
        # can dump non-primitives e.g., datetime
        assert dump(birthday, "json") == '{"date":"2020-01-01T00:00:00"}'
    person = Person(name="espen", age=99)
    assert dump(person, "yaml") == "name: espen
age: 99
"
    assert dump(person, "pretty_json") == _pretty_person


_metadata_dump = """model:
  name: espen
  age: 99
metadata:
  model_name: Person
"""


def test_show_parsing(tmp_path):
    path_json = tmp_path / "example.json"
    path_json.write_text(_pretty_person)
    person = Person(name="espen", age=99)
    assert parse_model(path_json, t=Person) == person
    assert dump_with_metadata(person, format="yaml") == _metadata_dump
    path_yaml = tmp_path / "example.yaml"
    path_yaml.write_text(_metadata_dump)
    assert parse_model(path_yaml) == person  # metadata is used to find the class

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

model_lib-0.0.21-py3-none-any.whl (24.9 kB view details)

Uploaded Python 3

File details

Details for the file model_lib-0.0.21-py3-none-any.whl.

File metadata

  • Download URL: model_lib-0.0.21-py3-none-any.whl
  • Upload date:
  • Size: 24.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.17

File hashes

Hashes for model_lib-0.0.21-py3-none-any.whl
Algorithm Hash digest
SHA256 59bb13659c29bba31baf3ec6714a949395b5416b7da9719e9c77c5de6d4a28d9
MD5 6acfeb3369edc87a8c2ad86f2ce6facc
BLAKE2b-256 265451b5e1dd8c848779fb4dd1ce63e2f1b3b62e88eed2b7022fbb92ec9d4def

See more details on using hashes here.

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