Skip to content

Instantly share code, notes, and snippets.

@mittenchops
mittenchops / init.el
Created March 26, 2014 15:24
init.el for stack overflow python pymacs question
(when (>= emacs-major-version 24)
(require 'package)
(package-initialize)
(setq package-archives '(("gnu" . "http://elpa.gnu.org/packages/")
("marmalade" . "http://marmalade-repo.org/packages/")
("melpa" . "http://melpa.milkbox.net/packages/")
)
)
)
@mittenchops
mittenchops / parmap.py
Last active August 29, 2015 13:57
Multiprocessing for a small set of things all simultaneous
# via here: http://stackoverflow.com/questions/5442910/python-multiprocessing-pool-map-for-multiple-arguments
from multiprocessing import Process, Pipe
from itertools import izip
def spawn(f):
def fun(pipe,x):
pipe.send(f(x))
pipe.close()
return fun
@mittenchops
mittenchops / rmisc.R
Last active August 29, 2015 13:57
miscellaneous R functions
# top 5 with respect to total
head(df[with(df, order(-total)),])
# RSiteSearch("whatever")
`%nin%` <- Negate(`%in%`)
@mittenchops
mittenchops / nin.R
Created February 27, 2014 22:37
R %nin% operator
`%nin%` <- Negate(`%in%`)
@mittenchops
mittenchops / groupby.py
Last active August 29, 2015 13:56
In which I get a better handle on how to use groupby in python to almost be as useful as R's native similar features.
from itertools import groupby, islice
from operator import itemgetter
from pprint import pprint
>>> gb = groupby(sorted(xrange(0,11),key=iseven),iseven)
>>> [','.join(map(str,k)) for g,k in gb]
['1,3,5,7,9', '0,2,4,6,8,10']
>>> sent = "This is a long sentence where I want to group words of similar length using the python groupby function"
>>> gb = groupby(sorted(sent.split(),key=len),len)
@mittenchops
mittenchops / mongo agg
Created February 21, 2014 23:19
Mongo Aggregation reminder
> db.coll.aggregate( [ { $group : {_id:0, minS : {$min: "$variabletomin"}, maxS : {$max : "$variabletomax"} } } ] )
@mittenchops
mittenchops / learn.py
Last active August 29, 2015 13:56
Learning pandas
import pandas
import numpy as np
import string
import random
import matplotlib.pyplot as plt
from pandas import DataFrame
#import statsmodels.formula.api as sm
df = DataFrame(np.random.randn(10,3))
df['3'] = random.sample(string.letters,10)
@mittenchops
mittenchops / TrueOCR.sh
Last active August 29, 2015 13:56
Convert a PDF with no text data into a text file
# https://launchpad.net/~gezakovacs/+archive/pdfocr
pdfocr -i "$file" -o /tmp/tmp.pdf
pdftotext /tmp/tmp.pdf "`basename "$file" .pdf`.txt"
@mittenchops
mittenchops / pylist2rlist.py
Created February 13, 2014 22:34
Export python list to R
lambda x: 'c({})'.format(x).replace("[","").replace("]","")
@mittenchops
mittenchops / histmaker.py
Last active August 29, 2015 13:56
Prepare a histogram in python, functional like whoa
mylist = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 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