Skip to content

Instantly share code, notes, and snippets.

Avatar
🍐
I like pears.

Adam Johnson adamchainz

🍐
I like pears.
View GitHub Profile
@adamchainz
adamchainz / migrate_postgres_serial_columns.py
Created Oct 18, 2022
Django 4.1 serial to identity migration script, alternative version
View migrate_postgres_serial_columns.py
"""
Alternative version that uses internal-poking technique from:
https://www.enterprisedb.com/blog/postgresql-10-identity-columns-explained
"""
from __future__ import annotations
import argparse
from typing import Any
from django.core.management.base import BaseCommand
@adamchainz
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
adamchainz / test_migrations.py
Last active Sep 2, 2022
Django test for pending migrations
View test_migrations.py
from io import StringIO
from django.core.management import call_command
from django.test import TestCase
class PendingMigrationsTests(TestCase):
def test_no_pending_migrations(self):
out = StringIO()
try:
call_command(
@adamchainz
adamchainz / get_line_offsets.py
Last active Jun 17, 2022
Will McGugan's get_line_offsets challenge
View get_line_offsets.py
def adam_get_line_offsets(text: str) -> list[int]:
offsets = [0]
n = 0
text_index = text.index
offsets_append = offsets.append
while True:
try:
n = text_index('\n', n) + 1
except ValueError:
break
@adamchainz
adamchainz / fix_curly_quotes.py
Created May 30, 2022
Curly quote fixer script
View fix_curly_quotes.py
#!/usr/bin/env python
import argparse
def main(argv=None):
parser = argparse.ArgumentParser(
description="Replace curly quotes with straight versions."
)
parser.add_argument(
"file",
@adamchainz
adamchainz / settings.py
Created Feb 10, 2022
Django logging setting with Rich
View settings.py
LOGGING = {
"version": 1,
"disable_existing_loggers": False,
"filters": {
"require_debug_true": {
"()": "django.utils.log.RequireDebugTrue",
},
},
"formatters": {
"rich": {"datefmt": "[%X]"},
@adamchainz
adamchainz / typed_property.py
Created Oct 18, 2021 — forked from willmcgugan/typed_property.py
A descriptor that permits a different type in the setter
View typed_property.py
from __future__ import annotations
from typing import NamedTuple, cast, overload
class ConsoleDimensions(NamedTuple):
width: int
height: int
@adamchainz
adamchainz / example.py
Created Aug 29, 2021
raylib python example
View example.py
# 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
adamchainz / shell session
Created Jul 8, 2021
metaclass repr puzzle
View shell session
$ cat base.py
class MyMeta(type):
def __new__(mcs, name, bases, attrs):
return super().__new__(mcs, name, bases, attrs)
class MyBase(metaclass=MyMeta):
pass
$ python -c 'from base import MyBase
@adamchainz
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 }}'