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 / 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 / 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 / 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 / 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 / _base.html
Created January 8, 2021 14:34
Django HTMX CSRF setup
<script src="{% static 'app.js' %}" data-csrftoken="{{ csrf_token }}"></script>
@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 / 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 / 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 / user.js
Last active June 1, 2017 13:26
GitHub Squash and Merge PR fixer
// ==UserScript==
// @name GitHub Squash and Merge PR fixer
// @namespace https://github.com/
// @version 1.0
// @description try to take over the world!
// @author Adam Johnson
// @match https://github.com/*
// @grant all
// ==/UserScript==
@adamchainz
adamchainz / ses_hash.py
Created November 23, 2016 16:46
SES credentials hash
"""
Implements the algorithm at https://docs.aws.amazon.com/ses/latest/DeveloperGuide/smtp-credentials.html
"""
import hmac
import hashlib
import base64
def ses_hash(secret_access_key):
message = "SendRawEmail"