Skip to content

Instantly share code, notes, and snippets.

View TomFaulkner's full-sized avatar
:octocat:
GitHub activity feed is almost as exciting as early Twitter.

Tom Faulkner TomFaulkner

:octocat:
GitHub activity feed is almost as exciting as early Twitter.
View GitHub Profile
@TomFaulkner
TomFaulkner / fa_post.py
Last active September 5, 2024 18:43
FastAPI Form handlng that doesn't suck
# Source https://github.com/fastapi/fastapi/discussions/7268#discussioncomment-6993634
from fastapi import Form
def form_body(cls):
"""
Converts a Pydantic BaseModel into FastAPI Form Body.
Usage:
@TomFaulkner
TomFaulkner / conftest.py
Created July 1, 2023 20:58
Fix FastAPI + some async db + httpx tests having no event loop / different event loop
"""Fixes the following error:
RuntimeError: Task <Task pending name='Task-215' coro=<test_view() running at
{proj}/{test_dir}/test_view.py:23> cb=[_run_until_complete_cb() at /{env_dir}/python3.11/asyncio/base_events.py:180]
> got Future <Future pending> attached to a different loop
"""
import asyncio
from typing import Generator
import pytest
@TomFaulkner
TomFaulkner / README.md
Created January 7, 2023 00:01
Monkeypatch Python functools.partials

Monkeypatching Python Partials

Python functools.partial instances bind the function before monkeypatch can get at the function. This makes patching them out difficult and tedious.

This fixture finds all partials in a module and replaces them with MagicMocks then yields the MagicMock to assert against.

This might be useful if the partials were calls to an API or service where we just want to make sure a call was made.

In my actual case these were calls to publish to various AWS SNS topics. The partials populate the topic ARN from an imported environment variable.

@TomFaulkner
TomFaulkner / README.md
Last active May 11, 2023 09:46
Remove fields from Pydantic models for FastAPI inputs

FastAPI and Pydantic are great, but in a situation where creating models through inheritence/mixins isn't practical (code generation from database queries, for example), it is difficult to remove keys from input models.

Having not found a good solution, here are two I've come up with.

Dynamically remove fields using create_model

I really wanted sans_fields to work, as it was my first solution and can be used dynamicly. Unfortunately due to typehint Forward Referencing it breaks if non-basic types are used (datetime, date, UUID, 'Foo', etc.). Calling update_forward_refs doesn't seem to fix things, neither does having the import in the namespace before running it. If objects are simple it works.

@TomFaulkner
TomFaulkner / README.md
Last active May 4, 2022 01:39
Dynamic StrEnums (Python's Enum inheriting from a str)

Why?

There may be a better way to do this, but this worked for me, and it is compatible with FastAPI's use of Enums.

Why not use the Enum functional API? (Animal = Enum('Animal', 'ANT BEE CAT DOG'))

This works if the fields are to be autogenerated using numbers, however to be useful with FastAPI Enum has to have values as str.

Are you sure?

Probably not, I would be glad to get an example of the above working, and compatible with FastAPI's Query Enums.

My use is:

@TomFaulkner
TomFaulkner / Yabai_Configs.md
Last active November 6, 2024 13:12
Yabai configs

This is my current (as of 4/30/2020) Yabai and skhdrc config as well as the Ubersicht (http://tracesof.net/uebersicht/) widget I use rather than the Yabai status bar. I went this route rather than the built-in status bar because I didn't like how Yabai as of 2.0.1 handled multiple monitors in the status bar.

Nothing is too far from defaults here, and the spaces.coffee was something I found linked in an issue on Yabai's github page.

@TomFaulkner
TomFaulkner / dynamo_upgrade.py
Created May 10, 2019 16:37
Dynamo (or other noSQL) record upgrades
import logging
LOGGER = logging.getLogger(__name__)
class SchemaUpgradeException(Exception):
""""""
@TomFaulkner
TomFaulkner / guuid.py
Created March 7, 2019 19:56
Generate x UUID4s
import sys
from uuid import uuid4
def make(number):
for _ in range(number):
print(str(uuid4()))
if __name__ == '__main__':
@TomFaulkner
TomFaulkner / going-fishing.ipynb
Last active September 9, 2019 00:33
Car Talk - Going Fishing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@TomFaulkner
TomFaulkner / gist:9c9f2fd785542947505a3204ddde1e69
Created February 21, 2019 02:36
Docker Compose for home server
version: "3"
services:
pihole:
container_name: pihole
image: pihole/pihole:latest
ports:
- "80:80/tcp"
- "443:443/tcp"
- "192.168.1.21:67:67/udp"
- "192.168.1.21:53:53/udp"