Skip to content

Instantly share code, notes, and snippets.

View atbradley's full-sized avatar

Adam Bradley atbradley

View GitHub Profile
@atbradley
atbradley / blink_sandbox.py
Created August 18, 2016 14:12
The product of some experimentation with a Blinkstick Nano (https://www.blinkstick.com/).
@atbradley
atbradley / Ocrify.user.js
Created July 28, 2016 14:47
Add an "Add to Course Reserves" link to Josiah (Brown University Library's discovery tool)
// ==UserScript==
// @name Ocrify
// @namespace http://library.brown.edu/
// @version 0.1
// @description Try to reserve an item on Josiah.
// @author Adam Bradley <atb@brown.edu>
// @match https://search.library.brown.edu/catalog/*
// @grant none
// ==/UserScript==
@atbradley
atbradley / find_marc_field.php
Last active July 26, 2016 18:14
Parse Blacklight's JSON representation of MaRC records.
<?php
/**
* Functions for treating (blacklight-based) Josiah as an API.
*/
namespace ocra\josiah;
/**
* Find a field (and optionally subfield) in the JSONified MaRCXML record.
*
@atbradley
atbradley / pinboard_remove_unread_tweets.py
Created April 1, 2016 17:24
Remove "unread" tweets from Pinboard.
USER_TOKEN = 'pinboard:ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'
import re
import requests
import sys
import time
links = requests.get('https://api.pinboard.in/v1/posts/all?auth_token='+USER_TOKEN+'&format=json')#&count=100')
if links.status_code != 200:
@atbradley
atbradley / import_excel_workbook.R
Created January 24, 2016 04:19
Import all sheets from an Excel workbook, using readxl.
library(readxl)
sheet_list <- lapply(excel_sheets('workbook.xlsx'), read_excel, path='workbook.xlsx')
@atbradley
atbradley / smode.R
Created December 17, 2015 20:11
Function to return the statistical mode in R.
smode <- function(x) {
which(tabulate(x)==max(tabulate(x)))
}
@atbradley
atbradley / choroplethr_format_labels.R
Last active November 5, 2015 12:19
Generate a state map using choroplethr and add custom-formatted labels.
c = StateChoropleth$new(df_state_demographics)
c$title = "Population by State, Percent Black"
c$set_num_colors(7)
c$show_labels = F
df_state_labels = data.frame(long = state.center$x, lat =
state.center$y, name=tolower(state.name), label = state.abb)
df_state_labels = df_state_labels[!df_state_labels$name %in% c("alaska",
"hawaii"), ]
@atbradley
atbradley / linear-programming.R
Created September 7, 2015 20:45
Simple linear programming function in R, with an example.
lp <- function(givenName, givenQuantity, resources, needsChart) {
if ( !all.equal(names(resources), colnames(needsChart)[2:length(colnames(needsChart))]) ) {
warning("constraints and needsChart don't match.")
return(NA)
} else if ( dim(needsChart)[1] != 2 ) {
warning("Can only handle two products.")
return(NA)
}
sapply(givenQuantity, function(x) {
@atbradley
atbradley / sparklines.js
Created June 15, 2015 12:27
Generate sparklines based on 'data-data' attributes of canvases.
//NOTE: Depends on paper.js.
if ('undefined' == typeof unset) function isset(x) { return !('undefined' == typeof x) }
if ( !isset(ifunset) ) function ifunset(x, def) { return ('undefined' == typeof x) ? def : x; }
//These are defaults; config variables can be set before loading this script.
var LOGGING = ifunset(LOGGING, false);
var CANVAS_SELECTOR = ifunset(CANVAS_SELECTOR, 'canvas.sparkler');
var XMARGIN = ifunset(XMARGIN, 2);
var YMARGIN = ifunset(YMARGIN, 2);
@atbradley
atbradley / feedstopinboard.py
Last active August 29, 2015 14:21
Read one or more RSS feeds and add the entry links to Pinboard.
#! /usr/bin/python
import feedparser
import yaml
import pinboard
from time import mktime
import os
class RssToPinboard(object):
def __init__(self, settings):