Skip to content

Instantly share code, notes, and snippets.

View adamchainz's full-sized avatar
🍐
I like pears.

Adam Johnson adamchainz

🍐
I like pears.
View GitHub Profile
@adamchainz
adamchainz / double_checked_lock_iterator.py
Created February 28, 2020 17:18
double_checked_lock_iterator.py
# refactor of https://lukeplant.me.uk/blog/posts/double-checked-locking-with-django-orm/
# untested
def double_checked_lock_iterator(queryset):
for item_pk in queryset.values_list("pk", flat=True):
with transaction.atomic():
try:
yield queryset.select_for_update(skip_locked=True).get(id=item_pk)
except queryset.model.DoesNotExist:
pass
@adamchainz
adamchainz / aws_feeds.opml
Created December 1, 2020 12:15
AWS feeds
<?xml version="1.0" encoding="UTF-8"?>
<opml version="1.1">
<head>
<title>Adam Johnson's AWS Old Reader Category</title>
<dateCreated>Tue, 01 Dec 2020 12:13:15 GMT</dateCreated>
<dateModified>Tue, 01 Dec 2020 12:13:15 GMT</dateModified>
<ownerName>me@adamj.eu</ownerName>
<ownerEmail>me@adamj.eu</ownerEmail>
</head>
<body>
@adamchainz
adamchainz / graph-migrations.py
Created January 4, 2021 13:14
Django graph-migrations management command
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
@adamchainz
adamchainz / _base.html
Created January 8, 2021 14:34
Django HTMX CSRF setup
<script src="{% static 'app.js' %}" data-csrftoken="{{ csrf_token }}"></script>
@adamchainz
adamchainz / models.py
Created April 21, 2021 09:34
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
adamchainz / test_example.py
Created May 14, 2021 10:42
parameterized-data
from copy import deepcopy
import pytest
from django.contrib.auth.models import User
users = [
User(username="a"),
User(username="b"),
]
@adamchainz
adamchainz / test.yml
Created May 26, 2021 19:54
ansible example getting item from previous iteration of loop
- hosts: localhost
connection: local
gather_facts: no
vars:
mylist:
- a
- b
- c
tasks:
- debug:
@adamchainz
adamchainz / inner.yml
Created May 26, 2021 20:05
ansible loop with persistence of result from last
- debug:
msg: '{{ last|default("") }} -> {{ item }}'
- set_fact:
last: '{{ item }}'
@adamchainz
adamchainz / shell session
Created July 8, 2021 14:09
metaclass repr puzzle
$ 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 / example.py
Created August 29, 2021 08:32
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