Skip to main content

This code simplifies the conversion of Pydantic schemas into Aiogram handler groups, making it easy to create form-filling handlers.

Project description

Pydantic-handler-converter

This code simplifies the conversion of Pydantic schemas into Aiogram handler groups, making it easy to create form-filling handlers.

Installation

    pip install pydantic_handler_converter

Usage:

>>> from enum import Enum
>>> from typing import Union
>>> from pydantic import BaseModel
>>> from pydantic_handler_converter import BasePydanticFormHandlers

# ----------------------------------------Simple datatypes schema--------------------------------------

>>> class PersonPydanticFormSchema(BaseModel):
...     name: str
...     age: int
...     height: float 
... 

>>> class PersonFormHanlders(BasePydanticFormHandlers[PersonPydanticFormSchema]):
...     pass
...
...
>>> dirs = dir(PersonFormHanlders)
>>> assert len(tuple(filter(lambda x: not x in dirs, ['name_view', 'age_view', 'height_view']))) == 0

# ----------------------------------------Enum datatype schema-----------------------------------------

>>> class Mood(Enum):
...     HAPPY = "😄 Happy"
...     SAD = "😢 Sad"
...     EXCITED = "🤩 Excited"
...     RELAXED = "😌 Relaxed"
...
>>>
>>>
>>> class PersonMoodPydanticFormSchema(BaseModel):
...     name: str
...     current_mood: Mood
...
>>> class PersonMoodFormHanlders(BasePydanticFormHandlers[PersonMoodPydanticFormSchema]): 
...     pass
...
...
>>> dirs = dir(PersonMoodFormHanlders)
>>> assert len(tuple(filter(lambda x: not x in dirs, ['name_view', 'current_mood_view']))) == 0

# ----------------------------------------Complex schema-----------------------------------------------

>>> class Address(BaseModel):
...     street: str
...     city: str
...     postal_code: str
...
>>> class Person(BaseModel):
...      name: str
...      age: int
...      address: Address
...
...
>>> class PersonFormHanlders(BasePydanticFormHandlers[Person]): 
...     pass
...
...
>>> dirs = dir(PersonFormHanlders)
>>> assert len(tuple(filter(lambda x: not x in dirs, 
...     ['name_view', 'address_street_view', 'address_city_view', 'address_postal_code_view']
... ))) == 0
...

# ------------------------------------Combined Enum datatype schema------------------------------------

>>> class HappyMood(Enum):
...     HAPPY = "😄 Happy"
...
>>> class SadMood(Enum):
...     SAD = "😢 Sad"
...
>>> class ExcitedMood(Enum):
...     EXCITED = "🤩 Excited"
...
>>> class RelaxedMood(Enum):
...     RELAXED = "😌 Relaxed"
...
>>>
>>> class PersonMoodPydanticFormSchema(BaseModel):
...     name: str
...     current_mood: Union[HappyMood, SadMood, ExcitedMood, RelaxedMood]
...     future_mood: HappyMood | SadMood | ExcitedMood | RelaxedMood
...
>>> class PersonMoodFormHanlders(BasePydanticFormHandlers[PersonMoodPydanticFormSchema]): 
...     pass
...
...
>>> dirs = dir(PersonMoodFormHanlders)
>>> assert len(tuple(filter(lambda x: not x in dirs, ['name_view', 'current_mood_view']))) == 0

Project details


Release history Release notifications | RSS feed

Download files

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

Source Distribution

pydantic_handler_converter-0.1.16.tar.gz (9.2 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file pydantic_handler_converter-0.1.16.tar.gz.

File metadata

  • Download URL: pydantic_handler_converter-0.1.16.tar.gz
  • Upload date:
  • Size: 9.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.10.6 Linux/5.15.0-1047-azure

File hashes

Hashes for pydantic_handler_converter-0.1.16.tar.gz
Algorithm Hash digest
SHA256 10fe14826e2f1f0e929d02f3475cce891756cadb87a898f4d87a149cf124a5d3
MD5 4a6a8f35d9a96891e598e9cb08cc851b
BLAKE2b-256 61c8b1fab024f2c968bf1d29f293d3641dbb983537f6356cadf55ed1348c5612

See more details on using hashes here.

File details

Details for the file pydantic_handler_converter-0.1.16-py3-none-any.whl.

File metadata

File hashes

Hashes for pydantic_handler_converter-0.1.16-py3-none-any.whl
Algorithm Hash digest
SHA256 c5372ebba026b38443f65b322429d428df1591ca7b104316bce6fa0c01a1d4da
MD5 a708de948bc448b51094d001b5ec0253
BLAKE2b-256 e9d2ccdf3b087066d82caa97976aab9107c56681346ee37ad4599699976b6dd8

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