Skip to content

Instantly share code, notes, and snippets.

@tfeldmann
tfeldmann / djadmin_theme.js
Created June 24, 2024 15:19
Find the currently active django admin mode (light / dark)
function currentTheme() {
const currentTheme = localStorage.getItem("theme") || "auto";
const prefersDark = window.matchMedia("(prefers-color-scheme: dark)").matches;
if (currentTheme === "auto") {
return prefersDark ? "dark" : "light";
}
return currentTheme;
}
@tfeldmann
tfeldmann / format_consecutive.py
Last active July 20, 2023 15:04
Format consecutive numbers as ranges
from itertools import count, groupby
from typing import Iterable, Tuple
def group_consecutive(nums: Iterable[int]) -> Iterable[Tuple[int, ...]]:
counter = count()
for _, g_it in groupby(nums, lambda n: n - next(counter)):
yield tuple(g_it)
@tfeldmann
tfeldmann / platformio.ini
Created November 14, 2020 11:46
PlatformIO config for using the BME680 with an ESP32 using the Arduino framework
; PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html
@tfeldmann
tfeldmann / rot18.c
Last active July 9, 2020 09:22
ROT18 implementation in python and c
void rot18(char *c)
{
while (*c)
{
if (*c >= 'A' && *c <= 'Z')
*c = ('A' + (*c - 'A' + 13) % 26);
else if (*c >= 'a' && *c <= 'z')
*c = ('a' + (*c - 'a' + 13) % 26);
else if (*c >= '0' && *c <= '9')
*c = ('0' + (*c - '0' + 5) % 10);
@tfeldmann
tfeldmann / duplicates.py
Last active August 1, 2024 07:45
Fast duplicate file finder written in python
#!/usr/bin/env python
"""
Fast duplicate file finder.
Usage: duplicates.py <folder> [<folder>...]
Based on https://stackoverflow.com/a/36113168/300783
Modified for Python3 with some small code improvements.
"""
import os
import sys
@tfeldmann
tfeldmann / update.sh
Last active November 27, 2019 11:22
Update all pip packages, brew and brew casks (macOS) and backup a list of the installed software
#!/bin/sh
set -e
PIP=pip3
scriptdir=$(dirname "$0")
listsdir="$scriptdir/lists"
mkdir -p "$listsdir"
echo "Update all python packages..."
@tfeldmann
tfeldmann / vector.py
Created June 16, 2016 13:48
A fast, feature-rich and easy-to-use python 2D vector class based on complex numbers
""" A python 2D vector class based on complex numbers
"""
import cmath
class Vector(complex):
@classmethod
def fromPolar(cls, r, phi):
return cls(cmath.rect(r, phi))
import itertools
def lowpass(img):
"""
A simple pure python low pass (antialiasing) filter.
Applies a gaussian blur on a 2D list of floats.
"""
width = len(img[0])
height = len(img)
@tfeldmann
tfeldmann / i2c_scanner.ino
Created April 18, 2013 09:13
A I2C Scanner for Arduino
// --------------------------------------
// i2c_scanner
//
// Version 1
// This program (or code that looks like it)
// can be found in many places.
// For example on the Arduino.cc forum.
// The original author is not known.
// Version 2, Juni 2012, Using Arduino 1.0.1
// Adapted to be as simple as possible by Arduino.cc user Krodal
@tfeldmann
tfeldmann / face_recognition.py
Last active December 11, 2015 15:49
Face recognition in Python + OpenCV. I used Homebrew to install OpenCV, change your path to the haarcascade if needed.
import cv
HAAR_CASCADE_PATH = "/usr/local/Cellar/opencv/2.4.3/share/OpenCV/" \
"haarcascades/haarcascade_frontalface_default.xml"
CAMERA_INDEX = 0
def detect_faces(image):
faces = []
detected = cv.HaarDetectObjects(image, cascade, storage, 1.2, 2,
cv.CV_HAAR_DO_CANNY_PRUNING, (100,100))