Skip to content

Instantly share code, notes, and snippets.

View torazem's full-sized avatar

Tracy torazem

View GitHub Profile
@torazem
torazem / main.py
Last active October 21, 2025 08:25
Inspect Starlette middleware
# Python 3.12.8
# fastapi==0.70.1
# rich==14.2.0
# starlette==0.16.0
from rich import print
from fastapi import FastAPI
def inspect_middleware(app: FastAPI):
# The middleware is a combination of exception handlers and user-defined
@torazem
torazem / list_applied_migrations.py
Last active July 24, 2021 07:38
Django management command for listing latest applied migrations for each app
from django.core.management.commands import showmigrations
from django.db import DEFAULT_DB_ALIAS
from django.db.migrations.loader import MigrationLoader
from django.db.migrations.recorder import MigrationRecorder
class Command(showmigrations.Command):
help = "Shows the most recently applied migration for each app."
def add_arguments(self, parser):
@torazem
torazem / pretty_bytes.py
Last active December 13, 2020 11:14
Pretty byte printer
def _pretty_bytes(n_bytes, threshold=10000, include_bytes=True):
n = n_bytes
suffix = f" ({n_bytes} B)" if include_bytes else ""
if n < threshold:
return f"{n} B"
n /= 1024
if n < threshold:
return f"{n:.3f} KiB{suffix}"
@torazem
torazem / remapping_numpy_unpickler.py
Last active May 24, 2020 09:46
Making it easier to refactor pickled code
"""
For unpickling joblib files with renamed imports
Sample usage:
import_mapping={"bar": "baz.bar"}
name_mapping={("old.module", "OldClass"): ("new.module", "NewClass")}
with open("foo.joblib", "rb") as f:
foo = RemappingNumpyUnpickler(
@torazem
torazem / khanMathFix.js
Created May 23, 2019 12:42
Changes Khan Academy number expressions from US- to SA-locale
$('.mord').each(function(i, ele){
if ($(ele).text() == '.') {
console.log('Replacing . at ' + i);
$(ele).text(',')
}
return true;
});
$('.mord .mpunct').each(function(i, ele){
if ($(ele).text() == ',') {
import numpy as np
import matplotlib.pyplot as plt
# see http://yann.lecun.com/exdb/mnist/ for data format
def load_mnist_params(fname):
with open(fname, 'rb') as f:
magic_number = f.read(4) # 0x00000803
number_of_images = int.from_bytes(f.read(4), byteorder='big')
rows = int.from_bytes(f.read(4), byteorder='big')
@torazem
torazem / .gitignore
Last active October 20, 2016 19:57
Learning D3
*.swp
libs/
other/