Skip to content

Instantly share code, notes, and snippets.

@DomWeldon
DomWeldon / crudrouter.py
Last active June 24, 2022 15:38
CRUD Router
# Standard Library
from typing import (
Any,
Callable,
Dict,
FrozenSet,
List,
Optional,
Sequence,
Set,
@erny
erny / renderers.py
Last active December 18, 2023 09:14
FastAPI Simple content negotiation
"""Simple content negotiation
Usage:
Example 1:
from renderers import render
@router.post("/myapp/items/")
async def api_login(item: Item, accept: Optional[str] = Header(default='application/jwt')):
@yesthesoup
yesthesoup / test_migrations.py
Created September 24, 2020 21:57
Unit testing Flask and Alembic database migrations with pytest
from alembic.config import Config
from alembic.script import ScriptDirectory
from flask_migrate import downgrade, upgrade, stamp
from flask_project import create_app
from flask_project.config import TestingConfig
from flask_project.database import db # db = flask_sqlalchemy.SQLAlchemy()
# in conftest.py
@pytest.fixture
@selimb
selimb / example_project.py
Last active May 10, 2025 04:43
FastAPI lifespan-scoped (singleton) dependencies
########
# app.py
########
# Example of how I use it in my project. This file cannot be run as-is.
# The only difference with the example in the fastapi_singleton module docstring is
# the use of a subclassed FastAPI application to define type annotations
import fastapi
import fastapi_singleton
@timhughes
timhughes / fastapi_websocket_redis_pubsub.py
Last active February 21, 2025 08:41
FastAPI Websocket Bidirectional Redis PubSub
"""
Usage:
Make sure that redis is running on localhost (or adjust the url)
Install uvicorn or some other asgi server https://asgi.readthedocs.io/en/latest/implementations.html
pip install -u uvicorn
Install dependencies
import asyncio
import logging
import time
from functools import lru_cache
import uvicorn
from aiocache import cached
from fastapi import FastAPI, APIRouter, Depends
logger = logging.getLogger(__name__)
@xirixiz
xirixiz / pihole-macvlan-synology-docker.txt
Last active December 2, 2024 19:32
Add a PiHole instance on a macvlan enabled Docker network (Synology eth0 example)
#!/bin/bash
# NAS IP: 192.168.1.10 in this example
# DHCP scope reservation for macvlan: 192.168.1.210/28 (Details below)
## Network: 192.168.1.210/28
## HostMin: 192.168.1.211
## HostMax: 192.168.1.224
## Hosts/Net: 14
# Create a Synology macvlan0 bridge network attached to the physical eth0, and add the ip range scope (sudo)
UserCreateT = TypeVar("UserCreateT", bound=UserCreate)
UserCreateRequestT = TypeVar("UserCreateRequestT", bound=UserCreateRequest)
UserInDBT = TypeVar("UserInDBT", bound=UserInDB)
UserUpdateT = TypeVar("UserUpdateT", bound=UserUpdate)
UserApiT = TypeVar("UserApiT", bound=UserBaseInDB)
UserOrmT = TypeVar("UserOrmT", bound=BaseUser)
logger = logging.getLogger(__name__)

How to setup a practically free CDN using Backblaze B2 and Cloudflare

⚠️ Note 2023-01-21
Some things have changed since I originally wrote this in 2016. I have updated a few minor details, and the advice is still broadly the same, but there are some new Cloudflare features you can (and should) take advantage of. In particular, pay attention to Trevor Stevens' comment here from 22 January 2022, and Matt Stenson's useful caching advice. In addition, Backblaze, with whom Cloudflare are a Bandwidth Alliance partner, have published their own guide detailing how to use Cloudflare's Web Workers to cache content from B2 private buckets. That is worth reading,

@tatianamac
tatianamac / tatiana-mac-speaker-rider.md
Last active April 22, 2025 22:44
Tatiana Mac's Speaker Rider

Speaker Rider

by Tatiana Mac

Last updated 14 April 2021

What is a speaker rider?

As speaking comes with immense privilege, I have crafted a speaker rider to set expectations and boundaries around my engagement. I am grateful to all the conference organisers who have brilliantly hosted me. I would love to continue to exercise this privilege to speak at conferences, and use this privilege to make the landscape more accessible and beneficial to tech's most historically excluded and marginalised communities.

Considerations

😫 I provide a lot of explanations for those of you who never had to consider these things. Most thoughtful conferences I've attended check most of these boxes intrinsically, particularly when conference runners are experienced speakers. They get it.