Skip to main content

Library for creating partial pydantic models (automatic converters) from different mappings

Project description

Pydantic Marshals

Library for creating partial pydantic models (automatic converters) from different mappings. Currently, it consists of basic boilerplate parts and functional implementation for sqlalchemy 2.0+ (included via extra)

Base Interface

TBA

Implementations

TBA

SQLAlchemy: Basic usage

# sqlalchemy 2.0+ is required
from sqlalchemy import ForeignKey, String, Text
from sqlalchemy.orm import Mapped, mapped_column, relationship

from pydantic_marshals.sqlalchemy import MappedModel

class Avatar(Base):
    __tablename__ = "avatars"
    id: Mapped[int] = mapped_column(primary_key=True)
    IdModel = MappedModel.create(columns=[id])

class User(Base):
    __tablename__ = "users"
    id: Mapped[int] = mapped_column(primary_key=True)
    name: Mapped[str] = mapped_column(String(100))
    description: Mapped[str | None] = mapped_column(Text())

    avatar_id: Mapped[int] = mapped_column(ForeignKey("avatars.id"))
    avatar: Mapped[Avatar] = relationship()

    @property
    def representation(self) -> str:
        return f"User #{self.id}: {self.name}"

    BaseModel = MappedModel.create(columns=[id])
    CreateModel = MappedModel.create(columns=[name, description])
    IndexModel = MappedModel.create(properties=[representation])
    FullModel = BaseModel.extend(
        relationships=[(avatar, Avatar.IdModel)],
        includes=[CreateModel, IndexModel],
    )


with sessionmaker.begin() as session:
    user = User(name="alex", description="cool person", avatar=Avatar())
    session.add(user)
    session.flush()

    print(User.BaseModel.model_validate(user).model_dump())
    # {"id": 0}
    print(User.CreateModel.model_validate(user).model_dump())
    # {"name": "alex", "description": "cool person"}
    print(User.IndexModel.model_validate(user).model_dump())
    # {"representation": "User #0: alex"}
    print(User.FullModel.model_validate(user).model_dump())
    # {
    #   "id": 0,
    #   "name": "alex",
    #   "description": "cool person",
    #   "representation": "User #0: alex",
    #   "avatar": {"id": 0}
    # }

Assert Contains

The "assert contains" is an interface for validating data, mainly used in testing. Use "assert-contains" extra to install this module:

pip install pydantic-marshals[assert-contains]

Documentation:

Local development

  1. Clone the repository
  2. Setup python (the library is made with python 3.10+)
  3. Install poetry (should work with v1.4.1)
  4. Install dependencies
  5. Install pre-commit hooks

Commands to use:

pip install poetry==1.4.1
poetry install
pre-commit install

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

pydantic_marshals-0.3.1.tar.gz (10.6 kB view details)

Uploaded Source

Built Distribution

pydantic_marshals-0.3.1-py3-none-any.whl (19.5 kB view details)

Uploaded Python 3

File details

Details for the file pydantic_marshals-0.3.1.tar.gz.

File metadata

  • Download URL: pydantic_marshals-0.3.1.tar.gz
  • Upload date:
  • Size: 10.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.1 CPython/3.11.1 Windows/10

File hashes

Hashes for pydantic_marshals-0.3.1.tar.gz
Algorithm Hash digest
SHA256 253347809a2acad1203a4ba3051e5fd8fd61fcf5140f1395b43c17f52931aa39
MD5 86d5d0078ed80256b4a3f1d9adf9f907
BLAKE2b-256 e80719327dbddb4f03e8c28593fba61f7f0295ecde8868a38a4a1c88b6be2cee

See more details on using hashes here.

Provenance

File details

Details for the file pydantic_marshals-0.3.1-py3-none-any.whl.

File metadata

File hashes

Hashes for pydantic_marshals-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c5650384e8034f0e58ca7f71fa07f5d61eb5cd17604c682f1f0e234b8739913e
MD5 a1f06616f6e4a5511f300063cc69a171
BLAKE2b-256 c45c6c26429fd037c1feb77b1dcb9c7ba1abf44e501373fd0e93c8a0c0619a3b

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