Skip to content

Instantly share code, notes, and snippets.

@jmcarp
jmcarp / box_dot_plot.py
Created March 9, 2013 17:18
Basic boxplot with dots.
# Import matplotlib
from matplotlib import pyplot as plt
# Define data
data_grp1 = [1, 2, 3, 4, 5, 3, 2, 1, 5, 6, 4, 2, 4]
data_grp2 = [6, 7, 8, 7, 8, 9, 1, 1, 9, 15]
# Boxplot
plt.boxplot([data_grp1, data_grp2])
# data = ...
# subjects = ...
# trials = ...
# computation = function(...)
for (subject in subjects) {
subject_rows = data$subject == subject
for (trial in trials) {
rows = subject_rows & data$trial == trial
result = computation(data[rows,])
@jmcarp
jmcarp / gist:5259520
Last active December 15, 2015 12:19
states_to_colors
def states_to_colors(states, cols):
'''Generate all possible mappings of states to colors.
Args:
states (list) : list of state names
cols (list) : list of color names
Returns:
list of dictionaries of state-color mappings
Example use:
def enum(sofar, new):
if len(new) == 0:
return sofar
else:
return enum (sofar + new[0] + 'r', new[1:]) \
+ enum (sofar + new[0] + 'g', new[1:]) \
+ enum (sofar + new[0] + 'b', new[1:]) \
@jmcarp
jmcarp / pdfxtract.py
Last active March 30, 2023 03:07
Extract text from PDF document using PDFMiner
"""
Extract PDF text using PDFMiner. Adapted from
http://stackoverflow.com/questions/5725278/python-help-using-pdfminer-as-a-library
"""
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter#process_pdf
from pdfminer.pdfpage import PDFPage
from pdfminer.converter import TextConverter
from pdfminer.layout import LAParams
@jmcarp
jmcarp / referrer.py
Created January 21, 2014 20:36
Copying URL parameters from referrer
@app.before_request
def prepare_private_key():
# Done if private_key in args
key_from_args = request.args.get('private_key')
if key_from_args:
return
# Check referrer for private key
parsed_referrer = urllib.urlparse(request.referrer)
@jmcarp
jmcarp / forceDownload.js
Created March 1, 2014 15:35
Forcing a file download in JavaScript
function forceDownload(href) {
var anchor = document.createElement('a');
anchor.href = href;
anchor.download = href;
document.body.appendChild(anchor);
anchor.click();
}
class _Bar(object):
pass
class Foo(object):
def __init__(self):
self._bar = None
@property
@jmcarp
jmcarp / gist:52ccd071faa1b910e2e9
Created November 22, 2014 20:20
Tornado / Celery / sockets proof of concept
# -*- coding: utf-8 -*-
"""Tornado / Celery / sockets proof of concept
To run:
$ celery -A app worker
$ python app.py
"""
import os
import socket
import random
@jmcarp
jmcarp / aio_tornado.py
Last active August 29, 2015 14:10
water-butlers
import asyncio
import aiohttp
import tornado.web
import tornado.options
import tornado.concurrent
import tornado.platform.asyncio
URL = '...'
UPLOAD_URL = '...'