Skip to main content

Custom Pydantic types and validators

Project description

grammarie

Grammarie is a type of Fae magic that is the craft of making things be. This package helps you shape your data into its proper form.

More specifically, this package defines a set of types annotated in a way that let's Pydantic validate and/or coerce data into the target type and form.

In the example below, data is provided with wrong types and formats, but are coerced by the types defined by the model and its fields.

from datetime import UTC, datetime
from pydantic import BaseModel
from grammarie import Decimal1, IntTimestampMS, LowercaseStr, UppercaseStr


class PowerTransformer(BaseModel):
    id: LowercaseStr
    name: UppercaseStr
    voltage_primary: Decimal1
    voltage_secondary: Decimal1
    installation_date: IntTimestampMS


transformers = [
    PowerTransformer(
        id="d37dca6f-6f23-4401-8cc8-e3ee422939e4",
        name="S Ulven T1 Transformator",
        voltage_primary=420.00056,
        voltage_secondary="132",
        installation_date="2023-05-03 11:15:01",
    ),
    PowerTransformer(
        id="26381360-959A-4D39-8049-D20889FAE0d6",
        name="S Ofoten T2 Transformator",
        voltage_primary="420",
        voltage_secondary=300,
        installation_date=datetime(2021, 8, 16, 8, 35, 1, tzinfo=UTC),
    ),
]

print(transformers)
# [
#     PowerTransformer(
#         id='d37dca6f-6f23-4401-8cc8-e3ee422939e4',
#         name='S ULVEN T1 TRANSFORMATOR',
#         voltage_primary=Decimal('420.0'),
#         voltage_secondary=Decimal('132.0'),
#         installation_date=1683105301000
#     ),
#     PowerTransformer(
#         id='26381360-959a-4d39-8049-d20889fae0d6',
#         name='S OFOTEN T2 TRANSFORMATOR',
#         voltage_primary=Decimal('420.0'),
#         voltage_secondary=Decimal('300.0'),
#         installation_date=1629102901000
#     )
# ]

Installation

This package is available on PyPI and can be installed with pip, uv, pdm, poetry or any other package manager.

Development

This project uses Poetry to manage the environment and Python dependencies. To install the development environment and run the test suite:

poetry install
poetry run pytest

It's recommended to also install the pre-commit hooks:

poetry run pre-commit install

This ensures that linting and formatting are run automatically on every commit.

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

grammarie-0.1.1.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

grammarie-0.1.1-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: grammarie-0.1.1.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.0.0 CPython/3.12.4

File hashes

Hashes for grammarie-0.1.1.tar.gz
Algorithm Hash digest
SHA256 7ad5d35bd5c1eaadbb8b372deccedd1bc047e1f418975d8473ee37e644c3feae
MD5 f173e2fb8957bfe339fe9c139fffcd12
BLAKE2b-256 b015fcb74cd4c3eaf77a1629698cb20980da559218b7e8f61dd5df79c9ee3b82

See more details on using hashes here.

File details

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

File metadata

  • Download URL: grammarie-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 4.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.0.0 CPython/3.12.4

File hashes

Hashes for grammarie-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 240f8c3d21153487bdf6270f5dcba1216c8298f2226826a47c28ea24559a3f05
MD5 7302840cec7f6f7994d4019287a02429
BLAKE2b-256 5e42187f2bd02400b04d4901ba854e67d63380b13993cd4a6f85bfb3a3fda379

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