Skip to content

Instantly share code, notes, and snippets.

View zmsmith's full-sized avatar

Zach Smith zmsmith

View GitHub Profile
@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
@zmsmith
zmsmith / add_server.py
Created November 8, 2011 02:21
Django Chef Blog Post
#!/usr/bin/env python
import subprocess
import sys
from boto.ec2.connection import EC2Connection
from chef import ChefAPI, Node
base_command = "knife ec2 server create"
@zmsmith
zmsmith / output
Created August 31, 2011 19:05
Node formula broken
brew install -v node
Warning: LLVM was requested, but this formula is reported to not work with LLVM:
Error: undefined method `reason' for true:TrueClass
Please report this bug:
https://github.com/mxcl/homebrew/wiki/checklist-before-filing-a-new-issue
/usr/local/Library/Homebrew/formula.rb:322:in `handle_llvm_failure'
/usr/local/Library/Homebrew/formula.rb:252:in `brew'
/usr/local/Library/Homebrew/build.rb:53:in `install'
/usr/local/Library/Homebrew/build.rb:27
@zmsmith
zmsmith / lugers.py
Created August 17, 2011 20:58
Which Day Should We Go to Luger's for Lunch?
import random
import subprocess
days = ["Thursday", "Friday"]
def main():
day_of_week = random.choice(days)
subprocess.call("say %s" % day_of_week, shell=True)
if __name__ == "__main__":
@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 / JPEGField.py
Created January 11, 2011 03:59
ImageField that converts all images to JPEG on save.
import Image
import cStringIO
import os
from django.core.files.base import ContentFile
from django.db.models import ImageField
from django.db.models.fields.files import ImageFieldFile
class JPEGFieldFile(ImageFieldFile):