Skip to content

Instantly share code, notes, and snippets.

@hjwp
hjwp / nvim-lua-config-writeup.md
Created December 21, 2023 11:21
new (neo) vim config from scratch, in lua, write-up.

Thought I'd provide a few more details in case anyone else fancies taking the plunge into neovim / lua-only config / declaring vim-bankruptcy on their current config and starting from scratch.

Objectives

My key objectives were:

  • keepings some, but not necessarily all, of my vim settings + custom keymaps
  • LSP features, specifically
    • autocompletion
  • jump-to-definition
@hjwp
hjwp / reimplement_counter.py
Last active October 17, 2023 10:25
reduce vs non-reduce versions of Counter / frequencies
"""
finding use cases for "reduce" in python, attempt #2.
"reimplementing Counter()
cf https://docs.python.org/3/library/collections.html#collections.Counter
cf https://clojuredocs.org/clojure.core/frequencies
"""
import functools
@hjwp
hjwp / alternatives_to_reduce.py
Last active October 24, 2023 22:28
A few examples of problems solved with reduce vs more "traditional" (pythonic?) ways
vehicles = [
{"category": "Cars", "id": "Audi"},
{"category": "Cars", "id": "Mercedes"},
{"category": "Motorbikes", "id": "Ducati"}
]
expected = {
"Cars": ["Audi", "Mercedes"],
"Motorbikes": ["Ducati"],
}

How to do clean architecture in Django - some options

Assumptions & Context

Assumes an existing Django app with substantial complexity, and the intuition that "clean architecture" might solve some problems.

Django's sweet spot is rapid application development, CRUD, low to moderate domain complexity / application size.

Are Green Electricity Tariffs a Waste of Time?

I've been looking into "green energy" tariffs recently. tl;dr: mostly, they're equivalent to voting Green in a safe Labour seat. A protest vote, with minimal immediate impact.

This was prompted by a friend expressing skepticism about them, words to the effect of "just rearranging slices in the pie chart, without changing any of the sizes".

import hashlib
import os
import shutil
from pathlib import Path
def hash_file(path):
hasher = hashlib.sha1()
with path.open("rb") as file:
while buf:= file.read(hasher.block_size):
@hjwp
hjwp / test_enums.py
Created October 27, 2020 14:03
Better string enums
import random
from enum import Enum, IntEnum
class BRAIN(str, Enum):
SMALL = "small"
MEDIUM = "medium"
GALAXY = "galaxy"
def __str__(self) -> str:
@hjwp
hjwp / default-acl.json
Last active May 4, 2020 20:36
Eventstore perms bug repro
[{
"eventId": "7c314750-05e1-439f-b2eb-f5b0e019be72",
"eventType": "update-default-acl",
"data": {
"$userStreamAcl" : {
"$r" : ["$admin", "$ops", "test-user"],
"$w" : ["$admin", "$ops", "test-user"],
"$d" : ["$admin", "$ops"],
"$mr" : ["$admin", "$ops"],
"$mw" : ["$admin", "$ops"]
@hjwp
hjwp / mymodule.py
Last active February 23, 2020 20:46
def foo() -> int:
return 42
@hjwp
hjwp / bowling.py
Last active November 23, 2019 12:23
Bowling TDD Kata
STRIKE = 'X-'
def score_frame(frame, next_frame):
if frame[:2] == STRIKE:
if next_frame == STRIKE:
return 20
if next_frame is LAST_FRAME:
first_ball_next_frame = int(frame[2])
second_ball_next_frame = int(frame[3])
else: