Смотрите, у меня есть такой класс, не нарушает ли концепцию асинхронности? Вкратце я пишу телеграмм бота используя aiogram
Вот сам код:
from collections import namedtuple
from typing import Union
from aiogram.types import ReplyKeyboardMarkup, KeyboardButton
from loader import _
class BaseMenu:
def __new__(cls, *args, **kwargs):
__buttons = {}
__count = 1
for var, value in cls.__dict__.items():
if not var.startswith('__'):
value.id = __count
__buttons[var] = value
__count += 1
_buttons = namedtuple('buttons', list(__buttons.keys()))
cls.buttons = _buttons(**__buttons)
return cls
async def validate_message(self, message: str) -> bool:
for button in self.buttons:
if message == _(button.text):
return True
else:
return False
async def get_as_keyboard(self, row_width=1) -> ReplyKeyboardMarkup:
keyboard = ReplyKeyboardMarkup(row_width=row_width)
for button in self.buttons:
keyboard.add(KeyboardButton(button.text))
return keyboard
async def get_id_by_text(self, text) -> Union[int, None]:
for button in self.buttons:
if text == _(button.text):
return button.id
else:
return
class MenuItem:
def __init__(self, text):
self.text = text
self.id = -1
def __repr__(self):
return f'<MenuItem id={self.id}, text={self.text}>'
# Examples
class ShopMenu(BaseMenu):
electronics = MenuItem('Электроника')
products = MenuItem('Продукты')
""" ...
menu = ShopMenu()
keyboard = await menu.get_as_keyboard()
await message.answer('Выберите категорию товаров:', reply_markup=keyboard)
... """