Skip to content

Instantly share code, notes, and snippets.

View kghose's full-sized avatar
🕉️

Kaushik Ghose kghose

🕉️
View GitHub Profile
@kghose
kghose / pretty_axes.py
Created July 25, 2011 18:35
Code to make pretty looking axes ready for plotting, using matplotlib
def pretty_axes(ax):
"""ax is a matplotlib axis instance."""
ax.spines['left'].set_position('zero')
ax.spines['bottom'].set_position('zero')
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
for tick in ax.xaxis.get_major_ticks():
tick.tick2On = False
for tick in ax.yaxis.get_major_ticks():
tick.tick2On = False
@kghose
kghose / histbug.py
Created July 27, 2011 15:04
Illustrates a small onscreen display bug in matplotlib hist
import pylab
sigma=.1
pylab.figure(figsize=(12,3))
r = pylab.randn(1000000)*sigma
pylab.hist(r,bins=200,range=[-3,3],normed=True, align='mid',histtype='stepfilled',color='gray')
x = pylab.linspace(-3,3,200)
y = (1./(2*pylab.pi*sigma**2)**.5)*pylab.exp(-x**2/(2*sigma**2))
pylab.plot(x,y,'k')
pylab.setp(pylab.gca(), xlim=[-3,3])
@kghose
kghose / gist:1119206
Created August 1, 2011 23:06
Mac OS X StartupItem script (can be generalized)
#!/bin/sh
##
# Diary service startup script
##
. /etc/rc.common
StartService ()
{
@kghose
kghose / queuing.py
Created August 9, 2011 00:44
Blocking, Polling, Pipes, Queues in multiprocessing and how they are affected by the OS's time-slicing schedule
import random
from multiprocessing import Process, Queue, Pipe
import sys
import time #For the timestamp and sleep function
if sys.platform == "win32":
# On Windows, the best timer is time.clock()
default_timer = time.clock
else:
# On most other platforms, the best timer is time.time()
default_timer = time.time
@kghose
kghose / main.cpp
Created November 20, 2011 20:34
How to get file creation time on Mac OS X (64 bit)
/*
* QFileInfo.created() on POSIX systems (like Mac OS X) returns the last modified time[1].
* This is annoying because on Mac OS X you know the system (e.g. finder) has access to the actual creation time.
* Turns out that BSD systems (like Mac OS X) have an extension to sys/stat.h that contains the file creation time[2].
[1]: http://doc.qt.nokia.com/latest/qfileinfo.html#created
[2]: http://developer.apple.com/library/IOS/#documentation/System/Conceptual/ManPages_iPhoneOS/man2/stat.2.html
The recipe, then is (shown using the QT framework)
*/
@kghose
kghose / flickrfaves.py
Created July 4, 2012 16:26
Short python script using flickrapi to grab favorites from your account
import flickrapi, urllib, os, argparse
parser = argparse.ArgumentParser(description='Grab favorites from a user')
parser.add_argument('-f', '--folder', help='Folder', default='./')
parser.add_argument('-n', '--number', help='Number', default=10)
parser.add_argument('-k', '--apikey', help='Api Key')
parser.add_argument('-s', '--apisecret', help='Api Secret')
parser.add_argument('-u', '--userid', help='User id')
args = parser.parse_args()
@kghose
kghose / pyboy4000.py
Created August 15, 2012 04:34
pyboy4000 - your companion to the pipboy3000 for all your hacking needs.
#!python
"""pyboy4000 - your companion to the pipboy3000 for all your hacking needs.
Simply fireup pyboy, enter all the choice words, then, as you try a word in the
terminal during hacking, enter the number of correct letters for each word tried.
pyboy will do the thinking for you. It will tell you if it has found a match, or
it will suggest the next word to try.
The algorithm used is as follows:
Keep a list of candidate words. In the beginning this is the whole list.
@kghose
kghose / p01.py
Last active December 16, 2015 22:49
One of the features of the Pandas library that I like the most is hierarchical indexing. The use of hierarchical indexing is illustrated by the following examples:
import pandas as pd, pylab
idx = pd.MultiIndex.from_tuples([('a','x'),('a','y'),('b','x'),('b','y')])
col = pd.MultiIndex.from_tuples([('c1',0),('c1',1),('c2',0)],names=['f','s'])
dat = pylab.randn(len(idx),len(col))
df1 = pd.DataFrame(dat, index=idx, columns=col)
@kghose
kghose / gitautocheckout.txt
Last active December 17, 2015 02:38
Setting up to push code from a developer machine to a production machine and have it automatically checkout the latest push.
On production machine
---------------------
mkdir wm.git
mkdir wm
cd wm.git
git init --bare
create a file: hooks/post-recieve
@kghose
kghose / pipetest.py
Created July 11, 2013 13:35
Demonstration of the limitation of Python's subprocess.PIPE
from subprocess import Popen, PIPE
import argparse, time
def execute(n):
p = Popen(['python', 'test.py', '-n', str(n)], stdin=PIPE, stdout=PIPE, stderr=PIPE)
p.wait()
return p.stdout.read().splitlines()
def execute_long(n):
with open('query.txt','w') as stdout: