Skip to content

Instantly share code, notes, and snippets.

@sbp
sbp / filenames.diff
Created July 6, 2011 16:30
Supercombiner filename diffs on OS X
$ diff file-before.txt file-after.txt
1a2,3
> xcd
> x8f
77a80,89
> xcd
> x87
> xcd
> x88
> xcd
@sbp
sbp / tumblr.py
Created June 18, 2011 13:20
Tumblr archive script
#!/usr/bin/env python
import sys, re, glob, urllib
r_header = re.compile('(?is)<header>(.*?)</header>')
r_section = re.compile('(?is)<section>(.*?)</section>')
r_src = re.compile(r'src="(.*?)"')
r_datetime = re.compile(r'datetime="(.*?)"')
print '<!DOCTYPE html>'
@sbp
sbp / datetz.py
Created June 8, 2011 15:37
Parse date in python ignoring timezone, python 2.4 compatible
#!/usr/bin/env python
import time, calendar, datetime
input = '2011-05-26T16:02:59.199400+00:00'
input = input.split('.', 1)[0]
input = input.split('+', 1)[0]
then = time.strptime(input, '%Y-%m-%dT%H:%M:%S')
then = datetime.datetime.utcfromtimestamp(calendar.timegm(then))
@sbp
sbp / datetz.py
Created June 8, 2011 15:19
Parse a datetime with a timezone in python, 2.4 compatible
#!/usr/bin/env python
import time, calendar, datetime
input = '2011-05-26T16:02:59.199400+00:00'
def dttz(input):
if '+' in input:
return input.split('+', 1)
else: return input, None
@sbp
sbp / convert.py
Created May 29, 2011 10:50
Convert a lightweight text format to hypertext
#!/usr/bin/env python
import sys, re
with open(sys.argv[1], 'rb') as f:
text = unicode(f.read(), 'utf-8')
r_paragraph = re.compile(ur'^|(?<=\n\n)(?=\S)')
r_link = re.compile(ur'(?s)(?<!\\)\*([^*]+)\*(\W?)\s+(\S*[./]\S+)')
r_em = re.compile(ur'(?<!\\)\*([^*]+)\*')
@sbp
sbp / feed.cgi
Created May 24, 2011 15:37
Atom feed script
#!/usr/bin/env python
'Atom Script, by Sean B. Palmer'
import os, re, cgi
r_title = re.compile('(?s)<title>(.*?), by (.*?)</title>')
r_entry = re.compile('(?s)<div([^>]+)>(.*?)</div>')
r_attrs = re.compile('(?s)([a-z-]+)="([^"]+)"')
with open('index.html') as f: bytes = f.read()
title, author = r_title.search(bytes).groups()
@sbp
sbp / json2yaml.sh
Created May 22, 2011 21:00
Convert JSON to YAML
#!/bin/sh
export PYTHONPATH=$HOME/usr/opt/pyyaml/lib/python2.6/site-packages
python -c '
import sys, json, yaml
with open(sys.argv[1]) as f:
print yaml.safe_dump(json.load(f), default_flow_style=False)
' $@
@sbp
sbp / hypertext.py
Created May 20, 2011 12:44
Convert plain text files to hypertext
#!/usr/bin/env python
import sys
name = sys.argv[1]
print '<!DOCTYPE html>'
print '<title>%s</title>' % name
print '<meta charset="utf-8">'
print '<link rel="stylesheet" href="style.css">'
@sbp
sbp / wikipedia.patch
Created May 16, 2011 23:00
Wikipedia languages patch which I wrote all myself
13,15c13,15
< wikiuri = 'http://en.wikipedia.org/wiki/%s'
< wikisearch = 'http://en.wikipedia.org/wiki/Special:Search?' \
< + 'search=%s&fulltext=Search'
---
> wikiuri = 'http://%s.wikipedia.org/wiki/%s'
> # wikisearch = 'http://%s.wikipedia.org/wiki/Special:Search?' \
> # + 'search=%s&fulltext=Search'
62c62
< def wikipedia(term, last=False):
@sbp
sbp / schemadoc.patch
Created May 10, 2011 16:57
schemadoc patch
$ diff -Naur schemadoc-2006-12-21.py schemadoc.py
--- schemadoc-2006-12-21.py 2006-12-21 17:31:40.000000000 +0000
+++ schemadoc.py 2011-05-10 18:08:38.000000000 +0100
@@ -6,7 +6,7 @@
"""
import sys
-from rdflib.Graph import ConjunctiveGraph as Graph
+from rdflib import ConjunctiveGraph as Graph
from rdflib import URIRef, Literal, BNode, Namespace