Skip to content

Instantly share code, notes, and snippets.

from xml.etree import ElementTree
tree = ElementTree.fromstring(
"""<?xml version="1.0"?>
<result>
<mediaid>[MediaID]</mediaid>
<source>[SourceFile]</source>
<status>[MediaStatus]</status>
<description>[ ErrorDescription]</description> <!-- Only in case of Status = Error -->
<format>
@csytan
csytan / uca.py
Created October 14, 2009 08:54
evil uca sort
import re
import urllib
def uca_sort(l, locale='en', _re_nums=re.compile('<tt class="count">(\d+):</tt>')):
"""Uses the ICU demo site to sort a list"""
url = 'http://demo.icu-project.org/icu-bin/locexp'
url += '?_=' + locale + '&d_=en&x=col'
data = 'str=' + '\n'.join(l)
html = urllib.urlopen(url, data).read()
@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://[email protected]/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
@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 / 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 / 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 / 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 / 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 / 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 / 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);