This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import traceback | |
import sys | |
import inspect | |
import logging | |
import asyncio | |
# Настройка логирования | |
logging.basicConfig(level=logging.ERROR) | |
def custom_exception_handler(function): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tracemalloc | |
from time import perf_counter | |
from typing import Callable, TypeVar, Awaitable | |
from functools import wraps | |
F = TypeVar('F', bound=Callable[..., Awaitable[None]]) | |
def measure_performance(func: F) -> F: | |
""" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import asyncio | |
from typing import Callable, TypeVar, Awaitable | |
F = TypeVar('F', bound=Callable[..., Awaitable[None]]) | |
def rate_limits(max_calls: int, period: int) -> Callable[[F], F]: | |
""" | |
Асинхронный декоратор для ограничения количества вызовов функции за определенный период времени. | |
Attrs: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
# Определение декоратора | |
def retry(max_retries: int, wait_time:int): | |
def decorator(func: Callable): | |
""" | |
Асинхронный декоратор осуществляющий несколько попыток выполнить функцию с задержкой | |
Attrs: | |
- max_retries: (int) — Количество попыток | |
- wait_time: (int) — Количество попыток |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyCustomException(Exception): | |
""" | |
Пример собственного исключения c информацией об ошибке | |
""" | |
def __init__(self, message, error_code, context_info=None, *args): | |
super().__init__(message, *args) | |
self.message = message | |
self.error_code = error_code | |
self.context_info = context_info |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Данный модуль отвечает за взаимодействие с Redis""" | |
import redis | |
import asyncio | |
import logging | |
from typing import Type, Union | |
from pydantic import BaseModel | |
from src.core.config import settings | |
logger = logging.getLogger(__name__) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import httpx | |
import logging | |
from typing import Any | |
# Извлекаем объект для отслеживания логов | |
logger = logging.getLogger(__name__) | |
class Requester: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logging | |
from typing import Type, Callable | |
from pydantic import BaseModel | |
import aio_pika | |
from config import RABBITMQ_USERNAME, RABBITMQ_PASSWORD, RABBITMQ_HOST, RABBITMQ_PORT | |
logger = logging.getLogger(__name__) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
# Для тестирования | |
if __name__ == '__main__': | |
# Запускаем функцию синхронно | |
sync_result = some_sync_func(arg1=1, arg2=2) | |
# Выводим результат |