/
BorisPlus
/
Telegram-Bot-Discussion-Examples
Обзор
Документация
Войти
/
BorisPlus
/
Telegram-Bot-Discussion-Examples
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
examples/store_button_bot/main/storage.py
53 строки
1 KB
b
2025_10_13_initial
13 окт 2025, 00:05
13 окт 2025, 00:05
0fc4405
Код
Авторство
О чём код?
from typing import Any, Dict from uuid import uuid4 from telegram_bot_discussion.logger import LoggerInterface from telegram_bot_discussion.ext.coder.store_coder import KeyValueStorageInterface class RetryCountExceed(Exception): count: int def __init__(self, count: int) -> None: self.count = count def __str__(self) -> str: return f"Retry count {self.count} exceed" class PythonInMemoryStorage(KeyValueStorageInterface): RETRIES_LIMIT = 10 db: Dict[str, Any] = dict() log: LoggerInterface def __init__(self, log: LoggerInterface) -> None: self.db = dict() self.log = log @staticmethod def autogenerate_primary_key(): while True: yield str(uuid4()).replace("-", "") def add(self, value: Any) -> str: retry: int = 0 while True: retry += 1 key = next(PythonInMemoryStorage.autogenerate_primary_key()) if key not in PythonInMemoryStorage.db: break if retry > PythonInMemoryStorage.RETRIES_LIMIT: raise RetryCountExceed(retry) PythonInMemoryStorage.db[key] = value self.log.debug(f"Add key->value: {key}->{value}") return key def get(self, key: str) -> Any: try: value = PythonInMemoryStorage.db[key] self.log.debug(f"Get key->value: {key}->{value}") return value except KeyError as e: # self.log.warning(e) raise e