Skip to content

Instantly share code, notes, and snippets.

View wolever's full-sized avatar

David Wolever wolever

View GitHub Profile
@wolever
wolever / ezabc.py
Last active March 16, 2017 00:20
A very simple abstract base class implementation which verifies, at compile time, that subclasses implement all required abstract methods.
import inspect
class ABCMeta(type):
""" A very simple abstract base class implementation which verifies,
at compile time, that subclasses implement all required abstract
methods.
Methods are considered abstract if their implementation contains the
string ``raise NotImplementedError``.
@wolever
wolever / watchdog.py
Created December 2, 2016 23:57
A simple watchdog for long-running Python processes
"""
A simple watchdog for long running processes which may stall for some reason or
another.
If the main thread hasn't logged progress (by updating
``self.last_progress_time``) in WATCHDOG_HARD_KILL_TIMEOUT, the watchdog
thread will log an error containing the stack trace of all currently running
threads then use ``kill -9`` to kill the main process.
Assumes that a process monitor like supervisor or systemd will then restart
@wolever
wolever / bcsx.rst
Last active December 9, 2022 07:41
Blockchain Santa Exchange (working title): organizing fair and secure secret santa gift exchanges on the blockchain

By shazow and wolever.

Problem

How can N secret santa gift exchange participants agree on a gift-giver -> gift-recipient bijection such that:

@wolever
wolever / testcase_helpers.py
Last active November 22, 2016 00:01
Testcase helper examples
"""
Testcase Helpers (pending a better name) make it easy to compose elements
which require setup/teardown on each testcase.
For example, consider a testcase which needs to clean a Redis database after
each run::
class TestRedis(TestCase):
def cleanup_redis(self):
for key in self.cxn.keys("*"):
@wolever
wolever / all_in_one_break.py
Last active November 11, 2016 22:56
PDB Tricks
"""
I have F8 in Vim (see pdb.vim) bound to insert this statement.
It will:
- Still work if running tests under nosetests
- Can be disabled if it's accidentally added in a loop by setting "st.off = True"
- The XXX is highlighted in Vim, and BREAK is easy to grep for
"""
from nose.tools import set_trace as st; st.__dict__.get("off") or st() #XXX: BREAK
@wolever
wolever / task_debouncer.py
Created November 4, 2016 22:38
A task debouncer for Celery.
import time
import redis
def get_redis_connection():
return redis.connect()
class TaskDebouncer(object):
""" A simple Celery task debouncer.
@wolever
wolever / username_or_email_backend.py
Last active October 24, 2016 18:13
Django authentication backend which will authenticate based on either username or email
from django.contribu.auth.backends import ModelBackend
from django.contrib.auth import get_user_model
class UsernameOrEmailBackend(ModelBackend):
""" An authentication backend which will allow users to login using
either their username or their email.
In settings.py, add:
AUTHENTICATION_BACKENDS = [
$ coverage run test.py
...
----------------------------------------------------------------------
Ran 3 tests in 0.000s
OK
$ coverage report
Name Stmts Miss Cover
-----------------------------------------------------------------------------------------------------------------------
/Users/wolever/code/sandbox/env/sandbox/lib/python2.7/site-packages/pkg_resources/_vendor/six 444 442 1%
@wolever
wolever / spinlock.py
Created April 12, 2016 00:16
Because sometimes you just need a spinlock
@contextmanager
def spinlock(key, suffix=":lock", duration=1, spinsleep=0.01):
""" A simple Redis-based spinlock for situations where pessimistic locking
makes life simpler, and the chance of collisions is low enough that
a spinlock is a reasonable tradeoff.
The lock is held for at most ``duration`` seconds (after which it's
ignored), and polled every ``spinsleep`` seconds.
For example::
@wolever
wolever / entropy.py
Last active February 7, 2016 03:48
Entropy: for when you'd prefer your entropy deterministic and immutable
import textwrap
from numpy.random import RandomState
def _entropy_mk_method(name):
orig_func = RandomState.__dict__[name]
args = None
if orig_func.__doc__:
# Most functions have their signature in the first line of their
# docstring (ex, "beta(a, b, size=None)"). Split that out so we can