Skip to content

Instantly share code, notes, and snippets.

View antonagestam's full-sized avatar
🍉

Anton Agestam antonagestam

🍉
View GitHub Profile
@antonagestam
antonagestam / bash-base.sh
Created October 1, 2019 21:19
sane defaults for bash
#!/usr/bin/env bash
set -euo pipefail
(( ${BASH_VERSION%%.*} > 4 )) || echo 'Error: Outdated bash version ' && exit 1
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
antonagestam / running_sum_deque.py
Created May 6, 2019 18:00
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
@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 13:34
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
antonagestam / angle.py
Created March 26, 2019 18:51
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.
@antonagestam
antonagestam / .gitconfig
Last active March 20, 2019 15:49
git config
# 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 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 / handlers.py
Created March 15, 2019 15:38
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):
super().__init__()
self.instance = instance