Skip to content

Instantly share code, notes, and snippets.

View dutc's full-sized avatar

James Powell dutc

View GitHub Profile
@dutc
dutc / generators-free-your-mind.ipynb
Last active January 11, 2024 02:49
IPython Notebook for "Generators Will Free Your Mind"
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@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 / nwise.py
Created December 1, 2021 14:42
`nwise` and friends!
#!/usr/bin/env python3
from itertools import tee, islice, zip_longest, chain, repeat
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(): ((xs[-1] is s, x) for x, *xs in nwise_longest(g, n=m+1, fv=s))
if __name__ == '__main__':
@dutc
dutc / Makefile
Last active September 27, 2023 04:40
function hooking in C
.PHONY: hook
CC=gcc -std=c99 -Wall
hook: hook-main hook-preload.so
./hook-main
LD_PRELOAD=./hook-preload.so ./hook-main
hook-main: hook-main.c
${CC} -O0 -Wl,--export-dynamic -g -ggdb -o $@ $^ -ldl
@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 / 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))

Linux可執行文件ㄉ內容分析工具

NOTE: This is a working copy. This tutorial is unfinished and may contain inaccuracies.

I've written the title of this tutorial in Chinese, as I suspect that its contents may, at first glance, appear similarly incomprehensible to the audience.

However, just as I can sketch for you the following...

可執行文件 = (可 = can) + (執行 = execute) + (文件 = file) = executable (file)

@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 / numpy-approach.ipynb
Created December 19, 2016 19:33
The NumPy Approach - PyData SF 2016
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.