Skip to content

Instantly share code, notes, and snippets.

Anton Agestam antonagestam

Block or report user

Report or block antonagestam

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
from mypy.plugin import Plugin, FunctionContext
from typing_extensions import Protocol
from typing import TypeVar, Any
from types import new_class
def handle_function_call(function_ctx: FunctionContext):
T = TypeVar('T', bound=Any)
attr = function_ctx.args[0][0].value
from asyncio import Lock
from collections import defaultdict
from functools import wraps
from typing import Any
from typing import Awaitable
from typing import DefaultDict
from typing import Dict
from typing import TypeVar
from typing_extensions import Protocol
antonagestam /
Created May 6, 2019
A Deque subclass that maintains a running sum of it's values
from typing import Deque, Iterable
T = float
class RunningSumDeque(Deque[T]):
def __init__(self, iterable: Iterable[T], maxlen: int):
super().__init__(iterable, maxlen)
self.__sum = sum(self)
from typing import Type, Iterator
import contextlib
def assert_raises(expected: Type[Exception]) -> Iterator:
except Exception as e:
assert isinstance(e, expected), (
antonagestam /
Created May 6, 2019
SQLAlchemy typed IntegerEnum
from typing import Type, TypeVar
from enum import IntEnum
import sqlalchemy
E = TypeVar('E', bound=IntEnum)
class IntegerEnum(sqlalchemy.types.TypeDecorator):
impl = sqlalchemy.Integer
antonagestam /
Created Mar 26, 2019
Memory efficient and immutable DegreeAngle implementation
from __future__ import annotations
from collections import OrderedDict
from typing import Any
import weakref
class InstanceCache(type):
Custom metaclass for DegreeAngle that reuses cached objects to optimize
memory consumption by storing weak references to them in an ordered dict.
View .gitconfig
# This is Git's per-user configuration file.
name = Anton Agestam
email =
excludesfile = ~/.gitignore_global
ui = true
wsErrorHighlight = all
antonagestam /
Last active Mar 20, 2019
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__(
antonagestam /
Created Mar 15, 2019
Django Model Log Handler
import logging
from django.db import models
from django.db.models.functions import Concat
class DatabaseStreamHandler(logging.StreamHandler):
def __init__(self, instance: models.Model, field: str):
self.instance = instance

Setting up serial on Raspberry Pi 3

Disable bluetooth and setup serial on /dev/ttyAMA0

  1. /boot/config.txt must contain:
You can’t perform that action at this time.