Skip to content

Instantly share code, notes, and snippets.

@mihi-tr
mihi-tr / ZeroPad.clj
Created August 22, 2014 15:21
Zero Pad a number in Clojure
(defn zp "Zero Pad numbers - takes a number and the length to pad to as arguments"
[n c]
(loop [s (str n)]
(if (< (.length s) c)
(recur (str "0" s))
s)))
@mihi-tr
mihi-tr / ZeroPad.js
Last active August 29, 2015 14:05
Zero Padding in Javascript
// Zero Pad a number in Javascript
// Takes e.g. zp(42,5) -> "00042"
var zp = function(n,c) {
var s = String(n);
if (s.length< c) { return zp("0" + n,c) }
else { return s } };
# Formulas for Z scores
zscore <- function (d) {
return ((d-mean(d))/sd(d))
}
# Read in data
d <- read.csv("Cleaned-20131016-1525.csv")
# The parties we want to use
parties<-colnames(d)[c(6,8,9,10,11,12,13,14,15,16)]
@mihi-tr
mihi-tr / SensitiveDependence.nlogo
Created October 8, 2013 20:52
SensitiveDependence - a DRY version of the SensitiveDependence NetLogo script from complexityexplorer
@mihi-tr
mihi-tr / Openspending.R
Created August 14, 2013 14:58
Querying the OpenSpending API with R
require(RCurl)
require(rjson)
topsuppl<-function (dataset,drilldown,year,q=c(0.75,0.9,0.99,0.999)) {
url=paste("http://openspending.org/api/2/aggregate?dataset=",dataset,"&cut=time.year:",year,"&drilldown
=",drilldown,sep="")
j=getURL(url)
data=fromJSON(j)
amounts<-as.vector(sapply(data$drilldown,function(x) { return (x$amount[[1]]) }))
labels<-as.vector(sapply(data$drilldown,function(x) { return (x$to$label) }))
@mihi-tr
mihi-tr / scraping.json
Last active December 21, 2015 00:29
Ipython notebook for the name-pdf scraper
{
"metadata": {
"name": "Scraping a PDF with Scraperwikis PDFtoXML"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
@mihi-tr
mihi-tr / scrape-names.py
Last active December 21, 2015 00:18
Scrape the Names of Statistik Austrias Naming PDF obtained here: http://images.derstandard.at/2013/08/12/VN2p_2012.pdf
import scraperwiki
import itertools, re, csv
import lxml
# Configuration - file name and ranges for boys/girls here
filename="/home/mihi/Downloads/VN2p_2012.pdf"
# page numbers for boy and girl names
range_boys=[0,20]
@mihi-tr
mihi-tr / Kenya-Gazette
Last active December 20, 2015 15:59
Scraping the Kenya-gazette + AlchemyAPI entity recognition.
from pyquery import PyQuery
from AlchemyAPI import AlchemyAPI
from itertools import ifilter
import re
import lxml
url="http://www.kenyalaw.org/klr/index.php?id=441"
ao=AlchemyAPI()
ao.loadAPIKey("api-key.txt")
(def coins [2 1 1 0.5 0.5 0.2 0.2 0.2 0.1 0.05 0.05 0.02 0.02 0.01 0.01])
(defn square [x]
(* x x))
(defn distance
[x y]
(square (- x y)))
(defn fix-coin