Skip to content

Instantly share code, notes, and snippets.

View sprin's full-sized avatar

Steffen Prince sprin

  • Medellín, Colombia
View GitHub Profile
@sprin
sprin / char_field_no_empty_string.py
Created August 30, 2012 23:55
Django: CharField that will save an empty string as NULL
class CharFieldNoEmptyString(CharField):
def get_prep_value(self, value):
# Cast empty strings to null before saving to the DB.
value = (
super(CharFieldNoEmptyString, self)
.get_prep_value(value)
)
return value or None
@sprin
sprin / create_test_db.py
Last active November 30, 2022 04:57
A demo of creating a new database via SQL Alchemy. This module takes the form of a nosetest with three steps: - Set up the new database. - Create a table in the new database. - Teardown the new database.
"""
A demo of creating a new database via SQL Alchemy.
Under MIT License from sprin (https://gist.github.com/sprin/5846464/)
This module takes the form of a nosetest with three steps:
- Set up the new database.
- Create a table in the new database.
- Teardown the new database.
"""
@sprin
sprin / dict_diff.py
Last active May 20, 2018 18:28
Functions for pretty-printing dictionary differences, including an assert function for testing dictionary equality (Python)
"""
Functions for pretty-printing dictionary differences, including an
assert function for testing purposes.
Under MIT License from sprin. (https://gist.github.com/sprin/6034947/)
Example:
a = {
'key1': 'val1',
@sprin
sprin / unroll_dict.py
Created November 8, 2013 02:42
Unroll a nested dictionary into flat lists.
"""
Unroll a nested dictionary into flat lists.
This might come in handy if you have on your hands a massive, hardcoded,
vertical- and horizontal-screen-spanning lookup map, and you would like to get
it into a tabular format, so you can, for example, load it into a relational
database table.
"""
import sys
import csv
@sprin
sprin / noisy_template_tag.py
Created November 12, 2013 02:36
A template tag for Django templates which does not allow non-existent variable errors to fail silently.
"""
A template tag for Django templates which does not allow
non-existent variable errors to fail silently.
Only works for simple lookups, where the variable is
a key in the context.
"""
from django import template
register = template.Library()
@sprin
sprin / gist:8643219
Created January 27, 2014 04:18
Backbone VisEventsView
var VisEventsView = Backbone.View.extend({
// A Backbone.View extension that allows a View to trigger events when
// it's visibility changes. The view may also listen to its own visibility
// events.
setup_visibility_observer: function() {
// Check if view element is visible when attributes of el change.
var t = this;
_.bindAll(t, 'check_visibility');
t.visibility_observer = new MutationObserver(t.check_visibility);
@sprin
sprin / threadlocal_abuse.py
Last active August 29, 2015 13:56
Thread Locals Abuse
from threading import local
threadlocal = local()
# foo takes a as an arg, but we can also see it is dependent on x from
# threadlocal if we look in the body.
def foo(a):
return a + threadlocal.x
# bar takes a as an arg, and calls foo on it. Unless we read through the
from flask import Flask
app = Flask(__name__)
import datetime
from threading import local
threadlocal = local()
@app.route('/')
@sprin
sprin / calling_patterns.py
Created July 10, 2014 17:36
Safe and unsafe Python function calling patterns
"""
A demonstration of safe and unsafe ways to write and call Python functions that
take required and optional arguments that are likely to change over time.
Try performing common refactoring operations on the functions such
as adding/removing required/optional args and changing args to and from
required to optional. Also consider combinations of these operations.
The calling patterns marked dangerous are likely to break in some of these
situations, whereas the safe pattern of always calling with keywords

Installation

FreeBSD

portmaster irc/irssi
portmaster irc/irssi-xmpp

OS X

brew install irssi