This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
NewerOlder