Skip to content

Instantly share code, notes, and snippets.

import random
import time
import sys
from multiprocessing import Pool
random.seed()
def genList (size):
randomList = []
#initialize random list with values between 0 and 100
for i in range(size):
def git_version():
from subprocess import Popen, PIPE
gitproc = Popen(['git', 'rev-parse','HEAD'], stdout = PIPE)
(stdout, _) = gitproc.communicate()
return stdout.strip()
@philippstroehle
philippstroehle / command_line_parsing.py
Created November 7, 2013 21:20
command line parsing in python, useful for parallelizing stuff on multicore machines
import argparse
def main(c=1, C=1):
for iteration in [iteration for iteration in range(config.iterations) if iteration % C == c - 1]:
logger.info("This is iteration %s" % iteration)
seed(iteration)
np.random.seed(iteration)
pass
R> dd[with(dd, order(-z, b)), ]
b x y z
4 Low C 9 2
2 Med D 3 1
1 Hi A 8 1
3 Hi A 9 1
# do this before importing pylab or pyplot
import matplotlib
matplotlib.use('Agg')
from matplotlib import pyplot as plt
plot.area<-function(x,col=NULL,horiz=F,prop=T,stp.ln=T,grp.ln=T,axs.cex=1,axs.lab=T,lab.cex=1,
names=c('Group','Step','Value'),...){
#sort out color fector
if(!is.null(col)){
if(sum(col %in% colors()) != length(col)) stop('col vector must be in "colors()"')
col<-colorRampPalette(col)(ncol(x))
}
else col<-colorRampPalette(c('lightblue','green'))(ncol(x))
@philippstroehle
philippstroehle / profileMemory.py
Created September 16, 2013 20:46
Profiling runtime and memory
@profile # python -m memory_profiler example.py
def my_func():
a = [1] * (10 ** 6)
b = [2] * (2 * 10 ** 7)
del b
return a
if __name__ == '__main__':
my_func()
>>> from pylab import *
>>> plot([1,3,2])
[<matplotlib.lines.Line2D object at 0x102bc8950>]
>>> gca()
<matplotlib.axes.AxesSubplot object at 0x102790cd0>
>>> gca().add_patch(Rectangle((1,1),1,1))
<matplotlib.patches.Rectangle object at 0x102790510>
>>> savefig("rect.png")
@philippstroehle
philippstroehle / Introspection_example.py
Created September 14, 2013 11:23
python introspection
baseClass = self.__class__.__name__
methodToCall = getattr(self,"selectJobs"+baseClass)
#now = self.selectJobsExpectation(J)
now = methodToCall(J)