Skip to content

Instantly share code, notes, and snippets.

View zmsmith's full-sized avatar

Zach Smith zmsmith

View GitHub Profile
@zmsmith
zmsmith / jpegfield.py
Created June 20, 2011 21:54
Implementation of a Django ImageField that converts all images to JPEG
import Image
import cStringIO
from django.core.files.base import ContentFile
from django.db.models import ImageField
from django.db.models.fields.files import ImageFieldFile
class JPEGImageFieldFile(ImageFieldFile):
@zmsmith
zmsmith / s3logs.py
Last active December 13, 2015 18:09
A inefficient but effective way to scan logs on s3
from __future__ import print_function
import StringIO
import gzip
from boto.s3.connection import S3Connection
conn = S3Connection("<key_id>", "<secret>")
bucket = conn.get_bucket('<bucket>')
@zmsmith
zmsmith / gist:4643655
Created January 26, 2013 18:25
Add spellchecking in Sublime Text 2 to Markdown files (assuming you don't already have Markdown specific settings).
echo '{
"spell_check": true,
"dictionary": "Packages/Language - English/en_US.dic"
}' > ~/Library/Application\ Support/Sublime\ Text\ 2/Packages/User/Markdown.sublime-settings
@zmsmith
zmsmith / thumb.py
Last active December 10, 2015 14:08
from django.conf import settings
from libthumbor import CryptoURL
def thumb(url, **kwargs):
'''
returns a thumbor url for 'url' with **kwargs as thumbor options.
Positional arguments:
url -- the location of the original image
@zmsmith
zmsmith / gist:3797402
Created September 28, 2012 01:06
Find URLs with underscores
import re
def find_underscores(patterns):
for pattern in patterns:
raw = pattern.regex.pattern
rr = re.sub('\(.*?\)', lambda x: '', raw)
if '_' in rr:
print pattern.regex.pattern
if hasattr(pattern, 'url_patterns'):
find_underscores(pattern.url_patterns)
@zmsmith
zmsmith / gist:3732960
Created September 16, 2012 16:00
Find the year an album was published
import requests
import pyquery
import re
import sys
import urllib
SEARCH_URL = 'http://ajax.googleapis.com/ajax/services/search/web'
def unquote_all(url):
@zmsmith
zmsmith / queues.py
Created July 11, 2012 16:05
Check Celery Queues
from django.conf import settings
used_queues = set()
for route, q_dict in settings.CELERY_ROUTES.items():
module, task = route.rsplit('.', 1)
mod = __import__(module, globals(), locals(), [task])
try:
t = getattr(mod, task)
except AttributeError:
@zmsmith
zmsmith / kill_queries.py
Created July 3, 2012 01:45
Django - Kill Queries Matching a RegEx
def kill_queries(query='', db_alias='default'):
from django.db import connections
import re
connection = connections[db_alias]
cursor = connection.cursor()
cursor.execute("SHOW PROCESSLIST")
for row in cursor.fetchall():
current = row[-1]
if current and re.search(query, current):
@zmsmith
zmsmith / open_module.sh
Created April 28, 2012 16:49
Quick bash helper for reading python source code
function open_module() {
mate `python -c "import $1 as mod; print (mod.__file__[:-12] if '__init__' in mod.__file__ else mod.__file__[:-1])"`
}
@zmsmith
zmsmith / get_exception_side_effect.py
Created April 4, 2012 23:37
Method for creating a Mock side_effect that only raises an exception for n calls
def get_exception_side_effect(num_of_exceptions, exception_class=Exception):
def side_effect(*args, **kwargs):
side_effect.calls += 1
if side_effect.calls > num_of_exceptions:
return Mock()
else:
raise exception_class
side_effect.calls = 0
return side_effect