This code simplifies the conversion of Pydantic schemas into Aiogram handler groups, making it easy to create form-filling handlers.
Project description
Pydantic-base-aiogram
This code simplifies the conversion of Pydantic schemas into Aiogram handler groups, making it easy to create form-filling handlers.
Installation
pip install pydantic_base_aiogram
Usage:
>>> from enum import Enum
>>> from typing import Union
>>> from pydantic import BaseModel
>>> from pydantic_base_aiogram import SchemaBaseHandlersGroup
# ----------------------------------------Simple datatypes schema--------------------------------------
>>> class PersonPydanticFormSchema(BaseModel):
... name: str
... age: int
... height: float
...
>>> class PersonFormHanlders(SchemaBaseHandlersGroup[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(SchemaBaseHandlersGroup[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(SchemaBaseHandlersGroup[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(SchemaBaseHandlersGroup[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
Close
Hashes for pydantic_base_aiogram-1.0.47.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | f37a15ad8f75bb2519fa009eb490b8f49abb9a51ebadb4563d53a64c4eba26db |
|
MD5 | d25f67b192e344fea4afd40edeaeddc3 |
|
BLAKE2b-256 | 7422d1a037f3d3c45023b5c3b02ae6a738203dc0c32010c7803267b14f45ae0d |
Close
Hashes for pydantic_base_aiogram-1.0.47-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 989f5a4152df9a90ac052114f205f2c7e8d9d496d8d8df10bf375c9fb244cd57 |
|
MD5 | c6e02c3a336315396e4877ebb0e5de41 |
|
BLAKE2b-256 | 2f2a127fdb1dbc95460c5a4f8132e0a89d613ec0f0c633ec34b509c6b39cc544 |