Skip to content

Instantly share code, notes, and snippets.

View atbradley's full-sized avatar

Adam Bradley atbradley

View GitHub Profile
@atbradley
atbradley / blog-to-solr.py
Created April 2, 2015 12:40
"Read" a Wordpress blog and update documents indexed in Solr with links to relevant posts, based on tags. Written to index the catalog for the Hall-Hoag collection (http://library.brown.edu/collatoz/info.php?id=62)
import json
from lxml import etree
import requests
from datetime import date
def findId(xml):
for cat in xml.findall('category'):
if cat.text.startswith('HH_'):
return cat.text
@atbradley
atbradley / mapvars.xsl
Last active August 29, 2015 14:20
Alter two SVG US maps for use in the Hall-Hoag catalog project.
<?xml version="1.0" encoding="windows-1252"?>
<xsl:stylesheet xmlns="http://www.w3.org/2000/svg"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl"
version="1.0">
<xsl:output method="xml" indent="yes" encoding="UTF-8" omit-xml-declaration="yes" />
<xsl:variable name="smallcase" select="'abcdefghijklmnopqrstuvwxyzàèìòùáéíóúýâêîôûãñõäëïöüÿåæœçðø'" />
<xsl:variable name="uppercase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZÀÈÌÒÙÁÉÍÓÚÝÂÊÎÔÛÃÑÕÄËÏÖÜŸÅÆŒÇÐØ'" />
@atbradley
atbradley / svg_link.js
Created May 15, 2015 17:28
Create a link (as a jQuery object) to an SVG on the current page.
@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):
@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 / 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 / gist:946435
Created April 28, 2011 14:18
Basic scaffold for a Python decorator
def SomeDecorator(next):
def getFunction(fn):
def doStuff(req, *args):
return fn(req, *args)
return doStuff
return getFunction
@atbradley
atbradley / gist:946438
Created April 28, 2011 14:19
Python equivalent of php's md5()
def md5(str):
import md5
hasher = md5.new()
hasher.update(str)
return hasher.hexdigest()
@atbradley
atbradley / gist:946439
Created April 28, 2011 14:20
Generate a random-enough string. Maybe useful for passwords or for the encryption seed required in some frameworks' configuration file.
function randstr($len = 8) {
$src = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
$src = $src.$src.$src.$src.$src;
return substr(str_shuffle($src), 0, $len);
}
@atbradley
atbradley / gist:1006998
Created June 3, 2011 19:30
CodeIgniter configuration to allow both GET variables and segment-based addressing
$config['uri_protocol'] = &quot;PATH_INFO&quot;;
$config['enable_query_strings'] = TRUE;
$config['permitted_uri_chars'] = 'a-z 0-9~%.:_-?=+&;';