Skip to content

Instantly share code, notes, and snippets.

View vskrachkov's full-sized avatar

Viacheslav Krachkov vskrachkov

View GitHub Profile
@vskrachkov
vskrachkov / dict_to_openapi.py
Created March 2, 2023 22:33
Generate OpenAPI Schema based on dictionary structure
import json
import re
from typing import Tuple
def dict_to_openapi(d: dict) -> dict:
result = {}
for k, v in d.items():
if isinstance(v, dict):
result[k] = dict(type="object", properties=dict_to_openapi(v))
from typing import Iterable, Tuple, Type, Optional, List
from django.conf import settings
from django.contrib.auth.models import Permission
from django.contrib.contenttypes.models import ContentType
from django.db.models import Model
from keycloak.admin.clientroles import ClientRoles
from keycloak.exceptions import KeycloakClientError
from keycloak.openid_connect import KeycloakOpenidConnect
from keycloak.realm import KeycloakRealm
@vskrachkov
vskrachkov / di.py
Last active February 22, 2020 21:12
Simple dependency injection sample. Python. Framework agnostic
import inspect
class Value:
__slots__ = ('name', 'default')
def __init__(self, name, default=None):
self.name = name
self.default = default
@vskrachkov
vskrachkov / pipe.py
Created January 13, 2020 22:01
python, pipe, chain, monad, functor
from typing import Callable, Union
class Pipe:
def __init__(self, callable_: Callable):
self.callable_ = callable_
def __rshift__(self, then: Union[Callable, "Pipe"]) -> "Pipe":
if not isinstance(then, Pipe):
then = Pipe(then)
@vskrachkov
vskrachkov / peewee_inspect.py
Created January 2, 2020 11:20
peewee python
from peewee import *
User = Table('users', ('id', 'username'))
s = Select().columns('id', 'username').from_(User)
sub = Select().columns(s.c.id, s.c.username).from_(s)
print(s.c)
print(s._returning)
print(sub._returning)
@vskrachkov
vskrachkov / pg_listener.py
Created December 30, 2019 17:30
listen notifications from postgresql using asyncpg
import asyncio
import asyncpg
async def main():
conn: asyncpg.Connection = await asyncpg.connect()
def callback(c: asyncpg.Connection, pid: int, ch: str, p: str):
print(c, pid, ch, p)
@vskrachkov
vskrachkov / inspect.py
Created December 17, 2019 14:14
find subclasses and bases
import inspect
from typing import List, TypeVar, Type, Set
T = TypeVar('T')
def find_subclasses(cls: Type[T], deep=True) -> List[Type[T]]:
res: List[Type[T]] = []
subclasses: List[Type[T]] = cls.__subclasses__()
for subclass in subclasses:
def to_minutes(wd, h, m):
return wd * 24 * 60 + h * 60 + m
def from_minutes(minutes):
wd = minutes // (24 * 60)
rest = minutes - (wd * 24 * 60)
return wd, rest // 60, rest % 60
@vskrachkov
vskrachkov / awaitable.py
Created November 30, 2019 23:11
Create awaitable object in Python
import asyncio
class Awaitable:
def __await__(self):
yield
async def main():
await Awaitable()
@vskrachkov
vskrachkov / sync.py
Last active February 25, 2018 20:43
# models mixin
class SyncMixin:
@classmethod
def get_sync_type(cls) -> int:
"""Returns sync type."""
raise NotImplemented()
@classmethod
def get_sync_model(cls):
"""Returns django.db.Model child class.