Skip to content

Instantly share code, notes, and snippets.

@wware
wware / perf.py
Last active May 18, 2017 19:35
An ill-designed performance measuring thing for Python, use at your own risk - I'd prefer line_profiler except I'm on Python 2.7 and cannot upgrade.
import pprint
import re
import logging
import timeit
import traceback
from functools import wraps
from contextlib import contextmanager
_WIDTH = 50
@wware
wware / running.py
Created June 27, 2017 03:12
Generate mu, sigma for a stream of numbers, incrementally.
import collections
class RunningAverage(object):
"""
>>> r = RunningAverage()
>>> r
<__main__.RunningAverage object at 0x...>
>>> r.update(1)
>>> r.update(2)
@wware
wware / pye.sh
Last active July 6, 2017 22:50
A shell script that manages my Python virtualenv, rebuilds and re-pip-installs my code, helps me with pdb debugging and performance profiling.
#!/bin/bash
PYTHON=python
help() {
echo "This is a virtualenv swiss army knife script."
echo ""
echo "Usage: $0 [options] # -S, -s, -i -c"
echo " $0 [options] foobar.py --arg1 --arg2"
echo ""
@wware
wware / memoize.py
Last active May 7, 2018 16:11
Handy memoization decorator for Python functions and methods, also see https://github.com/wware/python-hacks/tree/master/memoize
import unittest
_cache = {}
def memoize(func):
"""
Decorator that memoizes a function. The memoized function can use "fresh=True"
to avoid using cached results.
@param func: the function to be memoized
@wware
wware / bash_without_getopt_s.sh
Last active February 1, 2018 21:31
Apparently getopt(1) for Bash is buggy, according to http://mywiki.wooledge.org/BashFAQ/035#getopts, where getopts is OK. But it's not that hard to just write scripts that don't use either of them.
#!/bin/bash
help() {
echo "Bash script that parses cmd line args without getopt(s)"
echo "Adapted from https://stackoverflow.com/questions/192249"
echo " -h, --help print this help message"
echo " -f, --foo set FOO variable"
echo " -b, --bar set BAR variable"
echo " -d, --debug enable debug (doesn't really do anything here)"
echo " <anything else> becomes a positional argument"
@wware
wware / log_caller_frame.py
Last active November 28, 2017 19:51
Sometimes you want a Python function that includes a `logging.info()` or `logging.debug()` call, but you'd really prefer the logged filename and line number to be from the caller of your function, not your function itself. This is the solution to that problem, at least for Python 2.7. Also see https://stackoverflow.com/questions/12980512/custom-…
import os
import logging
from contextlib import contextmanager
logging.basicConfig(
format='%(asctime)-15s %(levelname)s %(filename)s:%(lineno)d %(message)s',
level=logging.INFO
)
def __LINE__():

Feature toggle decorator

This is a feature toggle scheme for the system I maintain at my job. The goal is to be able to easily toggle functions that have been recently pushed to production, so if things go bad, we have an easy mitigation plan.

Create a database table with three columns, Key (string), Value (boolean), LastChanged (timestamp). The purpose of the LastChanged column is just for record keeping to decide when a feature is deemed trustworthy. The several for a given key string present the history of switching the feature on and off,

#!/usr/bin/env python
import logging
from flask import Flask
from twisted.internet import ssl, reactor
from twisted.web.wsgi import WSGIResource
from twisted.web.server import Site
from twisted.python.threadpool import ThreadPool
from twisted.application import service
#!/usr/bin/env python
import argparse
import logging
import os
import sys
import pprint
from textwrap import dedent
HERE = os.path.dirname(__file__)
@wware
wware / bash_waiter.sh
Created February 22, 2018 23:36
I want to run a couple Bash processes in parallel in the background. If I use "wait", I might end up waiting for a long-running process to finish after another process has already failed, and in this case I want to stop as soon as I see the failure.
#!/bin/bash
wait_all() {
# As soon as one of the background processes creates the EPIC_FAIL file,
# kill all background processes, rm EPIC_FAIL, and stop looping.
while [ $(jobs | grep -v Done | wc -l) -ne 0 ]; do
sleep 1
if [ -f EPIC_FAIL ]; then
rm -f EPIC_FAIL
echo OUCH