Skip to content

Instantly share code, notes, and snippets.

@SF-300
SF-300 / actor.py
Last active July 27, 2025 23:09
Minimal asyncio-based externally-driven actor system
import asyncio as aio
import itertools
import logging
import typing as t
import dataclasses
import contextvars
from uuid import uuid4
from enum import IntEnum
from contextvars import ContextVar
from dataclasses import dataclass
import typing as t
import asyncio as aio
class Producer[T](t.Protocol):
async def put(self, item: T) -> None: ...
def put_nowait(self, item: T) -> None: ...
def multi_put_nowait[T](queues: t.Iterable[Producer[T]], item: T) -> None:
@SF-300
SF-300 / pubsub.py
Last active July 12, 2025 10:00
Temporal.io cross-workflow pub-sub implementation
import contextlib
import functools
import json
import typing as t
import hashlib
from types import MethodType
import pydantic
import pydantic_core
from temporalio import workflow as wf
@SF-300
SF-300 / delays.py
Last active July 3, 2023 18:23
Declaratively describe delays - mostly for retry actions.
"""
This module provides a declarative way to describe delay strategies for controlling the interval between repeated actions, such as retrying a network request.
There are two categories of strategies:
1. Root Strategies: These provide the base logic for calculating delay time.
- Linear: Increases delay time linearly.
- Exponential: Increases delay time exponentially.
2. Modifier Strategies: These adjust the delay time calculated by a root strategy.
@SF-300
SF-300 / komorebi_async_events.py
Last active November 17, 2023 21:12
async reading of win32 named pipe containing komorebi window manager events
# NOTE(zeronineseven): Heavily based on https://gist.github.com/denBot/4136279812f87819f86d99eba77c1ee0
import asyncio
import contextlib
import json
from contextlib import AsyncExitStack
from pathlib import Path
from typing import AsyncContextManager, Protocol, AsyncIterator, Never
import win32event
import win32pipe
@SF-300
SF-300 / winuds.pyx
Created April 2, 2023 12:05
UDS under Windows POC
from ctypes import windll, Structure, c_char, c_ushort, c_char_p, POINTER, c_int, WinError
from ctypes.wintypes import WORD
from typing import Literal, TypeAlias
__all__ = "test",
_ExtraInfo: TypeAlias = Literal["peername", "socket", "sockname"]
@SF-300
SF-300 / namespace.py
Last active March 23, 2023 11:06
More convenient syntax for SimpleNamespace instances creation.
from types import SimpleNamespace
from typing import Any
__all__ = "NamespaceMeta", "Namespace"
class NamespaceMeta(type):
"""Provides namespacing infrastructure.
This metaclass provides an alternative (and hopefully more convenient) syntax for creating 'types.SimpleNamespace'
instances using 'class' syntax.
@SF-300
SF-300 / sigslot.py
Last active September 10, 2024 11:38
Simple signal pattern implementation with asyncio support.
import weakref
import inspect
from asyncio import Future
from typing import Any, Callable, TypeVar, Protocol, Awaitable, Generator
__all__ = "Listenable", "Signal",
_T = TypeVar("_T", covariant=True)
@SF-300
SF-300 / anyof_constraint.py
Last active January 18, 2023 16:14
Any-of constraint building using SQLAlchemy
import operator as op
import sqlalchemy as sa
__all__ = "anyof_constraint",
def anyof_constraint(*cols, at_least_one: bool = False) -> sa.CheckConstraint:
if len(cols) < 2:
raise ValueError("At least 2 columns MUST be provided to create any-of constraint!")
@SF-300
SF-300 / composite.py
Last active January 9, 2023 12:24
Postgres composites for SQLAlchemy 1.4
from typing import TypeAlias, Type
from frozendict import frozendict
import sqlalchemy as sa
import sqlalchemy.types
import sqlalchemy.event
import sqlalchemy.schema
import sqlalchemy.sql.sqltypes
__all__ = "define_composite", "CreateComposite", "DropComposite"