Skip to content

Instantly share code, notes, and snippets.

View madssj's full-sized avatar

Mads Sülau Valstorp Jørgensen madssj

  • San Jose, United States
View GitHub Profile
@madssj
madssj / gist:1319151
Created October 27, 2011 09:25
Get a random name from /usr/share/dict/propernames
def get_random_name():
"""
Return a random proper name in a somewhat efficient way.
"""
fp = open("/usr/share/dict/propernames")
fp.seek(0, 2)
size = fp.tell()
fp.seek(0)
fp.seek(int(random.random() * size * 0.95))
@madssj
madssj / south_07to06.py
Last active December 18, 2015 01:29
South migration convert from 2 to 1.
import sys
import re
create_table_re = re.compile("db.create_table('([^']+)',")
field_name_re = re.compile("^(s*)('([^']+)', self.gf")
cur_model = None
input = open(sys.argv[1])
@madssj
madssj / echoserver.py
Created June 4, 2013 08:38
A simple python echo server.
import asyncore
import asynchat
import socket
class Lobby(object):
def __init__(self):
self.clients = set()
def leave(self, client):
self.clients.remove(client)
#include <math.h>
#include <stdlib.h>
#define NV_MAGICCONST 1.7155277699214135 /* (4 * exp(-0.5) / sqrt(2.0)); */
#define MAX_RANDOM 2147483647 /* (2 ** 31) - 1; */
/*
* normalvariate ported from python's stdlib (random.normalvariate).
* released under the same licence as that (python license).
*
def http_head(url):
import httplib
import urlparse
redirects = 0
while redirects < 10:
scheme, netloc, path, query, fragment = urlparse.urlsplit(url)
if scheme == 'https':
def send_plain_mail(subject, body, from_mail, to):
import smtplib
from email.MIMEText import MIMEText
from email.Encoders import encode_quopri
msg = MIMEText(body, 'plain', 'iso-8859-1')
msg['Subject'] = subject
msg['From'] = from_mail
msg['To'] = to
import os
import subprocess
import threading
from Queue import Queue
ITUNES_DIR = os.path.expanduser("~/Music/iTunes/iTunes Music/")
KNOWN_EXTENSIONS = ('.mp3', '.aac')
AACGAIN_PARAMS = ['-a', '-k', '-d', '3']
AACGAIN_LOCATION = 'aacgain'
var a:XML = <request command="run-command" />;
var r:URLRequest = new URLRequest("http://localhost/service.php");
r.data = a;
r.method = URLRequestMethod.POST;
r.contentType = "text/xml";
var l:URLLoader = new URLLoader();
l.addEventListener(Event.COMPLETE, function(e:Event) {
trace("success", l.data);
#!/bin/sh
if [ $# -ne 2 ]
then
echo "Usage: $0 <postgresql sql dump> <db_name>" >&2
exit 1
fi
db_file=$1
db_name=$2
import os, sys, re
from os.path import join, getsize, exists
def median(numbers):
s = sorted(numbers)
l = len(numbers)
if l % 2 == 0:
a, b = s[l / 2 - 1 : l / 2 + 1]
if a != b:
return a + b / 2.0