Skip to content

Instantly share code, notes, and snippets.

View malcolmgreaves's full-sized avatar

Malcolm Greaves malcolmgreaves

View GitHub Profile
import threading
from abc import ABC
from concurrent.futures import Executor, Future
from multiprocessing import synchronize
from typing import Optional, Sequence, Union
__all__: Sequence[str] = ("BoundedPoolExecutor",)
class BoundedPoolExecutor(ABC, Executor):
@malcolmgreaves
malcolmgreaves / dataclass_destructing_syntax_support.py
Last active June 20, 2023 15:58
Adds syntactic support to destructing an @DataClass into field-values the same way a NamedTuple (or tuple) is.
from abc import ABC
from typing import Any, Iterable, Tuple
from dataclasses import dataclass
from functools import partial
@dataclass
class TupleDestructureSyntax(ABC):
"""Allows any @dataclass class to be decomposed into its fields & values as if it were a tuple.
@malcolmgreaves
malcolmgreaves / enum_parser.py
Last active May 3, 2023 22:40
Function to create a function that can parse `str -> Enum instance` and one from `Enum instance -> str`.
from enum import Enum
from typing import Callable, Optional, Type, TypeVar
class EnumStr(str, Enum):
pass
E = TypeVar('E', bound=EnumStr)
@malcolmgreaves
malcolmgreaves / git_branch_commits_in_ecr_repo.py
Created April 14, 2023 17:37
Get the image tags for an AWS ECR repo that exist within a branch. Prints them to STDOUT in descending order of pushed date.
"""
Use as:
REPO='ecr-repo-name' BRANCH='main' python git_branch_commits_in_ecr_repo.py
You can pipe this to `cut -f1 -s` to get the commits only. Or `-f2` to get the pushed by date only.
These values are separated by a tab (`"\t"`).
"""
import json
import os
@malcolmgreaves
malcolmgreaves / descent_torchscript_iterating.py
Last active February 21, 2023 21:01
descent_script: A function that recursively expands a model's children and TorchScripts everything, making it easy to identify which network parts are and are not TorchScript compatible.
import traceback
from abc import ABC, abstractmethod
from dataclasses import dataclass
from typing import Iterator, Sequence, Union
import torch
from core_utils.common import type_name
__all__: Sequence[str] = (
"descent_script",
@malcolmgreaves
malcolmgreaves / canceling_asyncio_tasks_101.py
Created February 14, 2023 01:47
Simple example showing how to safely cancel a heartbeat task.
async def heartbeat(interval=5):
while True:
await asyncio.sleep(interval)
async def safe():
loop = asyncio.get_running_loop()
h = loop.create_task(heartbeat())
await asyncio.sleep(1)
@malcolmgreaves
malcolmgreaves / keep_process_alive.py
Last active December 21, 2022 23:06
Python process watchdog: monitors a process for termination and restarts it as necessary.
import os
import sys
from multiprocessing import Process
from typing import Callable, Optional, Sequence
def make_logger():
import logging
log_level = logging.INFO
@malcolmgreaves
malcolmgreaves / calculate_greast_common_subpath.py
Last active October 15, 2022 19:34
Provides a function `calculate_greatest_common_subpath` which accepts a `Sequence[Path]` and finds the deepest common parent among them.
import os
from collections import Counter
from pathlib import Path
from typing import Iterator, List, Sequence, Tuple
def calculate_greatest_common_subpath(files: Sequence[Path]) -> Path:
"""Find the longest common subpath amongst a collection of files."""
if len(files) == 0:
raise ValueError("Must input at least one file!")
@malcolmgreaves
malcolmgreaves / lazy.py
Last active October 5, 2022 21:42
A function / method wrapper to lazily evaluate a function ONCE.
def cache_once(func):
"""Decorator that caches the result of a unary function or method.
REQUIREMENTS: The decorated function must be **referentially transparent**. I.e. the same arguments
result in the same output result, **every time**. With this guarantee, it is safe to
cache the value of a unary function from a single execution. If your funciton has any
side effects, it is not referentially transparent. Avoid any dependencies on mutable
state, I/O, etc.
To use, you simply wrap an existing function:
@malcolmgreaves
malcolmgreaves / fast_http_utils.py
Last active September 28, 2022 23:00
Massively concurrent HTTP request sending with Python.
import logging
import threading
import time
import traceback
from concurrent.futures import FIRST_COMPLETED, Future, ThreadPoolExecutor, wait
from contextlib import ExitStack
from dataclasses import dataclass
from time import sleep
from typing import Any, Callable, ContextManager, List, NamedTuple, Optional, Sequence, Set