Skip to content

Instantly share code, notes, and snippets.

@Tishka17
Tishka17 / managed.py
Last active May 4, 2023 20:33
Managed widgets demo for aiogram-dialog
from typing import Protocol, TypeVar, Type, Dict, Any
ManagedW = TypeVar("ManagedW", covariant=True)
class Manageable(Protocol[ManagedW]):
def manage(self) -> ManagedW:
raise NotImplementedError
@Tishka17
Tishka17 / event.json
Created February 21, 2023 12:53
Example of REST API models with filtering
{
"body": "{\"sort\": {\"sortBy\": \"id\"}, \"filters\": {\"samples\": {\"concentration\": {\"min_value\": 1.2}, \"competitor\": \"abc\"}, \"hit_strength\": {\"min_value\": 20.0, \"max_value\": 50.0}}}",
"headers": {
"Accept": "*/*",
"Content-Length": "26",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "127.0.0.1:3000",
"User-Agent": "curl/7.68.0",
"X-Forwarded-Port": "3000",
"X-Forwarded-Proto": "http"
@Tishka17
Tishka17 / count_descendants.py
Last active February 15, 2023 16:06
Count descendants
class Cls:
def __init__(self, name, parents):
self.name = name
self.parents = parents
self.children = []
self.descendents = set()
def attach_parents(self, tree):
for parent in self.parents:
tree[parent].children.append(self)
@Tishka17
Tishka17 / providers.md
Last active February 5, 2023 19:23
Providers logic

Consumptions:

  • ListProvider - generates static list when list is requested
  • ListExtendProvider - prepends data to a list generated by other list provider
  • ItemProvider - generates single number
  • ListItemProvider - generates list of items retrieved from other providers

1. First can access data from following

Providers:

ListExtendProvider([1, 2]), ListProvider([3, 4])
@Tishka17
Tishka17 / README.md
Last active July 27, 2025 20:24
Fastpi Depends Stub

This Stub class is designed as a workaround for Fastapi Depends which mixes IoC-Container features with request parsing.

You can use parametrized Stub instance to declare dependency whilst not show your class'es __init__ params in OpenAPI specification.

@Tishka17
Tishka17 / test_get_chat.py
Created December 7, 2022 17:42
AI generated test for get_chat function
def test_get_chat():
# Test a Message event
message = Message(
id=1,
from_user=User(id=1, first_name="Alice"),
chat=Chat(id=1, type="private"),
date=datetime.now(),
text="Hello!",
)
assert get_chat(message) == message.chat
@Tishka17
Tishka17 / jsonrpc.py
Last active November 15, 2022 08:28
JsonRPC Client using dataclass-rest
import logging
from dataclasses import dataclass
from typing import Any, List
from uuid import uuid4
from dataclass_factory import Factory, Schema, NameStyle
from requests import Response
from dataclass_rest import post
from dataclass_rest.exceptions import ApiException
@Tishka17
Tishka17 / test_sqla.py
Created November 3, 2022 13:55
Test with sqlalchemy
import os
import pytest
from sqlalchemy import Column, Integer, create_engine, event, String
from sqlalchemy.orm import declarative_base, Session
Base = declarative_base()
class Model(Base):
@Tishka17
Tishka17 / depends.py
Last active December 21, 2022 16:41
Simple IoC framework
import contextlib
from contextlib import AbstractContextManager, ExitStack
from functools import wraps
from inspect import getfullargspec
from typing import Any, Callable, Dict, get_type_hints, Type
class Depends: # todo mypy
def __init__(self, dependency: Any = None):
self.dependency = dependency
@Tishka17
Tishka17 / confirm.py
Last active October 5, 2023 10:07
aiogram-dialog Confirmation Dialog with StatesGroup generation
import asyncio
import logging
import operator
from typing import Any, Tuple, List
from aiogram import Bot, Dispatcher
from aiogram.contrib.fsm_storage.memory import MemoryStorage
from aiogram.dispatcher.filters.state import (
StatesGroup, State, StatesGroupMeta,
)