Skip to content

Instantly share code, notes, and snippets.

View yamatt's full-sized avatar

Matt Copperwaite yamatt

View GitHub Profile
@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 / 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 / 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)