Skip to content

Instantly share code, notes, and snippets.

View Olegt0rr's full-sized avatar

Oleg A. Olegt0rr

View GitHub Profile
@Olegt0rr
Olegt0rr / aiohttp_handler_decorator_type_hints.md
Created February 8, 2024 11:33
aiohttp handler decorator type hints
from collections.abc import Awaitable, Callable
from functools import wraps
from typing import Concatenate, ParamSpec, TypeVar

from aiohttp import web

Params = ParamSpec("Params")
Request = TypeVar("Request", bound=web.Request)
Response = TypeVar("Response", bound=web.StreamResponse)
import asyncio
from collections import defaultdict
from collections.abc import AsyncIterator, Hashable
from contextlib import asynccontextmanager
from typing import Self
class IDLock:
"""Asyncio Lock synced to some id."""
@Olegt0rr
Olegt0rr / decorators.py
Created October 31, 2023 13:32
aiohttp contextvars logging
from __future__ import annotations
import logging
from functools import wraps
from typing import Callable, TYPE_CHECKING
if TYPE_CHECKING:
from aiohttp import web
logger = logging.getLogger(__name__)
@Olegt0rr
Olegt0rr / async_chunks.py
Last active July 28, 2023 11:31
Such a solution may be needed for the effective use of third-party asynchronous iterators, which imply a step-by-step return of one element, and under the hood make a request for several elements ahead. E.g. `.find()` method of motor mongo client.
from __future__ import annotations
from typing import TYPE_CHECKING, TypeVar
if TYPE_CHECKING:
from collections.abc import AsyncIterator
T = TypeVar("T")
@Olegt0rr
Olegt0rr / single_record_traceback.py
Last active July 27, 2023 06:47
aiohttp single log record traceback
import logging
from traceback import TracebackException
from aiohttp import web
from aiohttp.abc import StreamResponse
from aiohttp.typedefs import Handler
from aiohttp.web_exceptions import HTTPException
logger = logging.getLogger(__name__)
routes = web.RouteTableDef()
@Olegt0rr
Olegt0rr / throttling_middleware.py
Last active October 24, 2022 14:15
Throttling callback queries example
import logging
from typing import Any, Awaitable, Callable, Dict, Optional, TypeVar
from aiogram import BaseMiddleware
from aiogram.dispatcher.flags import get_flag
from aiogram.types import CallbackQuery, Message
from redis.asyncio import Redis
DEFAULT_PREFIX = "throttle"
DEFAULT_TTL = 2
@Olegt0rr
Olegt0rr / retry_after_backoff.py
Created September 8, 2022 19:48
RetryAfter backoff middleware for aiogram
import asyncio
import logging
from typing import Awaitable, Callable, Optional
from aiogram import Bot
from aiogram.client.session.middlewares.base import (
BaseRequestMiddleware,
NextRequestMiddlewareType,
)
from aiogram.exceptions import TelegramRetryAfter
@Olegt0rr
Olegt0rr / USER_BANNED_IN_CHANNEL.md
Last active January 22, 2022 14:33
USER_BANNED_IN_CHANNEL

Description

Can't understand the meaning of returned exception USER_BANNED_IN_CHANNEL on sending chatAction to the supergroup while @TrueMafiaBot (id: 468253535) is a member of that supergroup (e.g. -1001639443192) and has all required permissions.

Looks like a #bug. I know about 3000 groups with similar bot behaviour.

Workaroud

I tell my users to give admin permissions to the bot - it helps to avoid described behaviour.

@Olegt0rr
Olegt0rr / root-password-MariaDB-docker-compose.md
Created October 4, 2020 11:11 — forked from rordi/root-password-MariaDB-docker-compose.md
Change root password in MariaDB Docker container running with docker-compose

Override the entrypoint in docker-compose.yml for the MariaDB Docker container by adding:

entrypoint: mysqld_safe --skip-grant-tables --user=mysql

The start up the Docker Compose stack:

$> docker-compose up -d

Then login to the Docker container:

@Olegt0rr
Olegt0rr / text_converter.py
Last active July 21, 2023 08:53
Time to text converter
# _() - is a i18n translator function
FULL_MINUTE = 60
FULL_HOUR = 60 * FULL_MINUTE
FULL_DAY = 24 * FULL_HOUR
HIDE_SECONDS_AFTER_MIN = 3
def time_to_text(seconds: int | float) -> str:
"""Receive total seconds and return text quantity of days, hours, minutes and seconds."""