Skip to content

Instantly share code, notes, and snippets.

View xkcd2585.py
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 Dec 13, 2020
Generate a 82MB implementation of tic tac toe
View generate_tic_tac_toe.py
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 Oct 23, 2020
Create closures in Python without default arguments
View closure_decorator.py
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
metadecorators to construct decorators that only transform function arguments or return values
View transform_decorator.py
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
simple argparse wrapper
View coco.py
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 Mar 17, 2020 — forked from dotchetter/PollCache.py
Call functions through the PollCache object for only receiving output from the function if new output is detected.
View PollCache.py
# 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 Feb 1, 2019
Generalised hashable types in Python
View halfmutable.py
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):
View curry.py
@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 Feb 15, 2021
Find all occurrences of for ... else
View find_for_else.py
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
View test_threading.py
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