Skip to content

Instantly share code, notes, and snippets.

View dutc's full-sized avatar

James Powell dutc

View GitHub Profile
@dutc
dutc / post.py
Created November 18, 2024 18:42
Posting to Bluesky via API
#!/usr/bin/env python3
from asyncio import run
from os import environ
from atproto import AsyncClient, client_utils, models
def chunked(text, *, size):
buf = []
for ln in text.splitlines():
@dutc
dutc / index.html
Last active March 17, 2023 15:45
React but with Coroutines
<html>
<head>
<title>Coroutines</title>
</head>
<body>
<div id="root"></div>
<script src="index.jsx" async type="module"></script>
</body>
</html>
@dutc
dutc / bug-1-nondeterministic.py
Last active January 2, 2024 22:11
Spot the bug in this Python code! (Fri Feb 24 @ https://www.eventbrite.com/e/548056811677/)
#!/usr/bin/env python3
from random import Random
from itertools import combinations, chain
from collections import defaultdict, Counter
from statistics import mean
letters = {1: 'aeilnorstu', 2: 'dg', 3: 'bcmp', 4: 'fhvwy', 5: 'k', 8: 'jx', 10: 'qz'}
tiles = {v: k for k, vs in letters.items() for v in vs}
@dutc
dutc / git-vault.zsh
Last active February 12, 2023 06:28
Combining `bwrap` and the `.zip` trick for auto-concatenating single-file distributables
#!/bin/zsh
identity="${1:?Must supply identity file for `age` or '-' for first run}"
target="${@[2,-1]}"
setup() {
mkdir -p ~/public
if [[ -e /tmp/.identity ]]; then
unzip -d ~/public -o /tmp/.vault >/dev/null 2>&1
age -d -i /tmp/.identity ~/public/.private 2>/dev/null | tar -C ~ -I zstd -xf - >/dev/null 2>&1
@dutc
dutc / test.zsh
Created February 10, 2023 03:07
Constructing an array of arbitrary filenames in Zsh using `find`
#!/bin/zsh
script() {
typeset -A expected=(
a 0
b 1
c 5
"d d" 10
)
mkdir -p "${(@k)expected}"
@dutc
dutc / region.py
Created October 29, 2022 00:11
Longest consecutive region meeting a specified predicate (in `pandas`)
#!/usr/bin/env python3
from numpy.random import default_rng
from pandas import Series, date_range, MultiIndex, Index, to_timedelta
from string import ascii_lowercase
rng = default_rng(0)
s = Series(
@dutc
dutc / timer.py
Last active October 21, 2022 21:16
A timing context manager.
from dataclasses import dataclass, field, replace
from collections import namedtuple, defaultdict
from itertools import count
from collections.abc import Generator
from contextlib import contextmanager
from time import perf_counter
from networkx import DiGraph
@dataclass(frozen=True)
class timer:
@dutc
dutc / instance_context.py
Created October 20, 2022 17:33
Legitimately Bad Idea (defining data model methods using instance-level modalities via `__class__`-patching)
from dataclasses import dataclass
@dataclass
class Ctx:
mode : bool = True
def __post_init__(self):
if (mode := self.mode):
cls = type(self)
self.__class__ = type(
@dutc
dutc / retry.py
Last active February 21, 2023 19:32
Legitimately Bad Idea (`retry` decorator)
from functools import wraps
from itertools import islice, tee, zip_longest, chain, product
from collections import deque
from pandas import DataFrame
nwise = lambda g, *, n=2: zip(*(islice(g, i, None) for i, g in enumerate(tee(g, n))))
nwise_longest = lambda g, *, n=2, fv=object(): zip_longest(*(islice(g, i, None) for i, g in enumerate(tee(g, n))), fillvalue=fv)
first = lambda g, *, n=1: zip(chain(repeat(True, n), repeat(False)), g)
last = lambda g, *, m=1, s=object(): ((y[-1] is s, x) for x, *y in nwise_longest(g, n=m+1, fv=s))
@dutc
dutc / 1.StopIteration.zsh
Last active September 7, 2022 18:17
`raise StopIteration` in generator body results in `RuntimeError` in Python ≥3.7
#!/bin/zsh
code="$(<<-EOF
#!/usr/bin/env python3
from logging import getLogger, INFO, basicConfig
from sys import version_info
def g():
raise StopIteration()