Skip to content

Instantly share code, notes, and snippets.

View michaeloliverx's full-sized avatar
🏠
Working from home

Michael Oliver michaeloliverx

🏠
Working from home
View GitHub Profile
@samuelcolvin
samuelcolvin / insert_assert.py
Last active May 10, 2023 17:45
auto-generate assert statements in pytest
"""
License: MIT
Copyright (c) 2022 Samuel Colvin.
See https://twitter.com/adriangb01/status/1573708407479189505
## Usage
Once installed just add
@nymous
nymous / README.md
Last active July 2, 2024 07:29
Logging setup for FastAPI, Uvicorn and Structlog (with Datadog integration)

Logging setup for FastAPI

This logging setup configures Structlog to output pretty logs in development, and JSON log lines in production.

Then, you can use Structlog loggers or standard logging loggers, and they both will be processed by the Structlog pipeline (see the hello() endpoint for reference). That way any log generated by your dependencies will also be processed and enriched, even if they know nothing about Structlog!

Requests are assigned a correlation ID with the asgi-correlation-id middleware (either captured from incoming request or generated on the fly). All logs are linked to the correlation ID, and to the Datadog trace/span if instrumented. This data "global to the request" is stored in context vars, and automatically added to all logs produced during the request thanks to Structlog. You can add to these "global local variables" at any point in an endpoint with `structlog.contextvars.bind_contextvars(custom

@jacob-ebey
jacob-ebey / image.ts
Last active July 2, 2024 05:58
Remix Image Component
import { createHash } from "crypto";
import fs from "fs";
import fsp from "fs/promises";
import path from "path";
import https from "https";
import { PassThrough } from "stream";
import type { Readable } from "stream";
import type { LoaderFunction } from "remix";
import sharp from "sharp";
import type { Request as NodeRequest } from "@remix-run/node";
@bluwy
bluwy / sequence.js
Last active March 29, 2022 03:42
Run Svelte preprocessors in the sequence they are declared
import { preprocess } from 'svelte/compiler'
/**
* @typedef {import("svelte/types/compiler/preprocess").PreprocessorGroup} PreprocessorGroup
* @param {PreprocessorGroup[]} preprocessors
* @returns {PreprocessorGroup[]}
*/
export function sequence(preprocessors) {
return preprocessors.map((preprocessor) => ({
markup({ content, filename }) {
@j-bullard
j-bullard / App.tsx
Created July 4, 2021 07:15
Headless UI with React Hook Forms
import "./styles.css";
import { Person } from "./types";
import { Listbox } from "./listbox/listbox";
import { useForm } from "react-hook-form";
const people: Person[] = [
{ id: 1, name: "Durward Reynolds", unavailable: false },
{ id: 2, name: "Kenton Towne", unavailable: false },
{ id: 3, name: "Therese Wunsch", unavailable: false },
{ id: 4, name: "Benedict Kessler", unavailable: true },
@florimondmanca
florimondmanca / README.md
Last active June 13, 2024 16:44
URLLib3 transport implementation (Extracted from HTTPX)

urllib3-transport

An HTTPCore transport that uses urllib3 as the HTTP networking backend. (This was initially shipped with HTTPX.)

When used with HTTPX, this transport makes it easier to transition from Requests to HTTPX by keeping the same underlying HTTP networking layer.

Compatible with: HTTPX 0.15.x, 0.16.x (i.e. HTTPCore 0.11.x and HTTPCore 0.12.x).

Note: not all urllib3 pool manager options are supported here — feel free to adapt this gist to your specific needs.

#!/usr/bin/python3
import sys
import asyncio
import greenlet
class AsyncIoGreenlet(greenlet.greenlet):
def __init__(self, driver, fn):
greenlet.greenlet.__init__(self, fn, driver)
self.driver = driver
@selimb
selimb / example_project.py
Last active May 1, 2024 04:29
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
from typing import List
import aiohttp
async def send_request(client_session: aiohttp.ClientSession, url: str, rate_limiter: RateLimiter):
async with rate_limiter.throttle():
print(f'sending url: {url}')
response = await client_session.get(url)
print(f'releasing throttler')
import inspect
import sys
from datetime import datetime
from enum import EnumMeta
from typing import Any, Dict, List, Tuple, Union, _GenericAlias, get_type_hints
from pydantic import BaseModel
# Import your pydnatic models here
models = inspect.getmembers(