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
Built Distribution
File details
Details for the file pydantic_handler_converter-0.1.112.tar.gz
.
File metadata
- Download URL: pydantic_handler_converter-0.1.112.tar.gz
- Upload date:
- Size: 14.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.6.1 CPython/3.10.6 Linux/5.15.0-1049-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 19b128b8298e0dde6460e71f28e59dce93c0bdb27f89c978f6af4de3cca1b206 |
|
MD5 | 430088028389d5c230d6f0250517fc0a |
|
BLAKE2b-256 | 37eb5f9189e22a7a05c8c7c644c0ef13092aeab84875a4665705621da09e6ae4 |
File details
Details for the file pydantic_handler_converter-0.1.112-py3-none-any.whl
.
File metadata
- Download URL: pydantic_handler_converter-0.1.112-py3-none-any.whl
- Upload date:
- Size: 30.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.6.1 CPython/3.10.6 Linux/5.15.0-1049-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b1233b3f22dba59719b76c3e44a6eac4baa26564ac7466900c5e43a5f23d80f0 |
|
MD5 | 3e8340ef792882df683906ac2586a4e4 |
|
BLAKE2b-256 | 279072847cb924f9a60a516f3e663af14cb5d4e6a17db0061b873f6afb120924 |