Skip to content

Instantly share code, notes, and snippets.

View Feuermurmel's full-sized avatar
🐙

Feuermurmel Feuermurmel

🐙
  • Zürich, Switzerland
View GitHub Profile
@Feuermurmel
Feuermurmel / lvmstat.py
Created June 25, 2014 19:34
A script for LVM2 to display a quick overview over which LVs use space on which PVs.
#! /usr/bin/env python3.2
import sys, subprocess, argparse, itertools
class UserError(Exception):
def __init__(self, msg, *args):
super().__init__(msg.format(*args))
@Feuermurmel
Feuermurmel / prowl.py
Last active August 29, 2015 14:10
prowl.py
#! /usr/bin/env python3
import os, sys, argparse, subprocess, json, re, getpass, socket, requests
script_dir = os.path.dirname(os.path.realpath(__file__))
terminal_notifier = os.path.join(script_dir, 'terminal-notifier.app/Contents/MacOS/terminal-notifier')
def log(msg, *args):
@Feuermurmel
Feuermurmel / empty-1.scad
Last active August 29, 2015 14:16
OpenSCAD minkowski() test cases
minkowski() {
cube();
intersection() {
cube();
translate([2, 2, 2]) {
cube();
}
}
@Feuermurmel
Feuermurmel / retry.py
Created April 3, 2015 23:21
retry.py
#! /usr/bin/env python3
import sys, os, argparse, subprocess, time
class UserError(Exception):
def __init__(self, msg, *args):
super().__init__(msg.format(*args))
@Feuermurmel
Feuermurmel / example.py
Last active August 29, 2015 14:25
Python try-else
@contextlib.contextmanager
def temp_file_path(path):
"""
Context manager which returns slight variation of the specified path, which can be used to write a file to without clobbering an already existing file until the write is successful. The file at the returned path will either be moved onto the passed path (if the context is left normally) or deleted (if the context is left by throwing an exception).
"""
temp_path = path + '~'
dir_path = os.path.dirname(path)
if not os.path.exists(dir_path):
def partition_items(items, num_top_items, key=lambda x: x):
if items and 0 < num_top_items < len(items):
pivot = items[0]
smaller = []
larger = []
for i in items[1:]:
if key(i) < key(pivot):
smaller.append(i)
from contextlib import contextmanager
import fcntl
@contextmanager
def lock_file(path):
with open(path, 'wb') as file:
fcntl.flock(file, fcntl.LOCK_EX)
@Feuermurmel
Feuermurmel / method_lru_cache.py
Last active May 3, 2017 15:33
@method_lru_cache() decorator
from functools import lru_cache, wraps
from itertools import count
_method_lru_cache_sequence = count()
def method_lru_cache(*lru_args, **lru_kwargs):
"""
Like `functools.lru_cache` which can be used on methods of a class
@Feuermurmel
Feuermurmel / ca.lua
Created July 13, 2017 18:54
Cellular Automaton in Lua 5.1
--- Create a rule function which calculates the next value of a single cell in
-- a cellular automaton
-- @param rule
-- Rule number.
-- @param neighbours
-- Number of neighbours in each direction which affect the next state of a
-- cell. With 0, a cell only affects itself. With 1, the two immediate
-- neighbours also affect the cell.
function wolfram_ca_rule_fn(rule, neighbours)
return function(get_cell_fn)
@Feuermurmel
Feuermurmel / calldebug.py
Last active July 19, 2019 10:27
Script to show arguments with which a command is run
#! /usr/bin/env python3
import sys
def main():
args = sys.argv
for i, x in enumerate(args):
print('[{}] = {!r}'.format(i, x))