Skip to content

Instantly share code, notes, and snippets.

@mhthies
mhthies / fahclient.service
Created April 26, 2020 14:24
Systemd unit file for supervising the Folding@home Client (replacement for the old-style init script)
# To be placed in /etc/systemd/system/fahclient.service
# Remove /etc/init.d/FAHClient.
# Use `systemctl enable/disable fahclient` to enable/disable auto start.
[Unit]
Description=Folding@home Client
After=network.target
Requires=network.target
[Service]
@mhthies
mhthies / 70-network-switch.sh
Last active October 4, 2024 09:57
OpenWRT: Switch TL-WR902AC network configuration with hardware switch
#!/bin/ash
# Switch network configs according to the position of the 3way-switch, with help of the slide-switch package.
# Setup:
# * install `slide-switch` package (opkg install slide-switch)
# * Setup network interface `lan` as bridge interface with bridge device 'br-lan'
# * Setup network interface `wan` without physical interface
# * Place this file in /etc/hotplug.d/button/70-network-switch.sh
@mhthies
mhthies / count_wikipedia_german.py
Last active February 17, 2019 16:19
Special version of zxcvbn's count_wikipedia.py to count the word frequency in the german Wikipedia.
#!/usr/bin/python3
# Copyright (c) 2012-2016 Dan Wheeler and Dropbox, Inc.
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
@mhthies
mhthies / generator_bound_variable.py
Last active November 15, 2017 14:52
Lazy evaluation with bound variables kills you
x = [1,2,3]
y = ['α', 'β', 'γ']
# List comprehension works
g = [["{}_{}".format(a,b) for b in y] for a in x]
print(g)
# output: [['1_α', '1_β', '1_γ'], ['2_α', '2_β', '2_γ'], ['3_α', '3_β', '3_γ']]
@mhthies
mhthies / m3u_copy.py
Last active January 15, 2017 12:19
Python script to copy all files contained in an (url encoded) m3u playlist into a different directory. (Preserving original directory structure or prefixing filenames with index.)
#!/usr/bin/env python3
import shutil
import os
import argparse
import urllib.parse
import math
# Parse command line arguments
parser = argparse.ArgumentParser(description='Copy all music files listed in an m3u file to a destination directory.')
parser.add_argument('playlist', type=str, help='The m3u playlist file')
@mhthies
mhthies / fastfourrank.py
Created June 15, 2016 10:08
Find largest and 2nd largest of four elements with minimal comparsions
# based on http://stackoverflow.com/a/3628777
def fast_max(a,b,c,d):
if (a > b):
firstmax = a
firstmin = b
else:
firstmax = b
firstmin = a
if (c > d):