Skip to content

Instantly share code, notes, and snippets.

[color]
ui = auto
[user]
name = Phil Tysoe
email = phil@***.com
[push]
default = simple
[alias]
lg = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative
st = status
@igniteflow
igniteflow / bdd_stub_generator.py
Last active August 29, 2015 13:57
Generate Python BDD style test stubs from a simple text file format
#!/usr/bin/env python
import sys
"""
Given a file of the following format:
MyClass
- My test case
- Another test case
- And another test case
@igniteflow
igniteflow / middleware.py
Created March 11, 2014 12:02
Django language middleware. Detect the user's browser language settings and activate the language. If the default language is not supported, try secondary options. If none of the user's languages are supported, then do nothing.
class LanguageMiddleware(object):
"""
Detect the user's browser language settings and activate the language.
If the default language is not supported, try secondary options. If none of the
user's languages are supported, then do nothing.
"""
def is_supported_language(self, language_code):
supported_languages = dict(settings.LANGUAGES).keys()
return language_code in supported_languages
@igniteflow
igniteflow / sharded_cache.py
Last active August 29, 2015 13:56
ShardedCache mixin for AppEngine
def bytes_to(bytes, to, bsize=1024):
a = {'k': 1, 'm': 2, 'g': 3, 't': 4, 'p': 5, 'e': 6}
r = float(bytes)
for i in range(a[to]):
r /= bsize
return r
class ShardedCache(object):
"""
@igniteflow
igniteflow / oracle-java-ubuntu.sh
Last active January 3, 2016 08:19
Install Oracle Java 7 on Ubuntu in a script i.e. no confirmation
#!/bin/bash
# Install Oracle Java 7 on Ubuntu in a script i.e. no confirmation
sudo add-apt-repository ppa:webupd8team/java -y
sudo echo debconf shared/accepted-oracle-license-v1-1 select true | debconf-set-selections
sudo echo debconf shared/accepted-oracle-license-v1-1 seen true | debconf-set-selections
sudo apt-get install oracle-java7-installer
sudo update-java-alternatives -s java-7-oracle
java -version
@igniteflow
igniteflow / assertRaisesWithMessage.py
Last active December 31, 2015 03:28
assertRaisesWithMessage. Example usage (with optional args and kwargs): self.assertRaisesWithMessages(ValidationError, ['Boom!!!'], my_function, my_arg, **{'foo': 'bar'})
def assertRaisesWithMessages(self, exception, messages, _callable, *args, **kwargs):
try:
_callable(*args, **kwargs)
except exception as e:
self.assertEqual(e.messages, messages)
@igniteflow
igniteflow / prepare-commit-msg
Last active December 29, 2015 16:09
Python prepare-commit-msg to insert [touch:ticket number] to every commit message. Note it adds it commented in order to abort gracefully, otherwise exiting the EDITOR would cause the commit to be actioned as [touch:ticket number] would be recognised as the message. Note that get_ticket_number() logic is custom. Change to extract the ticket numb…
#!/usr/bin/env python
"""
To enable save as /yourproject/.git/hooks/prepare-commit-msg
and make executable: chmod +rx prepare-commit-msg
"""
import sys
from subprocess import check_output
def get_ticket_number():
@igniteflow
igniteflow / sublime-shortcuts.md
Created November 15, 2013 12:06
Favourite Sublime Text 2 shortcuts

Ctrl + K, Ctrl + b = Toggle sidebar

@igniteflow
igniteflow / diff.sh
Created November 8, 2013 10:15
Open up a diff of the changes made on a Git branch against the parent branch
#!/bin/bash
PARENT_BRANCH=master
CURRENT_BRANCH=`git rev-parse --abbrev-ref HEAD`
DIVERGED_FROM_PARENT=`git merge-base HEAD $PARENT_BRANCH`
# git difftool --tool-help to see supported software
git difftool --no-prompt $CURRENT_BRANCH $DIVERGED_FROM_PARENT
@igniteflow
igniteflow / env-context.py
Last active December 12, 2023 16:43
A Python context manager for setting/unsetting environment variables
from contextlib import contextmanager
"""
Usage:
with env_var('MY_VAR', 'foo'):
# is set here
# does not exist here
"""