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
View getattr_plugin.py
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
View resolve_once.py
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
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
@antonagestam
antonagestam / angle.py
Created Mar 26, 2019
Memory efficient and immutable DegreeAngle implementation
View angle.py
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.
[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
You can’t perform that action at this time.