Skip to content

Instantly share code, notes, and snippets.

View benauthor's full-sized avatar

Evan Bender benauthor

View GitHub Profile
@benauthor
benauthor / overhead.py
Last active August 27, 2018 20:34
Ballpark reference on function call, thread, process overhead
"""
Ballpark reference on function call, thread, process overhead
bender@dishoftheday:work$ python3 overhead.py 1000
forloop: 0.030994415283203125 ms
funcloop: 0.17786026000976562 ms
threadloop: 110.0301742553711 ms
threadloop2: 85.56795120239258 ms
threadloop_p: 77.57711410522461 ms
procloop: 5104.6202182769775 ms
@benauthor
benauthor / configured_spring_test.java
Last active July 11, 2018 13:44
Minimal spring-configured test setup
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = MyConfigurationClass.class)
public class MyTest {
@Autowired
private MyWhateverType myConfiguredBean;
@Test
public void testMyConfiguredBean() {
System.out.println("works");
@benauthor
benauthor / unicode_safe_repr.py
Created May 10, 2018 19:07
python unicode-safe __repr__ pattern
# This is a pattern I like but I always forget the details
class Boo(object):
def __init__(self):
self.foo = u"☃"
def __unicode__(self):
return u"Boo(foo={})".format(self.foo)
def __str__(self):
@benauthor
benauthor / profile_decorator.py
Created March 27, 2018 22:49
Python profiling decorator
from cProfile import Profile
from functools import wraps
import pstats
def profiled(f):
"""
Decorator for profiling a function
To use:
@benauthor
benauthor / retriable.py
Created February 28, 2018 19:54
python retries decorator
from time import sleep
class retriable(object):
"""
A retrying decorator with backoff.
Use it like so:
@retriable(tries=60, initial_backoff=60, backoff_multiplier=1)
@benauthor
benauthor / clean_old_kernels.sh
Created November 28, 2017 20:15
desktop linux still is a disaster
#!/bin/bash
echo 'cleaning old kernels. fuck you, ubuntu.'
df -h /boot
echo 'all versions'
dpkg --list | grep -P "linux-image"
echo 'maybe delete'
dpkg --list | grep -P "linux-image-\d" | tr -s " " | cut -d " " -f 2 | sort | head -n -1
@benauthor
benauthor / fake_statsd.py
Created November 7, 2017 19:00
fake statsd server
#!/usr/bin/env python
"""
A local 'statsd' server for dev purposes
"""
from __future__ import print_function
import socket
import sys
def main(port):
#!/bin/bash
args="--merged"
echo "delete some merged branches:"
{ git branch $args | grep -E -v "master|stable|prod|\*" | cut -c 3- | while read -r branchname; do
read -r -u 3 -p "Delete $branchname (y/n)? " answer
case ${answer:0:1} in
y|Y )
git branch -d "$branchname"
;;
@benauthor
benauthor / decisiontree.py
Created July 12, 2017 20:58
decision tree sketch
class AttrDict(dict):
def __init__(self, *args, **kwargs):
super(AttrDict, self).__init__(*args, **kwargs)
self.__dict__ = self
class Choices(dict):
pass
@benauthor
benauthor / schema_dump.sh
Created March 16, 2017 20:56
Dump a pretty mysql schema without all the noise
#!/bin/bash
mysqldump -d \
--skip-add-drop-table \
--skip-add-locks \
--skip-disable-keys \
--skip-set-charset $@ \
| grep -v SET | head -n -2 | tail -n +7