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
antonagestam / running_sum_deque.py
Created May 6, 2019
A Deque subclass that maintains a running sum of it's values
View running_sum_deque.py
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)
View assert_raises.py
from typing import Type, Iterator
import contextlib
@contextlib.contextmanager
def assert_raises(expected: Type[Exception]) -> Iterator:
try:
yield
except Exception as e:
assert isinstance(e, expected), (
@antonagestam
antonagestam / integerenum.py
Created May 6, 2019
SQLAlchemy typed IntegerEnum
View integerenum.py
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.
[user]
name = Anton Agestam
email = git@antonagestam.se
[core]
excludesfile = ~/.gitignore_global
[color]
ui = true
[diff]
wsErrorHighlight = all
@antonagestam
antonagestam / bounded-fields.py
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.
View bounded-fields.py
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 / handlers.py
Created Mar 15, 2019
Django Model Log Handler
View handlers.py
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):
super().__init__()
self.instance = instance
View serial.md

Setting up serial on Raspberry Pi 3

Disable bluetooth and setup serial on /dev/ttyAMA0

  1. /boot/config.txt must contain:
enable_uart=1
dtoverlay=pi3-disable-bt
@antonagestam
antonagestam / same.py
Created Feb 13, 2019
A class whose instances there may at any point only exist one of ...
View same.py
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
antonagestam / timed.py
Created Feb 5, 2019
Decorator for time limiting function calls in Python
View timed.py
import multiprocessing
import functools
import logging
from typing import Callable
class TimeLimitExceeded(Exception):
pass
@antonagestam
antonagestam / serializable.py
Last active Apr 21, 2019
Typed JSON serializability in Python
View serializable.py
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.