Skip to content

Instantly share code, notes, and snippets.

@billyshambrook
billyshambrook / retry.py
Last active April 27, 2020 03:11
Exception retry
# Retry decorator with exponential backoff
def retry(tries, delay=3, backoff=2):
"""
Retries a function or method if exception is raised.
delay sets the initial delay in seconds, and backoff sets the factor by which
the delay should lengthen after each failure. backoff must be greater than 1,
or else it isn't really a backoff. tries must be at least 0, and delay
greater than 0.
"""
@billyshambrook
billyshambrook / postdeactivate
Last active December 28, 2015 04:49
Virtualenvwrapper postdeactivate
#!/bin/zsh
# Global virtualenvwrapper postactivate, lives in $WORKON_HOME/postdeactivate
if [ $PRE_VENV_ACTIVATE_DIR ]; then
cd $PRE_VENV_ACTIVATE_DIR
unset PRE_VENV_ACTIVATE_DIR
unset CURRENT_PROJECT
fi
@billyshambrook
billyshambrook / postactivate
Created November 13, 2013 08:15
Virtualenvwrapper postactivate
#!/bin/zsh
# Global virtualenvwrapper postactivate, lives in $WORKON_HOME/postactivate
# Remove virtual env from start of PS1 as it's in RPROMPT instead
PS1="$_OLD_VIRTUAL_PS1"
PROJECT_DIR="$HOME/projects/$(basename $VIRTUAL_ENV)"
if [ -d $PROJECT_DIR ]; then
# If we aren't already within the project dir, cd into it
class my_error_handling(object):
def __enter__(self): pass
def __exit__(self, exc_type, exc_val, exc_tb):
if issubclass(exc_type, UnicodeError):
print "annoying"
elif issubclass(exc_type, ValueError):
print "hrmm"
elif issubclass(exc_type, OSError):
print "alert sysadmin"