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 / generators-free-your-mind.ipynb
Last active November 5, 2024 08:30
IPython Notebook for "Generators Will Free Your Mind"
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@dutc
dutc / implicit-self.py
Last active August 29, 2024 20:06
implicit self
#!/usr/bin/env python
from ctypes import c_void_p, c_char_p, c_long, c_int, c_size_t, py_object, \
Structure, POINTER, cdll
from opcode import HAVE_ARGUMENT, opmap
from itertools import imap, chain
from copy import deepcopy
from sysconfig import get_config_vars
from sys import version_info
@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__':
#!/usr/bin/env python3
class TaggedMethods(type):
def __new__(cls, name, bases, body):
tags = {}
for name, attr in ((k,v) for k,v in body.items() if isinstance(v, Tag)):
tags.setdefault(attr.tag, []).append(name)
body[name] = attr.func
body['methods_by_tag'] = property(lambda s: {k:[getattr(s,a) for a in v] for k,v in tags.items()})
return type.__new__(cls, name, bases, body)
@dutc
dutc / kwargh.py
Created September 1, 2015 16:04
help(f) # KWARGHHH!!!
#!/usr/bin/env python3
from ast import parse
from ast import AST, FunctionDef, Lambda, Call
from collections import Iterable
# some notes
# kwargh:
# when you're using matplotlib or pandas
# and you do help(func) to figure out what
@dutc
dutc / class.py
Created August 31, 2021 14:48
“Python Expert” Newsletter (Sep 22, 2021): Learning Corner
def foo(self, _):
pass
class T:
from json import dumps
from itertools import repeat
foo = foo
def bar(self, _):
pass
@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 / 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>