Skip to content

Instantly share code, notes, and snippets.

# Pickle with patch by Aaron Gallagher (habnabit) applied:
# http://bugs.python.org/issue3119
"""Create portable serialized representations of Python objects.
See module cPickle for a (much) faster implementation.
See module copy_reg for a mechanism for registering custom picklers.
See module pickletools source for extensive comments.
Classes:
@csytan
csytan / dbextras.py
Created May 9, 2009 09:12
appengine db extras
from google.appengine.ext import db
from django.utils import simplejson
### Functions ###
def fetch_references(entities, attr):
"""Fetches and sets ReferenceProperty attributes in one db call"""
keys = []
for entity in entities:
key = getattr(entity, '_' + attr)
@csytan
csytan / Tag coloring javascript
Created May 11, 2009 06:35
random tag colors
function hsv_to_rgb(h, s, v) {
// http://nofunc.org/Color_Conversion_Library/
var R, G, A, B, C, S=s/255, V=v/255, H=h/255;
if(S>0) {
if(H>=1) H=0;
H=6*H; F=H-Math.floor(H);
A=Math.round(255*V*(1-S));
B=Math.round(255*V*(1-(S*F)));
C=Math.round(255*V*(1-(S*(1-F))));
V=Math.round(255*V);
@csytan
csytan / crawler.py
Created May 11, 2009 06:37
crawler.py
import urllib2
from urlparse import urljoin
import time
import BeautifulSoup
class Crawler:
def __init__(self, parser):
self.parser = parser
self.queue = set()
@csytan
csytan / imdb.py
Created May 11, 2009 06:39
imdb.py
from lib import BeautifulSoup
import urllib2
from urlparse import urljoin
import difflib
import re
def get_title_name(self):
imdb_title = self.title_id
url = urllib2.urlopen("http://imdb.com/title/" + imdb_title)
string = url.read()
@csytan
csytan / webimport.py
Created May 11, 2009 06:40
webimport.py
import sys
import imp
import urllib2
PATHS = [
"http://www.crummy.com/software/BeautifulSoup/download/",
]
for path in PATHS:
sys.path.append(path)
@csytan
csytan / wikipedia.py
Created May 11, 2009 06:41
wikipedia helper module
import urllib
from django.utils import simplejson
def suggest(prefix, lang='en'):
url = 'http://' + lang + '.wikipedia.org/w/api.php?action=opensearch'
url += '&search=' + urllib.quote(prefix) + '&format=json'
json = urllib.urlopen(url).read()
data = simplejson.loads(json)
results = data[1]
@csytan
csytan / freebase.py
Created May 11, 2009 06:43
freebase.py
# Python imports
import urllib
import urllib2
# Local imports
try:
import simplejson
except ImportError:
from django.utils import simplejson
@csytan
csytan / templatetags.py
Created June 25, 2009 18:28
markdown2.py django custom filter
urlfinder = re.compile('^(http:\/\/\S+)')
urlfinder2 = re.compile('\s(http:\/\/\S+)')
@register.filter('markdownify')
def markdownify(value):
import feedparser
import markdown2
value = urlfinder.sub(r'<\1>', value)
value = urlfinder2.sub(r' <\1>', value)
html = markdown2.markdown(value)
html = feedparser._sanitizeHTML(html, 'utf-8')
@csytan
csytan / hexagon.cc dev server stuff
Created October 7, 2009 22:48
hexagon.cc dev cheat sheet
# clone hexagon repository
git clone git+ssh://git@git.isohunt.com/int/zeta-code
# install postgres
http://www.robbyonrails.com/articles/2008/01/22/installing-ruby-on-rails-and-postgresql-on-os-x-third-edition
# uninstall prev postgres (optional)
sudo port uninstall postgresql84
sudo port clean postgresql84
sudo port uninstall postgresql84-server