Skip to content

Instantly share code, notes, and snippets.

View rcalsaverini's full-sized avatar

Rafael Calsaverini rcalsaverini

View GitHub Profile
@rcalsaverini
rcalsaverini / contsince.py
Created June 12, 2012 20:14
Get running time of a process given a pid
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from subprocess import check_output
from sys import argv
from time import time
cmd = lambda pid: "stat -t /proc/%s | awk '{print $14}'" % (pid,)
@rcalsaverini
rcalsaverini / greptime
Created June 15, 2012 14:33
Get running time of a process given a pid
#!/bin/bash
pidlist=`ps ax | grep -i -E $1 | grep -v grep | awk '{print $1}' | grep -v PID | xargs echo`
for pid in $pidlist; do
sincetime $pid
done
@rcalsaverini
rcalsaverini / xmonad.hs
Created September 3, 2012 12:35
my xmonad.hs
-- basic imports
import XMonad
-- needed for layouts
import XMonad.Layout.Grid
-- needed for hot keys
import XMonad.Util.EZConfig(additionalKeys)
import XMonad.Actions.CycleWS (nextWS, prevWS, shiftToNext, shiftToPrev)
@rcalsaverini
rcalsaverini / testpymc.py
Created September 20, 2012 20:06
Testing pymc
import pymc
import numpy
def model(disasters):
size = len(disasters)
low = pymc.Exponential('low', beta=1.)
high = pymc.Exponential('high', beta=2.)
@pymc.stochastic(dtype=int)
@rcalsaverini
rcalsaverini / mongowatch.py
Created September 26, 2012 13:19
Script to monitor an indexing operation on mongodb
import pymongo
import re
import time
from datetime import datetime, timedelta
patt = re.compile("\d*/\d*\s\d*%")
class Operations(object):
def __init__(self, db, host='localhost'):
self.conn = pymongo.Connection(host)
@rcalsaverini
rcalsaverini / Singleton.py
Created October 8, 2012 00:16
A Singleton metaclass in python:
class Singleton(type):
""" This is a Singleton metaclass. All classes affected by this metaclass
have the property that only one instance is created for each set of arguments
passed to the class constructor."""
def __init__(cls, name, bases, dict):
super(Singleton, cls).__init__(cls, bases, dict)
cls._instanceDict = {}
def __call__(cls, *args, **kwargs):
@rcalsaverini
rcalsaverini / countourplot.py
Created March 22, 2013 16:56
Contourplot with matplotlib
import pandas
import datetime
from pylab import *
data = pandas.read_csv('file.csv', index_col = 0, header = 0, sep = '\t')
timesIndex = data.index
dynamapsIndex = data.columns
levels = map(lambda x : round(x,1), linspace(data.min().min(), data.max().max(), 101))
contourf(data, levels = levels, cmap=plt.cm.jet_r, interpolation = 'bicubic')
from mpl_toolkits.basemap import Basemap
import numpy as np
import matplotlib.pyplot as plt
m = Basemap(llcrnrlon=-73.45,llcrnrlat=-35,urcrnrlon=-30.1,urcrnrlat=6, resolution='i',projection='lcc',lon_0=-45,lat_0=-23.7)
m.drawcoastlines()
m.fillcontinents(color='beige',lake_color='aqua')
# draw parallels and meridians.
m.drawparallels(np.arange(-40,10,1.))
m.drawmeridians(np.arange(-80.,-0,1.))
@rcalsaverini
rcalsaverini / InvalidXMLFilter.py
Last active December 20, 2015 23:29
To filter invalid XML characters in python
#Based on an answer by John Machin on Stack Overflow (http://stackoverflow.com/users/84270/john-machin)
#http://stackoverflow.com/questions/8733233/filtering-out-certain-bytes-in-python
def isValidXMLChar(char):
codepoint = ord(char)
return 0x20 <= codepoint <= 0xD7FF or \
codepoint in (0x9, 0xA, 0xD) or \
0xE000 <= codepoint <= 0xFFFD or \
0x10000 <= codepoint <= 0x10FFFF
@rcalsaverini
rcalsaverini / crawl.py
Last active August 22, 2017 17:43
Crawl Futpedia for brazilian soccer data.
"""
Crawling brazilian soccer results from
http://futpedia.globo.com/campeonato/campeonato-brasileiro/2011#/fase=fase-unica/rodada=1
With:
http://www.clips.ua.ac.be/pages/pattern-web
"""
import pandas
from pattern import web