Skip to main content

A collection of helper functions/classes for Pydantic.

Project description

Phana's Pydantic Helpers

pypi pypi-python license

A collection of helper functions/classes for Pydantic.

Table of Contents

Install

Prerequisites

  • Poetry – dependency manager

Install Phana's Pydantic Helpers

To get started, install the package with Poetry.

poetry add phanas-pydantic-helpers

Usage

Factory

Factory(...) is simply an alias for pydantic.Field(default_factory=...).

from pydantic import BaseModel

from phanas_pydantic_helpers import Factory


class Config(BaseModel):
    token: str
    
    class _ExtraInfo(BaseModel):
        name: str = "Unnamed"
        description: str = "Empty description"

    extra_info: _ExtraInfo = Factory(_ExtraInfo)


model = Config(token="bleh")
assert model.extra_info.name == "Unnamed"
model.extra_info.description = "A more detailed description"

FieldConverter

Easily create custom fields with one or more type converters. Make sure the first superclass is the type you want to represent, as this is considered the main base class and will take precedence over FieldConverter, offering better code completion.

from phanas_pydantic_helpers import FieldConverter
from pydantic import BaseModel


class ToInt(int, FieldConverter):

    @classmethod
    def _pyd_convert_str(cls, value: str) -> int:
        return int(value)

    @classmethod
    def _pyd_convert_bytes(cls, value: bytes) -> int:
        return int.from_bytes(value, "big")


class Container(BaseModel):
    value: ToInt


container_from_str = Container(value="5")
assert container_from_str.value == 5

container_from_bytes = Container(value=b"\x00\xFF")
assert container_from_bytes.value == 0xFF

create_template_from_model

Create a dict from a model with required fields. This function fills in required fields with placeholders.

from typing import Dict, List

from pydantic import BaseModel
from phanas_pydantic_helpers import Factory, create_template_from_model


class Player(BaseModel):
    name: str
    admin = False
    highest_score: float = 1.0
    extra_data: Dict[str, str]


class PlayerDatabase(BaseModel):
    version: int
    players: List[Player]


class GameSystem(BaseModel):
    system_name = "PhanaBox"
    games: List[str]
    player_database: PlayerDatabase = Factory(PlayerDatabase)


assert create_template_from_model(GameSystem) == {
    "system_name": "PhanaBox",
    "games": ["GAMES"],
    "player_database": {
        "version": 0,
        "players": [
            {
                "name": "NAME",
                "admin": False,
                "highest_score": 1.0,
                "extra_data": {"KEY_NAME": "EXTRA_DATA"},
            }
        ],
    },
}

Changelog

See CHANGELOG.md.

Developers

Installation

Follow the installation steps in install and use Poetry to install the development dependencies:

poetry install

License

MIT © Phanabani.

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

phanas_pydantic_helpers-2.1.3.tar.gz (9.9 kB view details)

Uploaded Source

Built Distribution

phanas_pydantic_helpers-2.1.3-py3-none-any.whl (12.2 kB view details)

Uploaded Python 3

File details

Details for the file phanas_pydantic_helpers-2.1.3.tar.gz.

File metadata

  • Download URL: phanas_pydantic_helpers-2.1.3.tar.gz
  • Upload date:
  • Size: 9.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.2 CPython/3.10.6 Linux/5.15.0-1033-azure

File hashes

Hashes for phanas_pydantic_helpers-2.1.3.tar.gz
Algorithm Hash digest
SHA256 24dbe73781da005efe3e3fa8115a9096e9f1c09a9148204f6c297e04d3114b06
MD5 9e60d12651c84803f61577cf5c26f61f
BLAKE2b-256 5447a0130214d649e300098f93655bfe278fcd2f65e21580ec825739a85cd493

See more details on using hashes here.

File details

Details for the file phanas_pydantic_helpers-2.1.3-py3-none-any.whl.

File metadata

File hashes

Hashes for phanas_pydantic_helpers-2.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 160c841b891e41ec93c41d78c85966f1eb44e0726d78682bc389c84e3fc79870
MD5 844b2df5c618ab0d98954010e054ef35
BLAKE2b-256 76766a2ad9516c00c899e2c0b5106de578b5ae46ae0f764fdd30142f57ca969a

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