Skip to content

Instantly share code, notes, and snippets.

View btimby's full-sized avatar

Ben Timby btimby

View GitHub Profile
>>> one_gig = 1024*1024*1024
>>> thirty_two_gigs = one_gig * 32
>>> one_gig * 32 == thirty_two_gigs
True
>>> for i in range(thirty_two_gigs):
... _ = f.write("\n")
... if i % one_gig == 0: print(i)
...
0
1073741824
@btimby
btimby / cache_sim.py
Created August 2, 2017 14:21
Simulates various cache configurations over an nginx log.
import sys
import bisect
from UserDict import UserDict
from datetime import timedelta
from collections import deque, Counter
from peewee import *
@btimby
btimby / settings.py
Last active August 24, 2021 14:35
Use a Django database router, a TestCase mixin and thread local storage to allow unit tests to switch databases.
# Detect if executed under test
TESTING = any(test in sys.argv for test in (
'test', 'csslint', 'jenkins', 'jslint',
'jtest', 'lettuce', 'pep8', 'pyflakes',
'pylint', 'sloccount',
))
if TESTING:
# If testing, move the default DB to 'mysql' and replace it
# with a SQLite DB.
Handlebars.registerHelper('trans', function(fn) {
return gettext(fn(this));
});
@btimby
btimby / gravatar.js
Created June 25, 2012 21:49
How to insert a Gravatar in JavaScript only.
function absoluteUrl(url) {
if (url.substr(0, 4) == 'http')
return url;
return location.protocol + '//' + location.host + url;
}
/**
*
* MD5 (Message-Digest Algorithm)
* http://www.webtoolkit.info/
@btimby
btimby / django-storage-walk.py
Created March 23, 2012 21:13
os.walk() clone that uses Django storage object
import os
def walk(storage, top='/', topdown=False, onerror=None):
"""An implementation of os.walk() which uses the Django storage for
listing directories."""
try:
dirs, nondirs = storage.listdir(top)
except os.error, err:
if onerror is not None:
@btimby
btimby / gravatar.py
Created March 9, 2012 05:18
Gravatar Django template tag
import urllib, hashlib
from django import template
from django.conf import settings
from django.contrib.staticfiles.storage import staticfiles_storage
# To use this template tag:
# 1. Add it to the templatetags/ directory of your Django app.
# 2. Then load and use it in the template like so:
#
# {% load gravatar %}
@btimby
btimby / storage.py
Created February 20, 2012 19:43 — forked from jwineinger/storage.py
Django staticfiles JS-minifying storage backend
import os.path
from django.core.files.storage import FileSystemStorage
from django.core.files.base import ContentFile
from slimit import minify
class StaticFileStorageAndJSMinifier(FileSystemStorage):
"""
A storage backend to be used by the staticfiles app -- STATICFILES_STORAGE
setting. This backend operates just as a normal filesystem storage backend
except when it detects a javascript file.