Skip to content

Instantly share code, notes, and snippets.

I like pears.

Adam Johnson adamchainz

I like pears.
View GitHub Profile
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 }}'
adamchainz / test.yml
Created May 26, 2021
ansible example getting item from previous iteration of loop
View test.yml
- hosts: localhost
connection: local
gather_facts: no
- a
- b
- c
- debug:
from copy import deepcopy
import pytest
from django.contrib.auth.models import User
users = [
adamchainz /
Created Apr 21, 2021
optimus django edition
from __future__ import annotations
import base64
import itertools
import random
from dataclasses import dataclass
from functools import cached_property
from math import ceil, floor, log2, log10
from django.db import models
adamchainz / _base.html
Created Jan 8, 2021
Django HTMX CSRF setup
View _base.html
<script src="{% static 'app.js' %}" data-csrftoken="{{ csrf_token }}"></script>
adamchainz /
Created Jan 4, 2021
Django graph-migrations management command
import datetime as dt
import subprocess
from colorsys import hsv_to_rgb
from textwrap import dedent
from import BaseCommand
from django.db import DEFAULT_DB_ALIAS, connections
from django.db.migrations.loader import MigrationLoader