Skip to content

Instantly share code, notes, and snippets.

@Kellel
Kellel / fix migrations
Created November 19, 2014 03:28
Django migrations revert to original state
# Have you ever found your self in a place where you deleted all the tables in your
# database, but django migrations expects them to be there and throws exceptions
# like this:
#
# _mysql_exceptions.Warning: Table 'mysql.column_stats' doesn't exist
#
# And your all like "duh, I'm trying to create it!", but django migrations just
# doesn't want to cooperate!
#
# Well I've had issues like this too!!

Understanding Python Self

As a new python developer the magic 'self' identifier doesn't make a whole lot of sense. It seems like self is just magically added to the function call

Normal use of self

@Kellel
Kellel / gist:0beadaff910d72ea317e
Created September 30, 2014 01:04
Force trailing slash in bottle routes
from functools import wraps
def force_slash(fn):
@wraps(fn)
def wrapped(*args, **kwargs):
if request.environ['PATH_INFO'].endswith('/'):
return fn(*args, **kwargs)
else:
redirect(request.environ['SCRIPT_NAME'] + request.environ['PATH_INFO'] + '/')
return wrapped
@Kellel
Kellel / cache.py
Last active August 29, 2015 14:06
Nested Class Closure
def CacheFactory(name):
cache = manager.get_cache(name)
class CacheFactoryInstance(object):
class Cache(object):
def __init__(self, key):
self.key = key
def __call__(self, f):
def wrapped(*args, **kwargs):
@Kellel
Kellel / Bottle Class
Created June 30, 2014 20:12
Class based bottle routes
from bottle import route, abort, Bottle
class BasicRoute(object):
path = ''
def __init__(self, apppath, app):
self.app = app
self.methods = {
'GET': self.get,
'POST': self.post,