Skip to content

Instantly share code, notes, and snippets.

@sebastibe
sebastibe / runner.py
Created March 22, 2012 02:50 — forked from carljm/runner.py
Unittest2 test discovery and real dotted-path named test selection for Django
"""
An alternative Django ``TEST_RUNNER`` which uses unittest2 test discovery from
a base path specified in settings, rather than requiring all tests to be in
``tests`` module of an app.
If you just run ``./manage.py test``, it'll discover and run all tests
underneath the ``TEST_DISCOVERY_ROOT`` setting (a path). If you run
``./manage.py test full.dotted.path.to.test_module``, it'll run the tests in
that module (you can also pass multiple modules).
@sebastibe
sebastibe / iter_islast.py
Created September 9, 2012 00:09
Finding the last item in a loop
def iter_islast(iterable):
""" iter_islast(iterable) -> generates (item, islast) pairs
Generates pairs where the first element is an item from the iterable
source and the second element is a boolean flag indicating if it is the
last item in the sequence.
"""
it = iter(iterable)
prev = it.next()
for item in it:
apt-get install zlib1g-dev
apt-get install g++
export VENV=$VIRTUAL_ENV
mkdir $VENV/packages && cd $VENV/packages
curl -O http://oligarchy.co.uk/xapian/1.2.12/xapian-core-1.2.12.tar.gz
curl -O http://oligarchy.co.uk/xapian/1.2.12/xapian-bindings-1.2.12.tar.gz
tar xzvf xapian-core-1.2.12.tar.gz
@sebastibe
sebastibe / flask_local_static_files.py
Last active October 10, 2015 20:38
serving static files locally in Flask
from flask import Flask
app = Flask(__name__)
if app.config['DEBUG']:
from werkzeug.wsgi import SharedDataMiddleware
import os.path
app.wsgi_app = SharedDataMiddleware(app.wsgi_app, {
'/static': os.path.join(os.path.dirname(__file__), 'static')
})
@sebastibe
sebastibe / dropdown_stop_propagation.js
Last active October 10, 2015 21:28
don't clear bootstrap dropdown when sending form
$('.dropdown form').on('click', function (e) {
e.stopPropagation()
})
@sebastibe
sebastibe / runinenv.sh
Created October 9, 2012 02:24 — forked from parente/runinenv.sh
run a command in virtualenv, useful for supervisord
#!/bin/bash
VENV=$1
if [ -z $VENV ]; then
echo "usage: runinenv [virtualenv_path] CMDS"
exit 1
fi
. ${VENV}/bin/activate
shift 1
echo "Executing $@ in ${VENV}"
exec "$@"
@sebastibe
sebastibe / on_the_fly_form.py
Last active October 13, 2015 02:17
create on the fly Django ModelForm for a given model
from django import forms
# http://stackoverflow.com/questions/3915024/dynamically-creating-classes-python
OnTheFlyForm = type("OnTheFlyForm", (forms.Form,), fields)
# ModelForm
def get_on_the_fly_form(model):
class OnTheFlyModelForm(forms.ModelForm):
class Meta:
model = model
@sebastibe
sebastibe / borg_design_pattern.py
Created January 1, 2013 02:10
All instances share one same state.
# http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66531.
class Borg(object):
__shared_state = {}
def __init__(self):
self.__dict__ = self.__shared_state
@sebastibe
sebastibe / conf.py
Last active December 10, 2015 14:50
Sphinx conf.py for the documentation of a Django project.
import sys
from os.path import abspath, join, dirname
DOC_ROOT = abspath(dirname(dirname(__file__)))
BASE_PATH = dirname(DOC_ROOT)
PROJECT_PATH = join(BASE_PATH, 'api')
sys.path.append(BASE_PATH)
sys.path.append(PROJECT_PATH)
// Assuming we are working with the ASCII character set. 256 unique values.
//
// First, check that the length of the string is under the number of unique values.
// If it is greater, the string does not have all unique characters
//
// Second, create an array of boolean values of size of unique characters.
// Traverse the string and flag the array value from false to true when a character is hit.
// When the character is hit a second time, return false.
bool hasUniqueChars(char * string)