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 / geometry.swift
Created October 17, 2023 10:51
Swift Named Parameter Examples
import Foundation
struct Point {
var x: Float64
var y: Float64
init(_ x: Float64, _ y: Float64) {
self.x = x
self.y = y
}
@Feuermurmel
Feuermurmel / sitecustomize.py
Created March 22, 2020 11:04
Relative paths in Python stack traces.
import builtins
import os
import sys
from builtins import __import__
def fix_path(p: str):
if not p.startswith('/'):
return p
@Feuermurmel
Feuermurmel / pi_digits.py
Last active February 17, 2020 12:09
I wrote this for fun. Slow as hell. Maybe broken. Don't use.
from math import floor
from fractions import Fraction as F
def pi_approx(bits):
def term(k):
return F(1, 16 ** k) * (F(4, 8 * k + 1) - F(2, 8 * k + 4) - F(1, 8 * k + 5) - F(1, 8 * k + 6))
return sum(term(k) for k in range(bits // 4 + 1))
@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))
@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 / 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
from contextlib import contextmanager
import fcntl
@contextmanager
def lock_file(path):
with open(path, 'wb') as file:
fcntl.flock(file, fcntl.LOCK_EX)
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)
@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):
@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))