Skip to content

Instantly share code, notes, and snippets.

I like pears.

Adam Johnson adamchainz

I like pears.
View GitHub Profile
adamchainz /
Created Oct 18, 2022
Django 4.1 serial to identity migration script, alternative version
Alternative version that uses internal-poking technique from:
from __future__ import annotations
import argparse
from typing import Any
from import BaseCommand
adamchainz / authlib.patch
Created Sep 14, 2022
authlib 1.0.1 to 1.1.0 wheel diff
View authlib.patch
diff --git a/Users/chainz/Downloads/Authlib-1.0.1-py2.py3-none-any/Authlib-1.0.1.dist-info/LICENSE b/Users/chainz/Downloads/Authlib-1.1.0-py2.py3-none-any/Authlib-1.1.0.dist-info/LICENSE
similarity index 100%
rename from /Users/chainz/Downloads/Authlib-1.0.1-py2.py3-none-any/Authlib-1.0.1.dist-info/LICENSE
rename to /Users/chainz/Downloads/Authlib-1.1.0-py2.py3-none-any/Authlib-1.1.0.dist-info/LICENSE
diff --git a/Users/chainz/Downloads/Authlib-1.0.1-py2.py3-none-any/Authlib-1.0.1.dist-info/METADATA b/Users/chainz/Downloads/Authlib-1.1.0-py2.py3-none-any/Authlib-1.1.0.dist-info/METADATA
similarity index 99%
rename from /Users/chainz/Downloads/Authlib-1.0.1-py2.py3-none-any/Authlib-1.0.1.dist-info/METADATA
rename to /Users/chainz/Downloads/Authlib-1.1.0-py2.py3-none-any/Authlib-1.1.0.dist-info/METADATA
index da8bbee..58ce39e 100644
--- a/Users/chainz/Downloads/Authlib-1.0.1-py2.py3-none-any/Authlib-1.0.1.dist-info/METADATA
adamchainz /
Last active Sep 2, 2022
Django test for pending migrations
from io import StringIO
from import call_command
from django.test import TestCase
class PendingMigrationsTests(TestCase):
def test_no_pending_migrations(self):
out = StringIO()
adamchainz /
Last active Jun 17, 2022
Will McGugan's get_line_offsets challenge
def adam_get_line_offsets(text: str) -> list[int]:
offsets = [0]
n = 0
text_index = text.index
offsets_append = offsets.append
while True:
n = text_index('\n', n) + 1
except ValueError:
adamchainz /
Created May 30, 2022
Curly quote fixer script
#!/usr/bin/env python
import argparse
def main(argv=None):
parser = argparse.ArgumentParser(
description="Replace curly quotes with straight versions."
adamchainz /
Created Feb 10, 2022
Django logging setting with Rich
"version": 1,
"disable_existing_loggers": False,
"filters": {
"require_debug_true": {
"()": "django.utils.log.RequireDebugTrue",
"formatters": {
"rich": {"datefmt": "[%X]"},
adamchainz /
Created Oct 18, 2021 — forked from willmcgugan/
A descriptor that permits a different type in the setter
from __future__ import annotations
from typing import NamedTuple, cast, overload
class ConsoleDimensions(NamedTuple):
width: int
height: int
adamchainz /
Created Aug 29, 2021
raylib python example
# Example of using raylib using only Python standard library
# whilst raylib-py looks useful, it seems quite out of date
from pathlib import Path
from ctypes import CDLL, c_int, c_char_p, c_ubyte, c_bool, Structure
raylib = CDLL(Path(__file__).parent / "lib" / "libraylib.3.7.0.dylib")
raylib.InitWindow.argtypes = [c_int, c_int, c_char_p]
raylib.InitWindow.restype = None
adamchainz / shell session
Created Jul 8, 2021
metaclass repr puzzle
View shell session
$ cat
class MyMeta(type):
def __new__(mcs, name, bases, attrs):
return super().__new__(mcs, name, bases, attrs)
class MyBase(metaclass=MyMeta):
$ python -c 'from base import MyBase
adamchainz / inner.yml
Created May 26, 2021
ansible loop with persistence of result from last
View inner.yml
- debug:
msg: '{{ last|default("") }} -> {{ item }}'
- set_fact:
last: '{{ item }}'