Skip to content

Instantly share code, notes, and snippets.

import simplejson as json
import lxml
class objectJSONEncoder(json.JSONEncoder):
"""A specialized JSON encoder that can handle simple lxml objectify types
>>> from lxml import objectify
>>> obj = objectify.fromstring("<Book><price>1.50</price><author>W. Shakespeare</author></Book>")
>>> objectJSONEncoder().encode(obj)
'{"price": 1.5, "author": "W. Shakespeare"}'
"""
anonymous
anonymous / gist:476058
Created July 14, 2010 20:57
from django.core.exceptions import ValidationError
from django.db.models import Field, CharField
__all__ = ['MultiColumnField']
try:
from hashlib import md5
except ImportError:
from md5 import new as md5
class QuerySetDoubleIteration(Exception):
"A QuerySet was iterated over twice, you probably want to list() it."
pass
# "Skinny" here means we use iterator by default, rather than
# ballooning in memory.
class SkinnyManager(Manager):
def get_query_set(self):
return SkinnyQuerySet(self.model, using=self._db)
@samuelclay
samuelclay / image_dominant_color.py
Created April 14, 2011 01:21
Algorithm to find the dominant color in an image.
from PIL import Image
import scipy
import scipy.cluster
from pprint import pprint
image = Image.open('logo_newsblur_512.png')
NUM_CLUSTERS = 15
# Convert image into array of values for each point.
ar = scipy.misc.fromimage(image)
@mrspeaker
mrspeaker / README
Created November 2, 2011 11:04
AI Class playlist generator
These are the scripts I use to create the playlists for the AI Class (available here: http://www.mrspeaker.net/2011/10/26/ai-class-playlists/). It's not pretty, but it works. I put them up here due to popular demand.
The first stage is to grab the video list HTML from YouTube and extract/sort the videos. This is done with the Video_ID_scraper.html file (instructions there).
Next, paste the video info into the YouTube_Playlist_Uploader.py script and it generates the playlist. It relies on the GData APIs so you need a dev key etc.
Any questions to mrspeaker@gmail.com
@davidnunez
davidnunez / gist:1404789
Created November 29, 2011 13:20
list all installed packages in android adb shell
pm list packages -f
@JeffreyWay
JeffreyWay / gist:1525217
Created December 27, 2011 21:29
Instant Server for Current Directory
alias server='open http://localhost:8000 && python -m SimpleHTTPServer'
@hrldcpr
hrldcpr / tree.md
Last active May 1, 2024 00:11
one-line tree in python

One-line Tree in Python

Using Python's built-in defaultdict we can easily define a tree data structure:

def tree(): return defaultdict(tree)

That's it!

@erikh
erikh / hack.sh
Created March 31, 2012 07:02 — forked from DAddYE/hack.sh
OSX For Hackers
#!/usr/bin/env sh
##
# This is script with usefull tips taken from:
# https://github.com/mathiasbynens/dotfiles/blob/master/.osx
#
# install it:
# curl -sL https://raw.github.com/gist/2108403/hack.sh | sh
#
@zeke
zeke / mozilla-npm-config.sh
Last active January 3, 2016 06:59
A .npmrc file for use when the public npm registry is down.
# npm mirroring, courtesy @jbuck and Mozilla.
# See https://github.com/jbuck/npm-readonly-mirror
curl npmrc.zeke.xxx >> .npmrc
git add .npmrc
git commit -m "mozilla .npmrc"
git push heroku master