Skip to content

Instantly share code, notes, and snippets.

View hughdbrown's full-sized avatar

Hugh Brown hughdbrown

View GitHub Profile
@hughdbrown
hughdbrown / truncation.md
Last active March 13, 2025 19:33
Truncating numbers

The problem

So you are interested in rounding a number to its lowest multiple. Let's say you want numbers to be rounded to 10:

>>> for number in range(30):
...     print(f"{number}: {10 * (number // 10)}")
...
0: 0
1: 0
2: 0
@hughdbrown
hughdbrown / setup-aie5.sh
Created February 27, 2025 21:34
Setup AIE5 with upstream/main and origin/main
#!/bin/sh
set -eux -o pipefail
export GIT_USER=hughdbrown
export GIT_REPO_NAME_USER=AIE5 # What the user is going to call the repo
export GIT_REPO_NAME_AI_MAKER=AIE5 # What the repo is already called by AI_MAKER
mkdir -p ~/workspace/${GIT_USER}/maven.com/${GIT_REPO_NAME_USER}
cd ~/workspace/${GIT_USER}/maven.com/${GIT_REPO_NAME_USER}
@hughdbrown
hughdbrown / rust-trust.py
Created February 25, 2025 06:35
Check how many of the job entries on Who Is Hiring? pages are actually 'trust', not 'rust'.
>>> import requests
>>> IDS = {
42919502: "2025-02-01",
42575537: "2025-01-01",
42297424: "2024-12-01",
42017580: "2024-11-01",
41709301: "2024-10-01",
41425910: "2024-09-01",
41129813: "2024-08-01",
40846428: "2024-07-01",
@hughdbrown
hughdbrown / asyncio-local.py
Created February 14, 2025 19:21
Show that asyncio can be run on local files
#!/usr/bin/env python3
import asyncio
from pathlib import Path
async def line_count(filename):
try:
count = filename.read_text(encoding='utf-8').count('\n')
except:
count = -1
@hughdbrown
hughdbrown / w_function.py
Created February 5, 2025 15:27
Lambert's W function
#!/usr/bin/env python3
import math
# from sys import stderr
def w_function_dx(x):
return (x + 1) * math.exp(x)
def w_function(x, x0):
return x * math.exp(x) - x0
@hughdbrown
hughdbrown / random_numbers.py
Last active October 10, 2024 17:25
Test various strategies for creating arrays of random numbers, some with serial dependencies
import os
os.environ["NUMBA_LOOP_VECTORIZE"] = "0"
os.environ["NUMBA_SLP_VECTORIZE"] = "0"
from datetime import datetime
from numba import jit, uint32, uint64
import numpy as np
@hughdbrown
hughdbrown / top_sort.py
Last active August 12, 2024 21:04
Try graphlib.TopologicalSort
>>> from graphlib import TopologicalSorter
>>> data = {"A": ["B", "D"], "B": [], "D": [], "C": ["A"]}
>>> ts = TopologicalSorter(data)
>>> for a in ts.static_order():
... print(a)
...
B
D
A
@hughdbrown
hughdbrown / common-logging.py
Created August 5, 2024 16:03
Common python logging preamble
import logging
FORMAT = '%(asctime)s %(message)s'
logging.basicConfig(
format=FORMAT,
datefmt="%Y-%m-%dT%H:%M:%S",
# logger levels are: DEBUG, INFO, WARNING, ERROR, CRITICAL
level=os.environ.get('LOGLEVEL', 'INFO').upper(),
)
logger = logging.getLogger()
@hughdbrown
hughdbrown / who-is-hiring.txt
Created August 2, 2024 17:20
Count of "Who is hiring?" posts on Hacker News by month
-------------------- 2019-01-01 hacker-news-2019-01-01.txt: 782
-------------------- 2019-02-01 hacker-news-2019-02-01.txt: 702
-------------------- 2019-03-01 hacker-news-2019-03-01.txt: 759
-------------------- 2019-04-01 hacker-news-2019-04-01.txt: 757
-------------------- 2019-05-01 hacker-news-2019-05-01.txt: 829
-------------------- 2019-06-01 hacker-news-2019-06-01.txt: 675
-------------------- 2019-07-01 hacker-news-2019-07-01.txt: 767
-------------------- 2019-08-01 hacker-news-2019-08-01.txt: 741
-------------------- 2019-09-01 hacker-news-2019-09-01.txt: 615
-------------------- 2019-10-01 hacker-news-2019-10-01.txt: 733
@hughdbrown
hughdbrown / pokemon-grid.py
Created May 30, 2024 15:22
Take a single image and resale to pave it in a 3x3 grid, save new image
#!/usr/bin/env python3
from pathlib import Path
from PIL import Image, ImageChops
def trim(im):
bg = Image.new(im.mode, im.size, im.getpixel((0,0)))
diff = ImageChops.difference(im, bg)
diff = ImageChops.add(diff, diff, 2.0, -100)