Skip to content

Instantly share code, notes, and snippets.

View antonagestam's full-sized avatar
🍉

Anton Agestam antonagestam

🍉
View GitHub Profile
@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 / 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 / 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",
@antonagestam
antonagestam / bounded-fields.py
Last active August 7, 2023 07:51
Django model fields for easily creating fields with min and max validators and that renders with min and max attributes on the input element.
from typing import Dict, Optional, Any
from django.db import models
from django.core.validators import MinValueValidator, MaxValueValidator
from django import forms
class BaseBoundedField(models.Field):
def __init__(
self,
@antonagestam
antonagestam / CMakeLists.txt
Created February 14, 2017 23:28 — forked from adrien-f/CMakeLists.txt
CLion and Arduino via Platform.io
cmake_minimum_required(VERSION 3.2)
project(YourProject)
add_subdirectory(src)
add_subdirectory(lib)
@antonagestam
antonagestam / admin.sh
Created October 16, 2018 10:26
systemd service file for instantiated services of python-rq
$ systemctl enable rqworker@worker0{1..4}.service
$ systemctl start rqworker@worker0{1..4}.service
$ systemctl status 'rqworker@*'
$ journalctl -u 'rqworker@*'
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 / 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