Skip to content

Instantly share code, notes, and snippets.

View hrbrmstr's full-sized avatar
💤
#tired

boB Rudis hrbrmstr

💤
#tired
View GitHub Profile
@hrbrmstr
hrbrmstr / slopegraph.ph
Created May 29, 2012 15:03
slopegraph.py gist
# slopegraph.py
#
# Author: Bob Rudis (@hrbrmstr)
#
# Basic Python skeleton to do simple two value slopegraphs
# with output to PDF (most useful form for me...Cairo has tons of options)
#
# Find out more about & download Cairo here:
# http://cairographics.org/
#
@hrbrmstr
hrbrmstr / wknds.R
Created September 10, 2013 00:30
Extract weekend days from a sequence/list/etc and make a data frame of just the weekend along with which day of the week each weekend day is (Saturday|Sunday)
# need "chron" package for is.weekend()
require(chron)
# generate a sequence of dates
m.days <- seq.Date(from=as.Date("2013-08-01"), to=as.Date("2013-08-31"), by="1 day")
# if a day in the sequence is a weekend, return it
wknds <- ldply(m.days, function(m.day) {
if (is.weekend(m.day)) m.day
})
@hrbrmstr
hrbrmstr / reversedns.R
Created September 10, 2013 00:46
Given a list of IP addresses, perform a reverse lookup and return the original IP address and the resolved FQDN (if any)
# "dig" needs to be on your system and in the execution path for this to work
resolved = sapply(ip.list, function(x) system(sprintf("dig -x %s +short",x), intern=TRUE))
@hrbrmstr
hrbrmstr / notify.R
Last active May 27, 2022 15:27
Send OS X notifications from R (RStudio, R GUI or R console)
notify <- function(msg="Operation complete") {
in.osx <- (Sys.info()['sysname'] == "Darwin")
in.rstudio <- (Sys.getenv("RSTUDIO") == "1")
in.rgui <- (Sys.getenv("R_GUI_APP_REVISION") != "")
if (in.rstudio) { # hack to see if running in RStudio
title <- "RStudio"
sender <- activate <- "org.rstudio.RStudio"
}
@hrbrmstr
hrbrmstr / bls.R
Created September 15, 2013 00:10
Sample of how to extract and process US Bureau of Labor Statistics JSON API data : http://www.bls.gov/developers/
library(RCurl)
library(RJSONIO)
library(ggplot2)
# get data formats from: http://www.bls.gov/help/hlpforma.htm
# this one is "Average Hourly Earnings of All Employees: Private Service-Providing (CEU0800000003)"
bls.content <- getURLContent("http://api.bls.gov/publicAPI/v1/timeseries/data/CEU0800000003")
bls.json <- fromJSON(bls.content, simplify=TRUE)
bls.df <- data.frame(year=sapply(bls.json$Results[[1]]$series[[1]]$data,"[[","year"),
@hrbrmstr
hrbrmstr / batchip2long.c
Created September 16, 2013 21:37
Take a file with one IP address per line and convert the IP to a long int and output (to stdout) a paired list of IPSTRING,LONGIP (e.g. 1.163.160.100,27500644). I made this since it takes less than 3 seconds to process 4 million IP addresses in C vs the longer times in my other R code and longer times in general in interpreted code. Convert then…
#include <string.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
//
// Read in a file with one IP address per line
// and output (to stdout) the same list with
// the longint version of it.
//
@hrbrmstr
hrbrmstr / piratewalk-base.R
Last active December 23, 2015 10:59
Random Walk The Plancks Constant! #RRRRRRRRRRRRRRRRRRRR
set.seed(200109) # founding of The Pirate Bay
walk<-function(n) {
m <- matrix(6.62606896e-34, ncol = 2, nrow = n)
i <- cbind(seq(n), sample(c(1, 2), n, TRUE))
m[i] <- sample(c(-1, 1), n, TRUE)
m[,1] <- cumsum(m[, 1])
m[,2] <- cumsum(m[, 2])
m
}
@hrbrmstr
hrbrmstr / pirates.R
Created September 20, 2013 01:50
See: http://rud.is/b/2013/09/19/animated-irl-pirate-attacks-in-r/ for more info. Animated pirate attacks!
library(maps)
library(hexbin)
library(maptools)
library(ggplot2)
library(sp)
library(mapproj)
# piRate the data from the militaRy
download.file("http://msi.nga.mil/MSISiteContent/StaticFiles/Files/ASAM_shp.zip", destfile="ASAM_shp.zip")
unzip("ASAM_shp.zip")
@hrbrmstr
hrbrmstr / hrefs.py
Created September 21, 2013 13:20
Small python script to extract (print to stdout) HREFs from a web page. It fills in the base URL prefix for relative URLs and supports shoving as many URLs on the command line as your interpreter/python allows for command line args. Lightweight faking of user agent for more restrictive sites.
#!/usr/bin/python
import urllib2
from urlparse import urljoin
import sys
from bs4 import BeautifulSoup
if len(sys.argv) < 2:
print "Usage:\n hrefs url [url] ..."
sys.exit(1)
library(ggplot)
nosql.df <- read.csv("nosql.csv", header=TRUE)
nosql.df$Database <- factor(nosql.df$Database,
levels=c("MongoDB","Cassandra","Redis","HBase","CouchDB",
"Neo4j","Riak","MarkLogic","Couchbase","DynamoDB"))
gg <- ggplot(data=nosql.df, aes(x=Quarter, y=Index))
gg <- gg + geom_point(aes(color=Quarter), size=3)
gg <- gg + facet_grid(Database~.)