Skip to content

Instantly share code, notes, and snippets.

View pmeier's full-sized avatar

Philip Meier pmeier

View GitHub Profile
@pmeier
pmeier / main.py
Last active November 29, 2024 13:42
Convert any* string to kebab-case or snake_case
import re
def delimited_case(s: str, *, delimiter: str) -> str:
return re.sub(
r"(([a-z0-9])(?=[A-Z][a-zA-Z0-9])|([A-Z0-9])(?=[A-Z0-9][a-z]))",
rf"\1{delimiter}",
re.sub(r"[^a-zA-Z0-9]+", delimiter, s),
)
@pmeier
pmeier / main.py
Created October 30, 2024 13:51
Haversine distance matrix numpy
import numpy as np
def haversine_distance_matrix(lats, longs):
lats = np.radians(lats.reshape(-1, 1))
longs = np.radians(longs.reshape(1, -1))
return 2 * np.arcsin(np.sqrt((1 - np.cos(lats - lats.T) + np.cos(lats) * np.cos(lats.T) * (1 - np.cos(longs - longs.T))) / 2))
@pmeier
pmeier / unittest_multi_threaded.py
Last active August 28, 2024 09:58
Decorator for running multi threaded unittests
import unittest
from concurrent.futures import ThreadPoolExecutor
def multi_threaded(*, threads=2):
def decorator(test_cls):
for name, test_fn in test_cls.__dict__.copy().items():
if not (name.startswith("test") and callable(test_fn)):
continue
from typing import Any
from metadata_filter import MetadataFilter, MetadataFilterOperator
# https://docs.trychroma.com/usage-guide#using-where-filters
OPERATOR_MAP = {
MetadataFilterOperator.AND: "$and",
MetadataFilterOperator.OR: "$or",
MetadataFilterOperator.EQ: "$eq",
MetadataFilterOperator.NE: "$ne",
@pmeier
pmeier / remove_annotations.py
Created January 12, 2023 10:51
Remove Python 3 annotations from a codebase
import functools
import pathlib
import sys
import libcst as cst
def main(root):
root = pathlib.Path(root)
fn = functools.partial(remove_annotations, annotations_remover=AnnotationRemover())

Benchmark script and results for torchvision.transforms.functional v1 vs v2

Run benchmark.py to reproduce the results.

To add a new benchmark, add a BenchmarkConfig to BENCHMARK_CONFIGS in configs.py

@pmeier
pmeier / download.py
Created June 9, 2022 19:05
Simple download function in Python using requests and tqdm
import pathlib
from urllib.parse import urlparse
import requests
import tqdm
def download(url, root=".", *, name=None, chunk_size=32 * 1024):
root = pathlib.Path(root)
root.mkdir(exist_ok=True, parents=True)
@pmeier
pmeier / README.md
Created July 16, 2021 14:42
Hi Tony!

Hope this works!

Hopefully you can read this 🎉

@pmeier
pmeier / revert_submodule_updates.py
Created June 14, 2021 06:39
Revert submodule commits accidentally included in the most recent commit
import os
import pathlib
import re
import shlex
import subprocess
import sys
def main():
if git("status", "--porcelain"):
@pmeier
pmeier / type_promotion_array_api.py
Created May 21, 2021 09:51
Check inter-category type promotion behavior of 0d-tensors for array API compatibility
import itertools
from typing import Collection
import networkx as nx
# overwrite this with the array API that you want to test
import numpy as array_api
def maybe_add_dtype(