Skip to content

Instantly share code, notes, and snippets.

@nwjlyons
nwjlyons / queryset.py
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.
View queryset.py
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=[obj.id for obj in selected])[:max_length])
return (selected + fallback)[:max_length]
@nwjlyons
nwjlyons / circle.py
Last active Apr 5, 2021
Plot points along circumference using Trigonometry
View circle.py
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
nwjlyons / index.py
Last active Aug 26, 2021
CGI Python Example
View index.py
#! /usr/bin/env python
print("Content-Type: text/html")
print("")
print("<h1>Hello World</h1>")
@nwjlyons
nwjlyons / block_types_used_in_field.py
Created Jan 29, 2021
Determine what blocks are used in a Wagtail StreamField.
View block_types_used_in_field.py
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):
used_block_types.add(block.block_type)
return used_block_types
# block_types_used_in_field(BlogPage.objects.all(), 'body')
@nwjlyons
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
iyr:2010
iyr:2018
eyr:2026
@nwjlyons
nwjlyons / integer_float.py
Created Nov 22, 2020
Python isinstance checks behave differently from type annotations
View integer_float.py
>>> isinstance(1, int)
True # 👍
>>> isinstance(1, float)
False # 👍
>>> isinstance(1.0, int)
False # 👍
>>> isinstance(1.0, float)
@nwjlyons
nwjlyons / main.py
Last active Apr 5, 2021
Regression to the mean
View main.py
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
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)
end
@nwjlyons
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: @adaptor.save(io_device, @config)
def delete(path), do: @adaptor.save(path, @config)
end
end
@nwjlyons
nwjlyons / storage.go
Last active Jul 18, 2020
Adaptor pattern in Go
View storage.go
package main
import "fmt"
type StorageAdaptor interface {
Save(string)
Delete(string)
}