Skip to content

Instantly share code, notes, and snippets.

@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.
@leontrolski
leontrolski / quickstart.ts
Last active September 29, 2021 10:07
TypeScript REST server quickstart
// Get a recent version of node installed with npm
node --version
npm --version
// Make a new directory
mkdir ts-quickstart
cd ts-quickstart
// Get everything set up
npm init
@leontrolski
leontrolski / sqlalchemy-joins.sql
Created July 20, 2021 13:31
SQLAlchemy join types
-- given tables a, b
-- equivalent SQLAlchemy models A, B
-- a has many b, joined on a_id
-- a has l non-id fields, b has m
a_id | x1 | x2 | x... | xl
b_id | a_id | y1 | y2 | y... | ym
-- let's do a joined load:
# let's imagine you didn't care about
# the `output_filename` or the `extensions`
def decorator(f):
def inner(*args, **kwargs):
f(*args, **kwargs)
save(plt)
return inner
def plot_something(foo: int, bar: int) -> None: