Skip to content

Instantly share code, notes, and snippets.

View yamatt's full-sized avatar

Matt Copperwaite yamatt

View GitHub Profile
@yamatt
yamatt / gist:845735
Created February 26, 2011 23:24
Run VLC as X user
import os
sudo = "sudo -u xuser %s"
instruction = "vlc 20100420-0343a.mp3"
os.system(sudo % instruction)
@yamatt
yamatt / gist:1295517
Created October 18, 2011 14:13
select random key that is weighted by it's value
from random import choice
d = {
'foo1': 1,
'foo2': 5,
'foo3': 3
}
choice([k for k in d for x in range(d[k])])
@yamatt
yamatt / bijective.py
Created October 24, 2011 10:21 — forked from zumbojo/bijective.rb
Simple bijective function (base(n) encode/decode) in Python
# Simple bijective function
# Basically encodes any integer into a base(n) string,
# where n is ALPHABET.length.
# Based on pseudocode from http://stackoverflow.com/questions/742013/how-to-code-a-url-shortener/742047#742047
ALPHABET = list("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
# make your own alphabet using:
# (('a'..'z').to_a + ('A'..'Z').to_a + (0..9).to_a).shuffle.join
def bijective_encode(i):
@yamatt
yamatt / google-oauth.py
Created April 11, 2012 09:56
Teaching myself the Google OAuth process
from urllib import urlencode
from urllib2 import Request, urlopen, HTTPError
from json import loads as jsonsload
dictionary_of_useful_things = {
"client_id": "261777520064-etb9m51k800e6ucrcf767th54uc5nt7i.apps.googleusercontent.com",
"client_secret": "{{ it's a secret. SSHHHhhh! }}",
"redirect_uri": "urn:ietf:wg:oauth:2.0:oob",
"grant_type": "authorization_code"
}
@yamatt
yamatt / qrbookmarklet.js
Created April 24, 2012 11:04
Turn this page in to a QR code bookmarklet
javascript:(function(){var qr_url="http://chart.apis.google.com/chart?cht=qr&chs=300x300&chld=H|0&chl=";window.open(qr_url+encodeURIComponent(location.href));})();
@yamatt
yamatt / client.py
Created May 24, 2012 19:38
Definitive guide for creating ssl based server and client in Python 2.6 and higher
import socket
import sys
import ssl
class Client(object):
recv_length=1024
def __init__(self, server="localhost", port=5054):
self.server = server
self.port = port
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#!/usr/bin/env python
import pyglet
mp3 = "test.mp3"
source = pyglet.media.load(mp3)
player = pyglet.media.Player()
player.queue(source)
player.play()
@yamatt
yamatt / messaging.html
Created March 12, 2013 21:15
A starting point for a HTML5 messaging page using WebRTC
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>HTML5 Message Tests</title>
</head>
<body>
<h1>Set a name and type a message</h1>
<form id="name">
<label>Enter your name:
@yamatt
yamatt / SimpleWebSocketServer.py
Created March 19, 2013 13:59
Reference WebSocket server in Python. Works with WebSocket revision 13.
#!/usr/bin/env python
import time
import struct
import socket
import hashlib
import base64
import sys
from select import select
import re
@yamatt
yamatt / ihatebash.sh
Created April 8, 2013 14:39
Sometimes (quite often) I hate bash. This is mostly an issue if you want spaces in your directory path.
mkdir gubbins
touch gubbin{0..9}.file
ls gubbins/ # responds as you would expect
ls gubbins/* # responds as you would expect
ls gubbins/*.file # responds as you would expect
ls "gubbins/*.file" # no such file or directory
ls 'gubbins/*.file' # no such file or directory
ls "gubbins/"*".file" # works! damnit.