Skip to content

Instantly share code, notes, and snippets.

View Simsso's full-sized avatar

Timo Denk Simsso

View GitHub Profile
@Simsso
Simsso / retry.py
Created May 14, 2024 22:47
Retry annotation for Python
import functools
import time
def retry(max_retries=3, delay_s=2):
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
retries = max_retries
while retries > 0:
try:
def synchronized(func):
"""Wraps a function with a threading lock."""
lock = threading.Lock()
def wrapper(*args, **kwargs):
with lock:
return func(*args, **kwargs)
return wrapper
@Simsso
Simsso / string-compression.js
Created April 15, 2022 22:32
Experiments with string compression for storing maps of the game "Crusades" in the URL
const allowedChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=_"
const examples = [
"1010AAAAAAAAAAAAX_oX_oX_oX_oX_oFmUAAAaqQX_oZD4X_oTMgTMgJmQX_oZU8AAAEzIZD4OZYOZYTMgJmQFmUEzIX_oFVQEzIX_oEzIOZYTMgAAAEzIZgUZgUEzIZrsX_oaN0EzIAAAAAAEzIakkaCcEzIZJkEzIEzIJmQAAAOZYEzIX_oFmUEzIX_oX_oFVQJmQAAAOZYZU8a7UEzIAAATMgZD4TMgAAAAAAX_oX_oTMgEzIAAATMgX_oX_oAAAAAAZU8TMgTMgAAAAAAAAAFVQX_oX_oX_oX_oAAAAAAAAA",
"1010AAAAAAEzIEzIX_oX_oX_oX_oX_oAAAAAAZrsaN0X_oX_oJmQTMgFmUX_oAAAAAAZJkEzIEzITMgTMgJmQX_oX_oAAAAAAX_oEzIFVQTMgOZYEzIZU8EzIEzIAAAX_oaqQEzIOZYTMgEzIX_oZU8EzIAAAAAAX_oZD4JmQOZYTMgFmUX_oZU8EzIFVQEzIX_oOZYTMgTMgEzIX_oEzIEzIX_oZD4X_oOZYTMgEzIEzIakkFmUEzIZD4OZYOZYTMgTMgX_oZgUaCcEzIEzIOZYOZYTMgTMgJmQa7UEzIEzIEzI",
"2010AAAAAAAAAAAAAAAAAAAAAEzIEzIAAAAAAAAAEzIFmUAAAAAAEzIEzIAAAAAAAAAEzIFVQEzIAAAEzIEzIEzIFVQTMgTMgX_oX_oX_oX_oX_oZgUX_oEzIAAAAAAAAAEzIEzIEzIEzIEzIZD4X_oX_oX_oX_oTMgJmQX_oJmQEzIaCcEzIFmUAAAEzIEzIZD4ZD4X_oX_obMYOZYTMgTMgTMgJmQEzIZU8ZgUX_oakkEzIOZYAAAEzIaN0X_oEzIFVQZD4OZYOZYOZYTMgTMgEzIFmUEzIEzI
@Simsso
Simsso / calendar-aggregation-violin.js
Last active August 14, 2021 13:58
Code for Google Apps Script which reads a calendar and writes aggregated information into a Google spreadsheet. Details can be found in this blog post: https://timodenk.com/blog/populate-sheet-with-calendar-data/
const TIME_MIN = new Date('2020-10-27');
const CAL_ID = 'simssosdenk@gmail.com';
const QUERIES = ['violin lesson', 'violin practice'];
const RESULT_BATCH_SIZE = 40;
// App Script console testing endpoint
function main() {
loadWeeklyPracticeTime().forEach(x => console.log(x));
}
@Simsso
Simsso / multi-cursor-cheatsheet.md
Last active August 19, 2021 12:30
Hotkeys for multi cursor action in three different IDEs (for Mac)
Action Sublime VSCode PyCharm
Select next occurrence Cmd + D Cmd + D Alt + J
Select all occurrences Control + Cmd + G
Break selection into one cursor per line Cmd + Shift + L Shift + Option + I Option + Mouse selection
@Simsso
Simsso / raspi-photo-ssh.sh
Created August 8, 2020 10:04
Connects to a raspi, takes a photo, downloads it, and shows it (on Mac)
#!/bin/bash
set -euo pipefail
IFS=$'\n\t'
timestamp=$(date +%s)
ip=192.168.1.102
echo "Taking a photo..."
ssh pi@$ip "mkdir -p cam && cd cam && raspistill -o cam-$timestamp.jpg"
@Simsso
Simsso / vector-lookup-in-matrix.py
Created June 3, 2020 15:50
TensorFlow code for performing a lookup of values in matrix rows using the indices stored in a vector.
import tensorflow as tf
def vec_lookup_in_mat(mat: tf.Tensor, vec: tf.Tensor):
"""
Performs a lookup of values in the matrix rows using the indices stored in the vector.
For example, given the matrix [[1, 2, 3], [4, 5, 6]] and the vector [0, 2], the function would return a vector
consisting of the entry with index zero in the first row and index 2 in the second row, i.e. [0, 6].
:param mat: Tensor of shape [m, n, ...], values will be looked up from it; dtype any
:param vec: Tensor of shape [m], indices for each row of the matrix; dtype int32
@Simsso
Simsso / default.ino
Last active April 5, 2020 07:57
Arduino sketch boilerplate
// library imports
#define SERIAL_DEBUG
// global variables
void setup() {
#ifdef SERIAL_DEBUG
Serial.begin(9600);
Serial.println("Initializing...");
@Simsso
Simsso / article.tex
Last active May 24, 2019 17:51
LaTeX Simple Boilerplate
\documentclass[a4paper]{article}
\usepackage[utf8]{inputenc}
% math
\usepackage{amsmath}
\usepackage{amssymb} % special set sybmols e.g. for real numbers R
\usepackage{bm} % bold math
\usepackage{isomath}
% bracket fix (see https://tex.stackexchange.com/questions/2607)
@Simsso
Simsso / acc.py
Created July 6, 2018 13:26
TensorFlow Classification Accuracy Op
acc = tf.reduce_mean(tf.cast(tf.equal(tf.argmax(softmax, axis=1), tf.cast(labels, tf.int64)), 'float'), name='acc')