Skip to content

Instantly share code, notes, and snippets.

View vskrachkov's full-sized avatar

Viacheslav Krachkov vskrachkov

View GitHub Profile
@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 / 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
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 / 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))