Skip to content

Instantly share code, notes, and snippets.

View Kludex's full-sized avatar

Marcelo Trylesinski Kludex

View GitHub Profile
@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 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
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
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 / 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
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 / 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():
...
@Kludex
Kludex / main.py
Created June 30, 2022 05:33
Increase number of threads available on FastAPI.
import anyio
from fastapi import FastAPI
app = FastAPI()
@app.on_event("startup")
async def startup():
limiter = anyio.to_thread.current_default_thread_limiter()
@Kludex
Kludex / main.py
Last active June 1, 2022 22:15
Initial implementation of a Hook system to build middlewares.
from __future__ import annotations
from typing import TYPE_CHECKING, Awaitable, Callable, TypedDict, TypeVar
from starlette.datastructures import MutableHeaders
from typing_extensions import NotRequired
if TYPE_CHECKING:
from asgiref.typing import (
ASGI3Application,
@Kludex
Kludex / gcmsgd
Created March 10, 2022 08:43
Script that adds "[A-B] " as prefix, considering a branch called "A-B-other-stuff"
#!/usr/bin/env python3
import subprocess
import sys
branch_name = subprocess.check_output("git rev-parse --abbrev-ref HEAD".split()).decode().strip()
jira_ticket = "-".join(branch_name.split("-")[:2])
commit_msg = f"[{jira_ticket}] {sys.argv[1]}"
sys.argv[1] = commit_msg
gcmsg_args = sys.argv[1:]