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
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
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:
antonagestam /
Created Feb 13, 2019
A class whose instances there may at any point only exist one of ...
import weakref
class Same:
__the = None
def __new__(cls, *args, **kwargs):
if cls.__the is None or cls.__the() is None:
obj = super(Same, cls).__new__(cls)
cls.__the = weakref.ref(obj)
antonagestam /
Created Feb 5, 2019
Decorator for time limiting function calls in Python
import multiprocessing
import functools
import logging
from typing import Callable
class TimeLimitExceeded(Exception):
antonagestam /
Last active Apr 21, 2019
Typed JSON serializability in Python
from __future__ import annotations
import json
from typing import Any, Dict, List, Tuple, Union, cast
from typing_extensions import Protocol, runtime
# Type has to be ignored until mypy support recursive types (should be
# soon-ish, see mypy#731). This means that type errors one step down in a dict
# hierarchy will not be detected, since the "recursed" type will be interpreted
You can’t perform that action at this time.