Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View fnielsen's full-sized avatar

Finn Årup Nielsen fnielsen

View GitHub Profile
#!/usr/bin/env python
#
# $Id: Nielsen2010Python_sqlspeed.py,v 1.3 2010/11/15 00:59:14 fn Exp $
import os
import random
import sqlite3
import time
conversion = { 1: "one", 2: "two", 3: "three", 4: "four", 5: "five",
@fnielsen
fnielsen / Royal2008Whats.py
Created April 5, 2011 15:42
Analysis of Wikipedia coverage
#!/usr/bin/env python
#
# $Id: Royal2008Whats.py,v 1.2 2011/04/05 15:40:37 fn Exp $
from pylab import *
from scipy.signal import medfilt
from scipy.stats.stats import spearmanr
from urllib import FancyURLopener, urlopen, urlencode
import sys
reload(sys)
@fnielsen
fnielsen / Nielsen2011Median.py
Created May 4, 2011 12:47
Median filter bias
# $Id: Nielsen2011Median.py,v 1.1 2011/05/04 12:21:30 fn Exp $
from pylab import *
from scipy.signal import medfilt
from numpy import median
def g():
"""Generator for data points"""
if rand() < 0.90: return 12 + randn()
else: return 12 + 10 * randn()
@fnielsen
fnielsen / Wehner2011Trusselrapporter.py
Created May 12, 2011 17:08
Afghanistan threat reports
#!/usr/bin/env python
#
# $Id: Wehner2011Trusselrapporter.py,v 1.3 2011/05/12 17:07:21 fn Exp $
import csv
from pylab import *
from urllib import urlopen
url = 'http://data.information.dk/warlogs/afghanistan/data/wl_threat_reports.csv'
#!/usr/bin/env python3
import sys
texts = [ open(filename).read().split() for filename in sys.argv[1:]]
maxlength = [ max(map(len, t)) for t in texts ]
for line in zip(*texts):
print("".join([ line[n] + " " * (maxlength[n] - len(line[n]) + 1) for n in range(len(line)) ]))
#!/usr/bin/env python3
import sys
texts = [ open(filename).read().split() for filename in sys.argv[1:]]
maxlength = [ max(map(len, t)) for t in texts ]
print("\n".join(["".join([ line[n] + " " * (maxlength[n] - len(line[n]) + 1) for n in range(len(line)) ]) for line in zip(*texts) ]))
#!/usr/bin/env python3
import itertools, sys
texts = [ open(filename).read().split() for filename in sys.argv[1:]]
maxlength = [ max(map(len, t)) for t in texts ]
print("\n".join(["".join([ line[n] + " " * (maxlength[n] - len(line[n]) + 1) for n in range(len(line)) ]) for line in itertools.zip_longest(*texts, fillvalue="") ]))
@fnielsen
fnielsen / gist:1080175
Created July 13, 2011 11:56
pløf.python3
import itertools,sys
t=[open(f).read().split()for f in sys.argv[1:]]
m=[max(map(len,u)) for u in t]
print("\n".join(["".join([l[n]+" "*(m[n]-len(l[n])+1)for n in range(len(l))])for l in itertools.zip_longest(*t,fillvalue="")]))
import itertools,sys
t=[open(f).read().strip().split("\n")for f in sys.argv[1:]]
for l in itertools.zip_longest(*t,fillvalue=""):print(" ".join([l[n].ljust(max(map(len,t[n])))for n in range(len(l))]))
@fnielsen
fnielsen / gist:1194493
Created September 5, 2011 08:59
Small Python demonstration program to add numbers from input argument
#!/usr/bin/env python
import math, sys # Importing modules.
def formatresult(res): # Define function. Remember colon!
"""This is the documentation for a function."""
return "The result is %f" % res # Percentage for formating
if len(sys.argv) < 3: # Conditionals should be indended
print("Too few input argument")