Skip to content

Instantly share code, notes, and snippets.

View antonagestam's full-sized avatar
🍉

Anton Agestam antonagestam

🍉
View GitHub Profile

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 February 13, 2019 20:49
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
antonagestam / timed.py
Created February 5, 2019 16:50
Decorator for time limiting function calls in Python
import multiprocessing
import functools
import logging
from typing import Callable
class TimeLimitExceeded(Exception):
pass
@antonagestam
antonagestam / serializable.py
Last active April 21, 2019 19:54
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
defaults write com.apple.dock autohide-time-modifier -int 0; killall Dock
defaults write com.apple.Dock autohide-delay -float 0; killall Dock
@antonagestam
antonagestam / compose.py
Created January 9, 2019 10:10
Typed functional composition
from typing import Callable, TypeVar
import functools
A = TypeVar('A')
B = TypeVar('B')
C = TypeVar('C')
def combine(f: Callable[[B], C], g: Callable[[A], B]) -> Callable[[A], C]:
def h(x: A) -> C:
@antonagestam
antonagestam / cnf-diff.py
Last active December 19, 2018 17:11
Diff MySQL .cnf files
import re
import sys
import json
var_pattern = re.compile(r'^(?!\[)(?P<var>[A-z_-]+)\s*(?:=\s*(?P<val>.*))?$')
scope_pattern = re.compile(r'^\s*\[(?P<scope>[A-z_-]+)\]\s*$')
def get_lines(file_path):
with open(file_path, 'r') as f:
@antonagestam
antonagestam / reusing.py
Created December 3, 2018 20:59
Example of reusing objects in Python
class Reusing:
__free = []
def __new__(cls, *args, **kwargs):
try:
return cls.__free.pop()
except IndexError:
return super(Reusing, cls).__new__(cls)
def __del__(self):
@antonagestam
antonagestam / admin.sh
Created October 16, 2018 10:26
systemd service file for instantiated services of python-rq
$ systemctl enable rqworker@worker0{1..4}.service
$ systemctl start rqworker@worker0{1..4}.service
$ systemctl status 'rqworker@*'
$ journalctl -u 'rqworker@*'
@antonagestam
antonagestam / get_from.sh
Created September 23, 2018 08:39
get bash variables from a source
#!/usr/bin/env bash
set -euo pipefail
get_from () {
local file=$1
local variable=$2
echo "$(
source "$file" > /dev/null
eval "echo \"\$$variable\""