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
>>> assert PersonFormHanlders(finish_call=None)

# ----------------------------------------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
>>> assert PersonMoodFormHanlders(finish_call=None)

# ----------------------------------------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
...
>>> assert PersonFormHanlders(finish_call=None)

# ------------------------------------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
>>> assert PersonMoodFormHanlders(finish_call=None)

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.100.tar.gz (13.8 kB view details)

Uploaded Source

Built Distribution

File details

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

File metadata

File hashes

Hashes for pydantic_handler_converter-0.1.100.tar.gz
Algorithm Hash digest
SHA256 0c1fa3c7c22291736f98d26515450ee6794c857d18ee0e697af852179fca81b2
MD5 8d620f08616cd856a02a39b1d8a96a03
BLAKE2b-256 a43fab546f3619489d69ca0895572b7b5c7256349c70b6bded5cb3801b3da892

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pydantic_handler_converter-0.1.100-py3-none-any.whl
Algorithm Hash digest
SHA256 00f9661528748896e5a14c3088645c277e19eb6195cc052301dae0422163234b
MD5 3b12ea14106794bbdc3239556d8d33e9
BLAKE2b-256 f3a8be14a60187f79d1d475a1b9789b2796bb539c5ac409682236e255c7887f6

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