Skip to content

Instantly share code, notes, and snippets.

View kurtbrose's full-sized avatar
💭
carbon based

Kurt Rose kurtbrose

💭
carbon based
View GitHub Profile
@kurtbrose
kurtbrose / tags.py
Created August 31, 2022 23:11
python 3 module __getattr__ is neat
from dataclasses import dataclass, field
from typing import Union
@dataclass
class _Tag:
name: str
attributes: dict[str, str] = field(default_factory=dict)
children: tuple[Union["Tag", str]] = ()
@kurtbrose
kurtbrose / stress_test.ts
Last active March 10, 2022 05:17
playing around with typescript
// a First is a function which takes two arguments
// of any type, and returns the type of the first argument
type First<T, U> = (a: T, b: U) => T
// as a non-trivial example, add_timedelta(time, timedelta) returns time
// doubler is parameterized on <T, U>, passes those parameters through to First
// typescript can figure out that f(f(a, b), b) is type correct
// since f is of type First, then it will return the same type as its first argument
// note we don't have ANY concrete types yet
@kurtbrose
kurtbrose / sicp_ch_3.rkt
Created August 29, 2021 15:27
working my way through structure and interpretation of computer programs, ch 3
#lang sicp
(define balance 100)
(define (withdraw amount)
(if (>= balance amount)
(begin (set! balance (- balance amount))
balance)
"Insufficient funds"))
import re
def stripe_form(data):
"""perform a stripe nested-data form-encoding"""
form = []
def _form2(pre, val):
if type(val) not in (list, dict):
form.append((pre, val))
return
@kurtbrose
kurtbrose / module_memory.py
Last active July 30, 2020 00:13
tag memory over to which modules use it
import sys, gc, collections
import psutil
def size_modules():
module_size = collections.defaultdict(int)
for obj in gc.get_objects():
if not hasattr(obj, "__module__"):
continue
if not isinstance(obj.__module__, str):
@kurtbrose
kurtbrose / branches.py
Last active July 18, 2020 23:31
glom branch exception tracing
def _glom(spec, target, scope):
scope[CHILD_ERRORS] = []
try:
...
except Exception as e:
scope.parent[CHILD_ERRORS].append((scope, e))
raise
else:
@kurtbrose
kurtbrose / topogen.py
Created December 19, 2019 19:35
find "generations" of based on a dependency graph
from relativity import M2M
def generations(m2m):
"""
m2m is {dependent: dependes-on}
returns [{item, item, ...}, {item, item, ...}, ...] in topologically sorted order
"""
m2m = m2m.copy()
generations = []
# first generation is things that have no dependencies
@kurtbrose
kurtbrose / glom2.py
Created December 19, 2019 07:18
playing around with resumable glom execution model
from collections import ChainMap
def glom(target, spec):
return Glom(spec, target).glom(ChainMap())
class Glom(object):
"""
rather than state living inside glom() methods, the recursive
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@kurtbrose
kurtbrose / usage.py
Last active April 17, 2019 00:21
playing with relativity APIs
m2m = M2M()
ch = M2MChain()
gr = M2MGraph()
# TRANSFORMING BETWEEN TYPES
chain(*ch.m2ms) # kind of a copy
chain(ch) # also results in copy