Skip to content

Instantly share code, notes, and snippets.

from functools import partial
from pint import UnitRegistry, Unit, Quantity
ureg = UnitRegistry()
def rounded_conversion(from_: Quantity, to: Unit) -> Quantity:
return Quantity(round(, to)
gvx /
Created Dec 13, 2020
Generate a 82MB implementation of tic tac toe
from typing import Optional, Literal, Iterator
Player = Literal['X', 'O']
BoardEntry = Literal['X', 'O', ' ']
Board = tuple[BoardEntry, BoardEntry, BoardEntry, BoardEntry, BoardEntry, BoardEntry, BoardEntry, BoardEntry, BoardEntry]
def new_board() -> Board:
return tuple(' ' * 9)
def is_draw(board: Board) -> bool:
gvx /
Created Oct 23, 2020
Create closures in Python without default arguments
import dis
from typing import Iterable, TypeVar, Any
from import Callable
from types import CodeType
F = TypeVar('F', bound=Callable)
def extract_mapping(names: tuple[str, ...], mapping: dict[str, int]) -> dict[int, int]:
return {names.index(k): v for k, v in mapping.items() if k in names}
gvx /
Created May 18, 2020
metadecorators to construct decorators that only transform function arguments or return values
from functools import wraps, partial
def transform_return(transformation, decorated=None):
if decorated is None:
return partial(transform_return, transformation)
def wrapper(*args, **kwargs):
return transformation(decorated(*args, **kwargs))
return wrapper
gvx /
Created May 15, 2020
simple argparse wrapper
from inspect import signature, Signature, Parameter
from argparse import ArgumentParser
def parse_args(parser, argv=None):
return parser.parse_args(argv).__dict__
class Command:
def __init__(self, f, parser):
gvx /
Last active Mar 17, 2020 — forked from dotchetter/
Call functions through the PollCache object for only receiving output from the function if new output is detected.
# disclaimer: untested code ahead! see
from dataclasses import dataclass
from enum import Enum, auto
from typing import Any, Callable, Tuple
class CacheValue:
result: Any
times_changed: int = 1
gvx /
Created Feb 1, 2019
Generalised hashable types in Python
from dataclasses import dataclass, field, fields, MISSING
def immutable_field(*, default=MISSING, default_factory=MISSING):
return field(default=default, default_factory=default_factory, metadata={'frozen': True})
def mutable_field(*, default=MISSING, default_factory=MISSING):
return field(default=default, default_factory=default_factory, compare=False)
def halfmutable(_cls=None, *, init=True, repr=True, order=False):
def wrap(cls):
class Curried:
f: Callable
def __call__(self, *args, **kwargs):
f = partial(self.f, *args, **kwargs)
except TypeError:
return type(self)(f)
gvx /
Last active Feb 15, 2021
Find all occurrences of for ... else
import ast
from pathlib import Path
class Visitor(ast.NodeVisitor):
def __init__(self):
self.for_found = 0
self.for_else_found = 0
self.while_found = 0
self.while_else_found = 0
self.files_tried = 0
import threading
from time import perf_counter as time
import random
from statistics import median, stdev
results = {'thread': [], 'call': []}
end = 0
def inside():
global end