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 / 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 / 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 / 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 / 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 / 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