Skip to content

Instantly share code, notes, and snippets.

View SimonLammer's full-sized avatar

Simon Lammer SimonLammer

View GitHub Profile
@SimonLammer
SimonLammer / Makefile-tag_increment
Created October 14, 2023 18:35
Makefile that creates tags with incrementing suffix
GIT_REMOTE=origin
help:
@echo Use any of the following targets:
@grep '^[a-z]' Makefile | cut -d : -f 1
_submit:
# Argument ASS needs to be set for this target!
n=$(shell git tag -l | grep ${ASS} | cut -d '-' -f 2 | sort -n | tail -n 1) ;\
[ -z "$$n" ] && n=0 ;\
@SimonLammer
SimonLammer / Makefile-help
Last active October 14, 2023 18:33
Makefile help (list available make targets)
# have an intermediate _help, to move the actual help target to the file end
_help: help
#= Section 1
target1:
echo target1
target2: target1
echo target2
@SimonLammer
SimonLammer / gist:ac0da816617efd42f626c9157523f80c
Created August 12, 2023 13:38
Read password from stdin into keyctl
# probably want to start a new session, so the key is removed once the script closes
keyctl new_session >/dev/null
# read tye key into keyctl
stty -echo
echo -n "Enter ssh password: "
password_key=$(read -s password && echo -n "$password" | keyctl padd user pw @s)
stty echo
echo ""
@SimonLammer
SimonLammer / snakes_and_ladders.sage
Created July 10, 2023 07:13
Probability to win Snakes&Ladders after n rounds
snakes = {
17: 7,
54: 34,
62: 19,
64: 60,
87: 24,
93: 73,
95: 75,
99: 78,
}
@SimonLammer
SimonLammer / generator_vs_callback.py
Last active May 2, 2023 14:32
Performance comparison of python generator vs callback
#!/bin/python3
import timeit
from time import process_time_ns
def gen(n, rec):
if rec > 0:
yield from gen(n, rec-1)
else:
for i in range(n):
@SimonLammer
SimonLammer / numpy_array_access.py
Last active August 1, 2022 08:29
Comparing performance of repeated single-element numpy array access with index vs view
#!/bin/python3
from datetime import datetime, timedelta
import numpy as np
import timeit
np.set_printoptions(linewidth=np.inf, formatter={'float': lambda x: format(x, '1.5E')})
def indexed(arr, indices, num_indices, accesses):
s = 0
@SimonLammer
SimonLammer / python-logger.py
Created March 23, 2022 11:11
Python decorator to change loglevel within a function.
import logging
LOG = logging.getLogger(__name__)
logging.basicConfig(format="[%(levelname)8s] %(message)s (%(funcName)s@%(filename)s:%(lineno)s)")
LOG.setLevel(logging.DEBUG)
LOGLEVEL_FUNCS = {} # autoreload workaround https://github.com/ipython/ipython/issues/11099#issuecomment-382029692
def loglevel(level):
def outer(func):
LOG.debug(f"loglevel {level} for function {func.__qualname__}")
@SimonLammer
SimonLammer / Dockerfile
Last active November 1, 2021 18:52
Scientific python 3.9 container on rpi
FROM debian:10-slim
RUN apt-get update \
&& apt-get upgrade -y \
&& echo "deb http://deb.debian.org/debian testing non-free contrib main" >> /etc/apt/sources.list \
&& apt-get update \
&& apt-get install -y --no-install-recommends libcrypt1 \
&& apt-get install -y --no-install-recommends libc6 \
&& apt-get upgrade -y \
&& apt-get install -y --no-install-recommends \
@SimonLammer
SimonLammer / msmtprc
Last active October 1, 2021 12:07
Configuration of msmtp to send emails via gmail
# Resources this configurations is inspired by
# https://techrapiduk.blogspot.com/2017/04/send-email-on-raspberry-pi-with-msmtp.html
# https://www.raspberrypi.org/forums/viewtopic.php?t=244147
# Fix log permissions
# https://askubuntu.com/a/1269230/776650
account default
host smtp.gmail.com
port 465
@SimonLammer
SimonLammer / restart_unless_ssh.sh
Last active October 3, 2021 11:01
Restart the server unless ssh works.
#!/bin/sh
USERNAME=user
SSH_CMD="ssh user@domain -i /home/user/.ssh/no_pw"
MAX_TRIES=4
RESTART_DELAY=`expr 15 \* 60` # seconds
tmpfile=/tmp/restart_unless_ssh-`date +%s`
lock=/tmp/restart_unless_ssh-lock