Skip to content

Instantly share code, notes, and snippets.

View kjxlstad's full-sized avatar
🤌

Jonathan Kjølstad kjxlstad

🤌
View GitHub Profile
# /// script
# requires-python = ">=3.11"
# dependencies = [
# "numpy",
# "sentence-transformers",
# ]
# ///
import argparse
import urllib.request
@kjxlstad
kjxlstad / nested_progress.py
Last active September 25, 2024 11:31
Python rich automatic nested progress bars
from collections import OrderedDict
from typing import Iterable, Any, TypeVar, Dict, Callable
from operator import length_hint
from rich.progress import Progress
T = TypeVar("T")
def multi_track() -> Callable[[Iterable[T], str, Dict[str, Any]], Iterable[T]]:
@kjxlstad
kjxlstad / eight_queens_solver.py
Created January 16, 2024 13:00
Z3 eight queens solver. Finds all solutions.
from itertools import combinations
from z3 import Int, Solver, And, Or, sat, Not
def draw_model(solution):
grid = [
["." if (x, y) not in solution else "Q" for y in range(8)]
for x in range(8)
]
return "\n".join(" ".join(row) for row in grid)
@kjxlstad
kjxlstad / no_custom
Created May 22, 2023 06:23
xkb keymap: ansi with æøå
// based on a keyboard map from an 'xkb/symbols/no' file
default partial alphanumeric_keys
xkb_symbols "basic" {
include "us(basic)"
name[Group1]="US (Norwegian)";
key <AC01> { [ a, A, aring, Aring ] };
key <AD03> { [ e, E, ae, AE ] };
import torch
from torch import nn
class MinPool2d(nn.Module):
def __init__(self, *args, **kwargs):
super().__init__()
self.max_pool = nn.MaxPool2d(*args, **kwargs)
def forward(self, x):
@kjxlstad
kjxlstad / libgpt.py
Last active March 3, 2023 00:22
Why bother copying the codes suggestions from chatgpt, hwne you can just directly implement them. Infers function body based on function name and annotations. Complete with unit tests.
import requests
def _gpt(prompt):
response = requests.post(
"https://dashboard.scale.com/spellbook/api/app/REDACTED",
json={"input": prompt},
headers={"Authorization": "Basic REDACTED"},
)
return response.json()["text"]
@kjxlstad
kjxlstad / group_finder.py
Last active March 3, 2023 00:23
Given a set of subsets and a set of elements. Finds all combinations of subsets that exactly contain the elements.
import itertools
from typing import Union, Iterator
NestedSet = tuple[set[int], ...]
def _missing_singletons(groups: NestedSet, cap: int) -> NestedSet:
return tuple({e} for e in range(cap + 1) if {e} not in groups)