Skip to content

Instantly share code, notes, and snippets.

View antonagestam's full-sized avatar
🍉

Anton Agestam antonagestam

🍉
View GitHub Profile
@antonagestam
antonagestam / eager-load.py
Created March 3, 2024 11:29
Eagerly load all kio modules, takes 2-3s
import kio.schema
from pkgutil import iter_modules
from pkgutil import resolve_name
import time
from pathlib import Path
schema_dir = Path(kio.schema.__file__).parent.resolve()
print(f"{schema_dir=}")
t0 = time.monotonic_ns()
@antonagestam
antonagestam / join_streams.py
Last active March 11, 2024 07:17
Merge results from multiple async generators into one single stream.
import asyncio
import random
from typing import TypeVar, AsyncGenerator
T = TypeVar("T")
async def read_into_queue(
task: AsyncGenerator[T, None],
queue: asyncio.Queue[T],
@antonagestam
antonagestam / kafka-meta.schema.json
Last active February 12, 2024 20:28
A JSONSchema to describe the shape of Apache Kafka's protocol message schemas.
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"additionalProperties": false,
"oneOf": [
{
"properties": {
"apiKey": {"type": "number"},
"type": {
"type": "string",
from typing import Callable
from typing import TypeVar
from phantom.predicates.generic import equal
from phantom import Predicate
T = TypeVar("T")
def apply_factory(factory: Callable[[T], Predicate[T]]) -> Predicate[tuple[T, T]]:
import time
import signal
import resource
def debug(signum, frame):
print(f"RSS: {resource.getrusage(resource.RUSAGE_SELF).ru_maxrss}")
print(f"{resource.getpagesize()=}")
@antonagestam
antonagestam / phantom-literal.py
Created June 9, 2021 08:47
Recipe for a powerful combination of Literals and phantom types ...
from phantom import Phantom
from typing import Literal, get_args, Union, Mapping
from phantom.predicates.collection import contained
LiteralCountry = Literal["SE", "DE", "DK"]
class PhantomCountry(str, Phantom, predicate=contained(get_args(LiteralCountry))):
...
@antonagestam
antonagestam / trunk-based-release-flow.md
Created February 20, 2021 12:25
Handling trunk-based releases with issues and tags

Handling trunk-based release approval with issues and tags

Requesting a release

Command to create a release.

make request-release <sha>
make request-release  # default to current trunk
@antonagestam
antonagestam / composable.py
Last active February 12, 2021 13:12
Functional composition with cute syntax + typing support.
from __future__ import annotations
from typing import TypeVar, Generic, Callable
Arg = TypeVar("Arg")
Ret = TypeVar("Ret")
NewRet = TypeVar("NewRet")
class Composable(Generic[Arg, Ret]):
def __init__(self, fn: Callable[[Arg], Ret]) -> None:
@antonagestam
antonagestam / choice-constraints-wip.py
Last active May 20, 2021 09:16
Introspective choice constraints for Django models.
# Note: This doesn't properly yield a delete+create when choices change.
# -> This can probably be remedied by requiring passing the model "path" (<app>.<model_name>) as
# an argument to __init__. Not ideal ...
from typing import Callable, Sequence
from django.db import models
from django.db.backends.base.schema import BaseDatabaseSchemaEditor
from django.db.models.constraints import BaseConstraint
from django.db.models.sql.query import Query
@antonagestam
antonagestam / dependent-dataclass-fields.py
Last active November 6, 2020 17:35
A (very naïve) implementation of dependent parsers for Python dataclasses.
from __future__ import annotations
import dataclasses
from typing import Annotated
from typing import Callable
from typing import Generic
from typing import Iterable
from typing import Mapping
from typing import Tuple
from typing import Type