Skip to content

Instantly share code, notes, and snippets.

View clbarnes's full-sized avatar

Chris Barnes clbarnes

View GitHub Profile
@clbarnes
clbarnes / HCMST_ver_3.04.csv
Created October 15, 2019 00:55
How Couples Meet and Stay Together dataset, in CSV
We can't make this file beautiful and searchable because it's too large.
@clbarnes
clbarnes / install.sh
Last active March 12, 2026 10:18
Installing napari (and empanada) on an ARM mac
#!/bin/bash
# follow pixi installation instructions https://pixi.prefix.dev/latest/installation/
curl -fsSL https://pixi.sh/install.sh | sh
exec $SHELL
# create a new project using pixi in the directory `my_project`
pixi init my_project
cd my_project
@clbarnes
clbarnes / alt_meta_store.py
Last active January 21, 2026 11:51
A Zarr store which wraps another, and simply redirects requests for zarr.json objects to another object name
from typing import TYPE_CHECKING
from zarr.storage import WrapperStore
from zarr.core.common import ZARR_JSON
if TYPE_CHECKING:
from zarr.abc.store import (
Store,
RangeByteRequest,
OffsetByteRequest,
SuffixByteRequest,
@clbarnes
clbarnes / xml_to_json.py
Created March 3, 2025 14:51
XML to pydantic model/ JSON
from __future__ import annotations
from xml.etree import ElementTree as ET
from pydantic import BaseModel, Field
class XmlElement(BaseModel):
tag: str = Field(alias="#")
attributes: dict[str, str] = Field(alias="@", default_factory=dict)
children: list[str | XmlElement] = Field(alias="c", default_factory=list)
@clbarnes
clbarnes / cascade_deser.rs
Created August 22, 2025 22:44
Use serde JSON to decompose a serialised flat object into a tuple of different structs
use serde::de::DeserializeOwned;
trait JsonDeserializable {
fn deser<T: DeserializeOwned>(self) -> serde_json::Result<T>;
}
impl JsonDeserializable for serde_json::Value {
fn deser<T: DeserializeOwned>(self) -> serde_json::Result<T> {
serde_json::from_value(self)
}
@clbarnes
clbarnes / make_cmapnames.py
Last active June 17, 2025 11:15
Automatically generate Literal annotations for cmap names a la https://github.com/pyapp-kit/cmap/issues/105
from pathlib import Path
import json
scripts_dir = Path(__file__).resolve().parent
module_dir = scripts_dir.parent.joinpath("src", "cmap")
data_dir = module_dir.joinpath("data")
names = []
for rel_path in sorted(data_dir.glob("*/record.json")):
@clbarnes
clbarnes / classname.py
Created July 29, 2019 15:43
Get the fully-qualified class name of a python object
def classname(obj):
cls = type(obj)
module = cls.__module__
name = cls.__qualname__
if module is not None and module != "__builtin__":
name = module + "." + name
return name
@clbarnes
clbarnes / gcra.lua
Last active April 7, 2025 08:52
GCRA rate limiting algorithm implemented as a redis lua script
--[[
Load the script from this file into redis, storing its SHA1 hash as an environment variable
$ SCRIPT_SHA=$(redis-cli -x script load < gcra.lua)
Run the script by addressing its hash, with 1 key (as used by this script) called `myratelimitkey`,
with a rate limit of 2 attempts every 1 000 milliseconds (1 second).
$ redis-cli EVALSHA $SCRIPT_SHA 1 myratelimitkey 2 1000
@clbarnes
clbarnes / requirements.txt
Last active March 10, 2025 14:16
Read python extras from a standard requirements.txt
backports.strenum
molesq
hcl
yarqueue
#extra: ncollpyde
ncollpyde
#extra: test
@clbarnes
clbarnes / add_log_level.py
Last active February 6, 2025 08:37
Add a log level to python's logging mechanisms
# adapted from https://stackoverflow.com/a/35804945/2700168
import logging
def add_log_level(level_name, level_num, method_name=None):
"""
Adapted from https://stackoverflow.com/a/35804945/2700168
Comprehensively adds a new logging level to the `logging` module and the
currently configured logging class.