Skip to content

Instantly share code, notes, and snippets.

View L3viathan's full-sized avatar
🦊

Jonathan Oberländer L3viathan

🦊
View GitHub Profile
@L3viathan
L3viathan / brailleplot.py
Created May 30, 2025 15:15
Plotting with braille characters
import click
from math import ceil
COLORS = {
0: (0, 0, 0),
1: (133, 153, 0),
2: (42, 161, 152),
3: (38, 139, 210),
4: (108, 113, 196),
5: (211, 54, 130),
@L3viathan
L3viathan / contract.py
Created August 13, 2024 12:09
Protect parts of your code with a contract. Contract-protected code can't be (accidentally) changed.
import inspect
import ast
import hashlib
import sys
import textwrap
from contextlib import contextmanager
def indent(line):
return len(line) - len(line.lstrip(" "))
@L3viathan
L3viathan / intersection.py
Created July 21, 2024 18:32
Add intersection types to Python
import typing
import fishhook
class IntersectionMeta(type):
def __instancecheck__(self, other):
return all(isinstance(other, t) for t in self.types)
def __repr__(self):
if hasattr(self, "types"):
return " & ".join(t.__name__ for t in self.types)
@L3viathan
L3viathan / README.md
Created June 11, 2024 11:25
Column-based parallelization

Requires the dont library

@L3viathan
L3viathan / dots.py
Last active January 8, 2024 08:46
Infinite progress indicator thing
import sys
from time import sleep
from contextlib import contextmanager
@contextmanager
def dots():
def _dots():
while True:
for i, char in enumerate("⠁⠃⠇⡇⣇⣧⣷⣿"):
if i:
@L3viathan
L3viathan / pytest_allow_extensionless.py
Created September 21, 2023 20:32
Tiny pytest plugin that allows collecting any file regardless of extension. If you don't manually specify test files, this will probably crash.
from importlib.util import spec_from_loader, module_from_spec
from importlib.machinery import SourceFileLoader
import pytest
class ModuleWithoutExtension(pytest.Module):
def _importtestmodule(self):
print(self.path, type(self.path))
spec = spec_from_loader(self.path.name, SourceFileLoader(self.path.name, str(self.path)))
@L3viathan
L3viathan / expiry.py
Created August 18, 2023 14:48
Allow annotated variables as lasting only for a bit
import sys
import threading
from time import sleep
from typing import Annotated, get_origin, get_args
from collections import namedtuple
timeout = namedtuple("timeout", "s")
@L3viathan
L3viathan / sumtypes.py
Last active July 24, 2023 15:31
Sum types in Python
class SubscriptableSneaky:
def __init__(self, ns, name):
self.ns = ns
self.args = []
self.name = name
def __getitem__(self, items):
if not isinstance(items, tuple):
items = items,
self.args = [item.name for item in items]
@L3viathan
L3viathan / matchmeta.py
Created February 16, 2023 19:10
custom match-case rules
class MatchMeta(type):
def __instancecheck__(cls, inst):
return cls.isinstance(inst)
def matching(fn):
class cls(metaclass=MatchMeta):
isinstance = fn
return cls
@L3viathan
L3viathan / tssh
Last active November 22, 2023 08:39
Start a tmux pane for each of the specified hosts, in synchronized-panes mode
#!/bin/bash
if (( $# > 6 )); then
layout=tiled
else
layout=even-vertical
fi
hosts=( "$@" )
tmux new-window
tmux send-keys "ssh ${hosts[0]}"
for i in $(seq 1 $((${#hosts[@]} - 1)))