Skip to content

Instantly share code, notes, and snippets.

View sampsyo's full-sized avatar

Adrian Sampson sampsyo

View GitHub Profile
@sampsyo
sampsyo / mk2mpris.py
Created July 9, 2012 20:41
forward GNOME media key events to an MPRIS2 player
"""mk2mpris.py: forward GNOME media key events to an MPRIS2 player.
"""
import dbus
from dbus.mainloop.glib import DBusGMainLoop
import gobject
APP_NAME = 'mk2mpris'
DBUS_INTERFACE_MK = 'org.gnome.SettingsDaemon.MediaKeys'
OBJ_PATH_MK = '/org/gnome/SettingsDaemon/MediaKeys'
OBJ_NAME_GSETTINGS = 'org.gnome.SettingsDaemon'
@sampsyo
sampsyo / genres.txt
Created September 25, 2011 23:19
music genre list scraper
2 tone
2-step garage
4-beat
4x4 garage
8-bit
acapella
acid
acid breaks
acid house
acid jazz
@sampsyo
sampsyo / lyrics.py
Created August 10, 2011 23:57
Simple dependency-free multi-source lyrics scraper
"""Simple scraper for LyricsWiki and Lyrics.com. The code is hacky and
ugly because I wanted to implement this without a full-blown HTML parser
like BeautifulSoup or lxml.
"""
import urllib
import sys
import re
COMMENT_RE = re.compile(r'<!--.*-->', re.S)
DIV_RE = re.compile(r'<(/?)div>?')
@sampsyo
sampsyo / gist:1137717
Created August 10, 2011 18:30
making MusicBrainz getters using templates
def _make_id_getter(entity):
def getter(id, **kwargs):
includes = []
for name in kwargs:
if not name.startswith('inc_'):
raise TypeError('got unexpected keyword argument...')
elif kwargs[name]:
includes.append(name[4:])
return _do_mb_query(entity, id, includes)
getter.__name__ = 'get_%s_by_id' % entity
@sampsyo
sampsyo / multijson.py
Created April 14, 2011 18:59
quick & dirty reading/writing of files containing multiple JSON objects
"""A couple of very simple utilities for reading and writing files
that contain multiple JSON values. Could be useful in situations where
you're generating a bunch of data for later processing and then, later,
you want to read it in an element at a time.
The json module doesn't really support streaming reads, though, so this
is limited by that. If you need real streaming, you probably want to use
something like ijson:
http://pypi.python.org/pypi/ijson/
"""
@sampsyo
sampsyo / fingerprinter-macports.diff
Created February 1, 2011 00:49
Fixes compilation of Last.fm fingerprinter under Mac OS X with libraries installed by MacPorts (correctly specifying the libraries that need to be linked).
diff -r 6a862c34b61f fplib/CMakeLists.txt
--- a/fplib/CMakeLists.txt Mon Jul 12 18:14:59 2010 +0100
+++ b/fplib/CMakeLists.txt Mon Jan 31 16:49:31 2011 -0800
@@ -19,6 +19,7 @@
IF(APPLE)
INCLUDE_DIRECTORIES(/opt/local/include/)
+LINK_DIRECTORIES(${LINK_DIRECTORIES} /opt/local/lib/)
ENDIF(APPLE)
@sampsyo
sampsyo / pipeline.py
Created August 1, 2010 02:25
multithreaded pipelines for Python coroutines
"""Simple but robust implementation of generator/coroutine-based
pipelines in Python. The pipelines may be run either sequentially
(single-threaded) or in parallel (one thread per pipeline stage).
This implementation supports pipeline bubbles (indications that the
processing for a certain item should abort). To use them, yield the
BUBBLE constant from any stage coroutine except the last.
In the parallel case, the implementation transparently handles thread
shutdown when the processing is complete and when a stage raises an
@sampsyo
sampsyo / runm.py
Created July 28, 2010 16:42
plot time data from runmeter with pychart
"""Plot time data from a Runmeter <http://www.abvio.com/runmeter/>
data export."""
import csv
import sys
from collections import defaultdict
import datetime
def runmeter_read(path):
doc = csv.reader(open(path))
@sampsyo
sampsyo / empty.py
Created July 23, 2010 02:34
quick EC2 sandbox creator
#!/usr/bin/env python
"""Quickly get a temporary Ubuntu EC2 instance, SSH into it, and then
terminate the instance on logout.
"""
import boto
import subprocess
import time
# Set your AWS credentials.
EC2_ACCESS = ''
@sampsyo
sampsyo / aliases.py
Created July 11, 2010 20:04
hack for argparse adding subcommand aliases
#!/usr/bin/env python
"""Aliases for argparse positional arguments."""
import argparse
class AliasedSubParsersAction(argparse._SubParsersAction):
class _AliasedPseudoAction(argparse.Action):
def __init__(self, name, aliases, help):