Skip to content

Instantly share code, notes, and snippets.

@gvx
gvx / xkcd2585.py
Created February 24, 2022 01:14
Little script to verify https://xkcd.com/2585/
from functools import partial
from pint import UnitRegistry, Unit, Quantity
ureg = UnitRegistry()
def rounded_conversion(from_: Quantity, to: Unit) -> Quantity:
return Quantity(round(from_.to(to).magnitude), to)
@gvx
gvx / generate_tic_tac_toe.py
Created December 13, 2020 20:21
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
gvx / closure_decorator.py
Created October 23, 2020 14:11
Create closures in Python without default arguments
import dis
from typing import Iterable, TypeVar, Any
from collections.abc 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
gvx / transform_decorator.py
Created May 18, 2020 13:21
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)
@wraps(decorated)
def wrapper(*args, **kwargs):
return transformation(decorated(*args, **kwargs))
return wrapper
@gvx
gvx / coco.py
Created May 15, 2020 17:53
simple argparse wrapper
from inspect import signature, Signature, Parameter
from argparse import ArgumentParser
POSITIONAL = (Parameter.POSITIONAL_ONLY, Parameter.POSITIONAL_OR_KEYWORD)
def parse_args(parser, argv=None):
return parser.parse_args(argv).__dict__
class Command:
def __init__(self, f, parser):
@gvx
gvx / PollCache.py
Last active March 17, 2020 17:37 — forked from dotchetter/PollCache.py
Call functions through the PollCache object for only receiving output from the function if new output is detected.
# disclaimer: untested code ahead! see https://www.reddit.com/r/Python/comments/fk4wal/i_needed_an_object_that_can_with_one_instance/fkrbodb/
from dataclasses import dataclass
from enum import Enum, auto
from typing import Any, Callable, Tuple
@dataclass
class CacheValue:
result: Any
times_changed: int = 1
@gvx
gvx / halfmutable.py
Created February 1, 2019 21:33
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):
@dataclass
class Curried:
f: Callable
def __call__(self, *args, **kwargs):
f = partial(self.f, *args, **kwargs)
try:
signature(f).bind()
except TypeError:
return type(self)(f)
else:
@gvx
gvx / find_for_else.py
Last active February 15, 2021 10:21
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