This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sqlalchemy import select, text | |
def reset_sequences(engine, schema="public", gap=10000): | |
""" | |
Reset all sequences in a given schema to 10000, 20000, etc. so that ids won't overlap easily. | |
Ensures we don't get "lucky" and crossing ids between tables works because they are both id=1, | |
passing a test that should fail. | |
""" | |
with engine.connect() as conn: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
This defines a decorator that works kind of like a type-guard (https://peps.python.org/pep-0647/) for the arguments | |
of a function. | |
The usefulness of this is for functions that "defer" their arguments to another function, e.g. places where you | |
would call functools.wraps(). | |
@same_params_as(f) | |
@functools.wraps(f) | |
def passes_args_through(*a, **kw): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function deflate(obj) { | |
function _deflate(obj, _sofar = new Map(), _cur_ref = [0]) { | |
if (obj === null || typeof obj !== 'object') { | |
return obj; | |
} | |
if (Array.isArray(obj)) { | |
return obj.map(v => _deflate(v, _sofar, _cur_ref)); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
import importlib | |
from pathlib import Path | |
def import_all(module_name: str, ignore: str = None) -> None: | |
""" | |
Helper to recursively import all .py files on disk. | |
This ensures that all side effects from importing a module "trigger". | |
(For example, registering a model with the ORM, or adding routes.) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def inherit_annotations(cls): | |
""" | |
Inherit annotations from all base classes according to method-resolution-order. | |
This is the same way that type checkers will interpret annotations. | |
This allows for other class decorators such as attr.define() or dataclasses.dataclass() | |
to see the inherited annotations from plain-vanilla python classes. This, in turn, | |
allows base classes defining common fields to be shared among different class-decorator-annotation | |
libraries. | |
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
This module is a helper for converting a dataclass-like annotation class into a sqlalchemy ORM. | |
""" | |
from dataclasses import dataclass | |
from datetime import datetime | |
import enum | |
import functools | |
import inspect | |
import re | |
import types |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Utility for parsing .env files, as used by docker-compose and a few others. | |
""" | |
import re | |
QUOTED_STRING = r'("(?:(\\.|[^"\\])*)")' | |
SINGLE_QUOTED_STRING = r"('(?:(\\.|[^'\\])*)')" | |
VAL = fr'([^\s]+|{QUOTED_STRING}|{SINGLE_QUOTED_STRING})' | |
LINE = fr'^\s*(?P<key>{VAL})\s*=\s*(?P<val>{VAL})\s*(#.*)?$' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lang sicp | |
(define balance 100) | |
(define (withdraw amount) | |
(if (>= balance amount) | |
(begin (set! balance (- balance amount)) | |
balance) | |
"Insufficient funds")) |
NewerOlder