Skip to content

Instantly share code, notes, and snippets.

View Kludex's full-sized avatar

Marcelo Trylesinski Kludex

View GitHub Profile
@Kludex
Kludex / haha.py
Created July 9, 2022 14:35
Dinamically import routers. Do not do this! This is a bad recommendation.
from fastapi import APIRouter
router = APIRouter(prefix="/haha")
@router.get("/haha")
def home():
...
import io
from typing import Any
import anyio
import ijson
from anyio.streams.buffered import BufferedByteReceiveStream
from anyio.streams.memory import MemoryObjectReceiveStream
from fastapi import FastAPI, Request, UploadFile
app = FastAPI()
@Kludex
Kludex / Dockerfile
Created September 11, 2022 15:35
FastAPI Dockerfile
# Inspired by https://snyk.io/blog/best-practices-containerizing-python-docker/
FROM python:3.10-slim
ENV PIP_NO_CACHE_DIR=off
RUN groupadd -g 999 python \
&& useradd -r -u 999 -g python python \
&& mkdir /usr/app \
&& chown python:python /usr/app
@Kludex
Kludex / main.py
Last active September 28, 2024 08:53
Properly annotate `apply_async` in Celery
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from typing import Any, Callable, Generic, Literal, Protocol, Tuple, overload
from celery import Celery as _Celery
from celery import Task as _Task
from typing_extensions import TypeVarTuple, Unpack
Ts = TypeVarTuple("Ts")
@Kludex
Kludex / main.py
Created October 18, 2022 14:29
Mandatorily have a parameter, either a or b.
from __future__ import annotations
from typing import overload
@overload
def potato(a: int, b: None = None) -> int:
...
@Kludex
Kludex / main.py
Created November 15, 2022 07:39
Inject JS script into ASGI application
from __future__ import annotations
from starlette.datastructures import MutableHeaders
BROWSER_SYNC_SCRIPT =b"<script>console.log('Hi there!');</script>"
class InjectScriptMiddleware:
def __init__(self, app, script: bytes = BROWSER_SYNC_SCRIPT):
self.app = app
self.script = script
@Kludex
Kludex / main.py
Last active December 4, 2022 19:46
Use WakaTime API
import hashlib
import os
import httpx
from httpx_auth import OAuth2AuthorizationCode
from pydantic import BaseSettings
from rich.console import Console
console = Console()
@Kludex
Kludex / main.py
Created December 5, 2022 08:58
Uvicorn logging for prod & dev
import logging
import sys
from typing import List
import structlog
import uvicorn
from fastapi import FastAPI
from structlog.stdlib import ProcessorFormatter
from structlog.types import Processor
@Kludex
Kludex / build_monorepo.sh
Last active December 28, 2022 15:02
Build a monorepo repository from scratch!
#!/bin/sh
# Creates a new monorepo by fusing multiple repositories
# Mainly copied from http://www.sevangelatos.com/monorepo/
# Child repositories that are going to be fused
CHILDREN="repo_lib_a repo_lib_b"
# Name of the created monorepo
MONOREPO="monorepo"
@Kludex
Kludex / main.py
Created April 13, 2023 08:14
Create a virtual environment, and run a different python process.
"""Create a virtual environment, and run a different python process."""
import os
import subprocess
import tempfile
import venv
with tempfile.TemporaryDirectory() as temp_dir:
VENV_PATH = os.path.join(temp_dir, "venv")
venv.create(VENV_PATH, with_pip=True)
python_exe = os.path.join(VENV_PATH, "bin", "python")