Skip to content

Instantly share code, notes, and snippets.

@leontrolski
leontrolski / dumbass_structural_type_checker.py
Created August 5, 2025 09:04
Structural typechecking- adjust to your domain
from functools import cache
from types import UnionType
from typing import Annotated, Any, Literal, Union, get_args, get_origin
import pydantic
from pydantic.fields import PydanticUndefined as MISSING # type: ignore[attr-defined]
def normalize(t: Any) -> Any:
if get_origin(t) is Annotated:
"""Basically the same as pydifact, but with some changes:
- Doesn't magically turn `[value] -> value`, each segment is a `list[list[str]]`.
- Public singular `segment_to_raw()` function.
- Easier to work out how the `Characters` configuration actually gets passed around.
- Simplified and de-OO-ed code. Doesn't handle segment names any differently to other values.
- Simpler style should make a rewrite-it-in-Rust (+PyO3) very easy if required.
"""
from __future__ import annotations
import os
from typing import TypeVar
import pydantic
T = TypeVar("T")
class _Missing:
...
@leontrolski
leontrolski / frontend.html
Created May 3, 2023 07:21
Minimal frontend
<style>
.red {
color: red;
}
</style>
<div id="root"></div>
<script>
@leontrolski
leontrolski / for_update_skip_locked.py
Created April 7, 2022 13:45
testing FOR UPDATE SKIP LOCKED
# pip install sqlalchemy testing.postgresql
# see also: https://blog.crunchydata.com/blog/message-queuing-using-native-postgresql
import threading
from contextlib import contextmanager
from sqlalchemy import Column, Integer, String, create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
import testing.postgresql
@leontrolski
leontrolski / b_tree.py
Last active March 16, 2022 10:01
minimal Python B-tree implementation
# This is a version of https://gist.github.com/natekupp/1763661 without
# using mutation and with some other simplifications. Hopefully it's slightly
# easier to follow. Performance wise - from some small benchmarking - it has
# the same O(N) characteristics/memory usage, but is about twice as slow.
#
# Use it like:
#
# b = Node([], [])
# b = add(b, n)
# print(pp(b))
@leontrolski
leontrolski / constraint.ts
Created March 7, 2022 17:55
backtracking constraint solver applied to sudoku
// ___________________________
// _| generic constraint solver |__________________________________________________
type Assignments<V> = {[K: string]: V}
type Constraint<V> = (assignments: Assignments<V>) => boolean
type KProperties<V> = {
domain: V[]
constraints: Constraint<V>[]
}
type Problem<V> = {
@leontrolski
leontrolski / interpreter.ts
Last active March 4, 2022 16:20
interpreter with assignment
// 80 line parser and interpreter for a small language with
// integers, assignments, lexical scope, first class functions
const source = `
(block
(assign x 5)
(assign f
(function
(a b)
(block
(assign c (+ a b))
@leontrolski
leontrolski / interpreter.py
Last active April 29, 2022 15:54
interpreter with assignment
# 105 line parser and interpreter for a small language with
# integers, assignments, lexical scope, first class functions
from __future__ import annotations
from dataclasses import dataclass, replace
from typing import Callable, Union
source = """
(#{
(#= x 5)
(#= f
@leontrolski
leontrolski / pubsub_rest.py
Created December 3, 2021 16:04
pubsub REST
import base64
from functools import lru_cache
import logging
from time import sleep, time
from typing import Dict, TypedDict, Tuple, List, TypeVar
import requests
from google.auth.transport.requests import AuthorizedSession
import google.auth