Skip to content

Instantly share code, notes, and snippets.

@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 / 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
[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 / worst-offenders.sql
Created May 21, 2014 17:29
MySQL: Show how much space tables are using in descending order
SELECT `TABLE_SCHEMA`, `TABLE_NAME`, `TABLE_ROWS`, `DATA_LENGTH`, `INDEX_LENGTH`, `DATA_FREE` FROM `TABLES` WHERE `TABLE_SCHEMA` LIKE "my_database" ORDER BY `DATA_LENGTH` DESC;
@igniteflow
igniteflow / debug.py
Created June 4, 2014 13:53
Useful debugging context managers for Django
import logging
from contextlib import contextmanager
from django.db.backends import BaseDatabaseWrapper
from django.db.backends.util import CursorWrapper
@contextmanager
def turn_off_sql_logging():
@igniteflow
igniteflow / git-post-receive-trigger-jenkins-build.md
Last active August 29, 2015 14:02
Setting up a local git server for testing and experimenting with hooks

First create the user and folder structure:

adduser git
su git
mkdir -p ~/repos/myrepo.git
git --bare init ~/repos/myrepo.git

Now our remote is ready to add to our local repository. In the local repo:

@igniteflow
igniteflow / callback.py
Created August 22, 2014 14:43
Sequential method callbacks. In Appengine this would allow a deferred task to defer another task, and this task would defer another etc. so that tasks are completed sequentially
#!/usr/bin/env python
from time import sleep
def simulate_defer(_callable, *args, **kwargs):
"""mocking Appengine's deferred.defer"""
sleep(2)
_callable(*args, **kwargs)
@igniteflow
igniteflow / gist:a1b438d5aba485cc2709
Last active August 29, 2015 14:06
Add content_as_python attribute to the APIClient.get() returned response object
import json
from rest_framework.test import APIClient
def get(self, *args, **kwargs):
response = super(APIClient, self).get(*args, **kwargs)
try:
response.content_as_python = json.loads(response.content)
except ValueError as e:
@igniteflow
igniteflow / gist:2c4b033cc70cf25b8e84
Last active August 29, 2015 14:14
Django: Set all foreign keys to None to prevent cascade deletions
from django.db.models import get_models, get_app
MY_APPS = ('foo', 'bar')
# get all the models
models = []
for app in MY_APPS:
try:
models += get_models(app_mod=get_app(app))
except ImproperlyConfigured:
@igniteflow
igniteflow / examples.sh
Last active August 29, 2015 14:15
Ack examples
# search all files ignoring those paths that contain regex_pattern (should be quoted)
ack -v -g regex_pattern | ack -x pattern
# practical Django usage to ignore migrations files
ack --python -v -g 'migrations' | ack -x foo