Skip to content

Instantly share code, notes, and snippets.

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
"""This isn't the most efficient, but is the most "natural" to me.
match(i, j) => do letters `s[:i]` match chars `p[:j]`
- If i == -1, return False
- Set letter to the last of letters and char to last of chars
- If chars is empty, the only match is if letters is empty
- If char is *, did s[:i-1] match p[:j] or did s[:i] match p[:j-1]?
- Else does char match letter and did s[:i-1] match p[:j-1]?
@leontrolski
leontrolski / pandas_testing.py
Created November 16, 2021 11:22
pandas testing
from StringIO import StringIO
from itertools import islice, izip_longest, tee
import pandas as pd
from pandas.util.testing import assert_frame_equal
def read_data_frame_str(s, first_col_index=False):
"""Read a markdown-style description of a table into a DataFrame.
:param str s:
:param bool first_col_index: if `True`, use the first column as an index.