Skip to content

Instantly share code, notes, and snippets.

Avatar
🍐
I like pears.

Adam Johnson adamchainz

🍐
I like pears.
View GitHub Profile
@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 }}'
@adamchainz
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
vars:
mylist:
- a
- b
- c
tasks:
- debug:
View test_example.py
from copy import deepcopy
import pytest
from django.contrib.auth.models import User
users = [
User(username="a"),
User(username="b"),
]
@adamchainz
adamchainz / models.py
Created Apr 21, 2021
optimus django edition
View models.py
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
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
adamchainz / graph-migrations.py
Created Jan 4, 2021
Django graph-migrations management command
View graph-migrations.py
import datetime as dt
import subprocess
from colorsys import hsv_to_rgb
from textwrap import dedent
from django.core.management.base import BaseCommand
from django.db import DEFAULT_DB_ALIAS, connections
from django.db.migrations.loader import MigrationLoader