Skip to content

Instantly share code, notes, and snippets.

@msullivan
msullivan / extensions-test.py
Created May 15, 2023 22:25
script for quickly iterating on extension packages
#!/usr/bin/env python3
import edgedb
import sys
EXT = 'ltree'
def main(argv):
db = edgedb.create_client(
port=5656, database='edgedb', tls_security='insecure'
@msullivan
msullivan / xdedent.py
Last active March 17, 2023 17:38
Library for dynamically building properly indented output using f strings
import textwrap
from typing import Any
# Escape delimeters for maintaining a nesting structure in strings
# that the user produces. Obviously, as with all schemes for in-band
# signalling, all hell can break loose if the signals appear in the
# input data unescaped.
#
# Our signal sequences contain a null byte and both kinds of quote
# character, so you should be fine as long as any untrusted data
@msullivan
msullivan / view_patterns.py
Last active December 9, 2023 12:47
A few related schemes for implementing view patterns in Python. view_patterns_explicit.py is probably the least dodgy of them
"""Hacky implementation of "view patterns" with Python match
A "view pattern" is one that does some transformation on the data
being matched before attempting to match it. This can be super useful,
as it allows writing "helper functions" for pattern matching.
We provide a class, ViewPattern, that can be subclassed with custom
`match` methods that performs a transformation on the scructinee,
returning a transformed value or raising NoMatch if a match is not
possible.
@msullivan
msullivan / ops.py
Created December 21, 2022 18:56
automatically build a table of python operator functions
import operator
OPS = {
x.__doc__.split()[3]: x
for k in dir(operator)
if not k.startswith('__')
and (x := getattr(operator, k)).__doc__.startswith("Same as a ")
}
@msullivan
msullivan / aoc-1.edgeql
Last active December 1, 2022 20:05
advent of code day 1 in edgeql
with
# I made an object Input with a data property, which made it easier to
# develop this in the CLI. It also works fine to make the input a parameter.
input := (select Input filter .day = 1).data,
# input := <str>$0,
grps := array_unpack(str_split(input[:-1], '\n\n')),
sums := (for grp in grps union (
with entries := <int64>array_unpack(str_split(grp, '\n')),
select sum(entries)
)),
@msullivan
msullivan / no_tuples.py
Last active September 27, 2022 00:24
algorithm for eliminating argument tuples
from __future__ import annotations
from dataclasses import dataclass
import json
import time
import textwrap
from typing import Any, Callable
@msullivan
msullivan / aoc-scrape.py
Created December 10, 2021 20:04
Scrape an advent of code problem description for inputs
#!/usr/bin/env python3
"""Script that tries to scrape all potential test inputs.
By default it writes all of them to files in the directory
<day>.tests. It also prints all of the contents out along
with the file names so that you can quickly inspect and
determine which you want to use.
Depends on advent-of-code-data 1.1.0 (later versions may
@msullivan
msullivan / stlc-tuples.elf
Last active November 3, 2021 07:42
twelf formalization and type safety for simply typed lambda calculus with n-ary tuples
%%%% Formalization of a simply typed lambda-calculus with n-ary tuples.
% I decided to do this because it seemed like something small and
% interesting that was definitely not on the twelf wiki as a case
% study or a tutorial. n-ary tuples are a great feature in that they
% add no expressive power over pairs but are a much bigger pain to
% machine formalize!
%
% Doing it on paper is mostly just a matter of sprinkling "subscript i"
% and "..." around, but is otherwise pretty straightforward.
@msullivan
msullivan / full_outer_join_1.edgeql
Last active July 14, 2021 22:12
simulate a FULL OUTER JOIN in edgeql
# Do a FULL OUTER JOIN on User.name[0] = Card.name[0]
# (the seed computable is to demonstrate that it works with computed data)
WITH
L := User { seed := random() },
R := Card,
joined := DISTINCT {
(FOR l IN {L} UNION ([l.id], [(SELECT R FILTER l.name[0] = R.name[0]).id] ?? <array<uuid>>[])),
(FOR r IN {R} UNION (([(SELECT L FILTER L.name[0] = r.name[0]).id] ?? <array<uuid>>[]), [r.id])),
},
FOR row in {joined} UNION (
@msullivan
msullivan / scopetree-test-fixup.py
Last active March 17, 2021 20:33
fixup test_edgeql_ir_scopetree.py from test output log
#!/usr/bin/env python3
# Run with:
# edb test tests/test_edgeql_ir_scopetree.py 2>&1 | python3 scopetree-test-fixup.py -i
import textwrap
import sys
from typing import *