Skip to content

Instantly share code, notes, and snippets.

@dbr
dbr / timeout.py
Created December 5, 2008 16:44
signal based timeout for function call
#!/usr/bin/env python
class TimeExceededError(Exception):pass
def run_cmd(cmd, timeout = 0):
import signal
def alarmHandler(signum, frame):
raise TimeExceededError, "Took too long to execute"
signal.signal(signal.SIGALRM, alarmHandler)
signal.alarm(1)
@dbr
dbr / jnot.py
Created April 23, 2009 04:03
Simple message queue based Jabber bot
#!/usr/bin/env python
import yaml
from jabberbot import JabberBot
from beanstalkc import Connection as BSC
def connect_beanstalk(host = "127.0.0.1", port = "11300", tube = None):
try:
c = BSC()
except:
return None
@dbr
dbr / musicalTorrents.py
Created October 5, 2009 23:29
Encode file as MIDI
#!/usr/bin/env python2.6
# Converts an arbitrary file to MIDI. Obviously.
# Note the output files can be.. long (a 25KiB file becomes about 10 hours),
# and may cause players to lockup while opening (the aforementioned 10 hour
# file took Quicktime Player 7 about 5-10 seconds to open)
import smidi
def fileToMidi(input_fname, output_fname):
@dbr
dbr / makeValidFilename.py
Created December 14, 2009 18:15
Function to ensure a filename is valid
import os
import re
import platform
def makeValidFilename(value, normalize_unicode = False, windows_safe = False, custom_blacklist = None):
"""
Takes a string and makes it into a valid filename.
normalize_unicode replaces accented characters with ASCII equivalent, and
removes characters that cannot be converted sensibly to ASCII.
@dbr
dbr / twittersearcher.py
Created December 31, 2009 03:05
Twitter search history
#!/usr/bin/env python2.6
"""Twitter's "advanced" search can't seem to find a users tweet from more
than a few days ago, so I wrote this simple function to do so.
Not good code, not very efficient, only finds the most recent occurrence of
the word.. but it worked for what I needed
"""
import urllib
import simplejson as json
@dbr
dbr / nameByDate.py
Created January 2, 2010 21:07
Name TV episodes by first aired date
#!/usr/bin/env python2.6
"""Simple script to names TV episode by first aired date.
Files must be organised as follows:
./ShowName/20091130-anything.ext
For example:
./Scrubs/20050118.avi
@dbr
dbr / howlong.py
Created January 5, 2010 20:46
Approximate time to brute-force a password
"""Calculates how long a password might take to brute force
"""
def chunks(l, n):
for i in xrange(0, len(l), n):
yield l[i:i+n]
def human_readable_number(n):
return ",".join(list(chunks(str(n)[::-1],3)))[::-1]
@dbr
dbr / nameByName.py
Created January 9, 2010 21:21
Name TV episodes by name
#!/usr/bin/env python2.6
"""Simple script to names TV episode by episode name.
Files must be named as follows:
./ShowName - Episode Name.ext
For example:
./Scrubs - My First Day.avi
@dbr
dbr / fxphd_processor.py
Created January 16, 2010 17:47
fxphd.com class download processor
#!/usr/bin/env python2.6
"""Processes fxphd.com class ZIP files, by copying the archive to a specified
location, then extracting this into a specified path.
Directory structure is:
/example/fxphd/10jan_term/
archives/
preview_classes/
@dbr
dbr / gist:279677
Created January 18, 2010 00:12
Nuke show-node-with-postage-stamps nonsense
"""Nuke/Python script that displays the selected node in the DAG
using a lot of node postage-stamps.. Not *exactly* useful..
http://img46.imageshack.us/img46/923/nukenodalimgviewer.png
"""
import nuke
def getRGB(node, x, y):
return [node.sample(chan, x, y) for chan in ['red', 'green', 'blue']]