Skip to content

Instantly share code, notes, and snippets.

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"
@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
@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 / 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 / detect_interlacing.sh
Last active December 28, 2015 05:49
FFMPEG: Detect interlacing.
ffmpeg -t 00:01:00 -i input.mpg -map 0:0 -vf idet -c rawvideo -y -f rawvideo /dev/null
@billyshambrook
billyshambrook / detect_interlacing_output.txt
Created November 13, 2013 17:37
FFMPEG: Detect interlacing output
[idet @ 0x17d10a0] Single frame detection: TFF:32 BFF:0 Progressive:0 Undetermined:42
[idet @ 0x17d10a0] Multi frame detection: TFF:58 BFF:0 Progressive:0 Undetermined:16
@billyshambrook
billyshambrook / parse_detect_interlacing_output.py
Created November 13, 2013 17:39
Parse FFMPEG detect interlacing filter output in python
import re
pattern = "\\[(.*)] Multi (.+):(.+):(?P\\w+) (.+):(?P\\w+) (.+):(?P\\w+) (.+):(?P\\w+)"
match = re.search(pattern, output)
scan = dict((k, int(v)) for k, v in match.groupdict().iteritems())
scan = max(scan, key=scan.get)
@billyshambrook
billyshambrook / retry_decorator_example.py
Last active December 28, 2015 06:09
Example usage for the retry decorator.
import boto.sqs
""" Defence from brief connection outages """
@retry(10)
def connect_to_sqs():
conn = boto.sqs.connect_to_region("us-west-2")
return conn.create_queue('myqueue')
@billyshambrook
billyshambrook / .zshtheme.sh
Last active August 29, 2015 13:55
ZSH Theme
function virtualenv_info {
[ $VIRTUAL_ENV ] && echo '('`basename $VIRTUAL_ENV`') '
}
function prompt_char {
git branch >/dev/null 2>/dev/null && echo '±' && return
hg root >/dev/null 2>/dev/null && echo '☿' && return
echo '○'
}
@billyshambrook
billyshambrook / arguments_in_decorator.py
Last active August 29, 2015 13:56
Passing around arguments in decorators
"""
Create argument within decorator and pass to decorated method.
Key things that make this possible is to ensure that your decorated method allows *args and **kwargs to be passed.
You then within the decorator append whatever to kwargs and/or args.
"""
def decorate_my_decorator(f):
def function_my_decorator(*args, **kwargs):
kwargs['somethings'] = 'hello'
return f(*args, **kwargs)
return function_my_decorator