Skip to content

Instantly share code, notes, and snippets.

@sbp
sbp / search.cgi
Created January 27, 2011 13:40
Service to search the Swhack logs
#!/usr/bin/python
import os, re, urllib, time
querystring = os.environ.get('QUERY_STRING', '')
pattern = urllib.unquote(querystring)
r_pattern = re.compile('(?i)' + pattern)
os.chdir('logs')
matches = []
@sbp
sbp / LinkParser.py
Created February 10, 2011 14:13
LinkParser class in Python
class LinkParser(HTMLParser.HTMLParser):
def __init__(self, *args, **kargs):
HTMLParser.HTMLParser.__init__(self, *args, **kargs)
self.links = {}
self.base = None
def handle_starttag(self, tag, attrs):
attrdict = dict(attrs)
if tag != 'base':
self.handle_link(attrdict)
@sbp
sbp / extreme5.txt
Created February 27, 2011 15:14
Parsing extreme HTML as HTML5
>>> import html5lib
>>> f = open('extreme.html')
>>> doc = html5lib.parse(f)
>>> for element in doc:
... print element
...
<html>
<head>
<body>
<meta-start>
@sbp
sbp / vowels.py
Created March 9, 2011 21:59
vowel munger
#!/usr/bin/env python
# coding=utf-8
import sys, re, random
r_vowel = re.compile(r'[aeiou]')
def rand(m):
return random.choice(u'áéíóúâêîôûäëïöüáéíóúâêîôûäëïöüaeiou')
@sbp
sbp / LICENSE.txt
Created March 16, 2011 11:20
Proposed Sansmotif license.
Copyright (c) 2005, Cody Woodard (d8uv.org),
with Reserved Font Name Sansmotif.
Copyright (c) 2011, Mark Shoulson (meson.org).
This Font Software is licensed under the SIL Open Font License, Version 1.1.
This license is copied below, and is also available with a FAQ at: http://scripts.sil.org/OFL
SIL OPEN FONT LICENSE
@sbp
sbp / parse.py
Created March 19, 2011 15:25
Convert Mail.app messages into HTML
#!/usr/bin/env python
import re, glob, email.parser
def message_body(message):
maintype = message.get_content_maintype()
if maintype == 'text':
return message.get_payload(decode=True)
@sbp
sbp / colour.py
Created March 22, 2011 16:08
Colourise a CQ using a mixture of logarithmic and linear scales
def colour(cq):
rating = abs(cq - 100)
base = math.pow(10, 1./111) # i.e. 0.5 * 1/(255 - 32 - 1)
LOG = math.log(rating + 1, base) / 2
LIN = rating * 2.23 / 2 # i.e. 255 - 32 / 100 (/ 2)
red, green, blue = 32, 32, 32
if cq > 100:
blue += int(LOG + LIN)
elif cq < 100:
@sbp
sbp / oblique.html
Created March 25, 2011 15:45
Phenny oblique services
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
@sbp
sbp / docrun.py
Created March 28, 2011 16:20
Run Python documentation as script content
from itertools import groupby
from operator import itemgetter
def first(length): return itemgetter(slice(None, length))
def remove_indent(group, n): return (line[n:] for line in group)
def combine(lines, sep): return sep.join(lines) + sep
def indented_sections(text):
"""
>>> example = 'One:\n abc\n def\nTwo:\n ghi'
@sbp
sbp / flatten.sh
Created April 25, 2011 16:11
Shell script to flatten a hierarchy
#!/bin/bash
find . -type f -exec cp -pf {} ~/web \;