Skip to content

Instantly share code, notes, and snippets.

nwjlyons /
Created Apr 12, 2021
Django QuerySet function that will return up to `max_length` objects from the `selected` queryset, falling back to the `fallback` queryset to make up the numbers.
def get_selected_or_fallback(*, selected: QuerySet, fallback: QuerySet, max_length: int) -> list:
Return up to `max_length` objects from the `selected` queryset,
falling back to the `fallback` queryset to make up the numbers.
selected = list(selected[:max_length])
fallback = list(fallback.exclude(id__in=[ for obj in selected])[:max_length])
return (selected + fallback)[:max_length]
nwjlyons /
Last active Apr 5, 2021
Plot points along circumference using Trigonometry
import math
radius = 20
circumference_points_ratios = [(math.cos(degree), math.sin(degree)) for degree in range(360)]
circumference_points = [(x * radius, y * radius) for x, y in circumference_points_ratios]
circumference_points_ints = [(int(x), int(y)) for x, y in circumference_points]
for y in range(-30, 30):
nwjlyons /
Last active Aug 26, 2021
CGI Python Example
#! /usr/bin/env python
print("Content-Type: text/html")
print("<h1>Hello World</h1>")
nwjlyons /
Created Jan 29, 2021
Determine what blocks are used in a Wagtail StreamField.
def block_types_used_in_field(queryset, field_name: str) -> set:
used_block_types = set()
for page in queryset:
for block in getattr(page, field_name):
return used_block_types
# block_types_used_in_field(BlogPage.objects.all(), 'body')
nwjlyons / data.txt
Created Dec 10, 2020
Advent Of Code Day Four Part Two
View data.txt
ecl:hzl byr:1926 iyr:2010
pid:221225902 cid:61 hgt:186cm eyr:2021 hcl:#7d3b0c
hcl:#efcc98 hgt:178 pid:433543520
eyr:2020 byr:1926
ecl:blu cid:92
nwjlyons /
Created Nov 22, 2020
Python isinstance checks behave differently from type annotations
>>> isinstance(1, int)
True # 👍
>>> isinstance(1, float)
False # 👍
>>> isinstance(1.0, int)
False # 👍
>>> isinstance(1.0, float)
nwjlyons /
Last active Apr 5, 2021
Regression to the mean
import random
from collections import Counter
def regression_to_the_mean(n):
data = [random.randint(1, 10) for x in range(n)]
return sum(data) // len(data)
# 10
Counter([regression_to_the_mean(10) for x in range(50)]).most_common()
nwjlyons / irrational_numbers.ex
Last active Aug 17, 2020
Taking the square root of each number from 1 to 100, which of them are irrational
View irrational_numbers.ex
Enum.reject 1..100, fn i ->
square_root = Decimal.sqrt(i)
remainder = Decimal.rem(square_root, 1)
Decimal.eq?(remainder, 0)
nwjlyons / storage.ex
Last active Jul 18, 2020
Adaptor pattern in Elixir with compile time config checking
View storage.ex
defmodule Storage do
defmacro __using__(opts) do
quote bind_quoted: [opts: opts] do
@adaptor Keyword.fetch!(opts, :adaptor)
@config @adaptor.check_config(opts)
def save(io_device), do:, @config)
def delete(path), do:, @config)
nwjlyons / storage.go
Last active Jul 18, 2020
Adaptor pattern in Go
View storage.go
package main
import "fmt"
type StorageAdaptor interface {