Skip to content

Instantly share code, notes, and snippets.

@vpetro
vpetro / parser.py
Created March 1, 2011 19:12
SAX-based parser to extract blocks of elements as Python dictionaries
from xml.sax import ContentHandler, parseString
from datetime import datetime
class Parser(ContentHandler):
def __init__(self, block_name, attrs=None):
ContentHandler.__init__(self)
self._block_name = block_name
self._name = None
self._blocks = list()
self._current_block = None
@vpetro
vpetro / bar_plot.py
Created May 13, 2011 19:44
plotting bar graphs
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
def plot_state_history(filename):
fname = filename
# get the values
# values = [0, 1, 0, 3, 0, 5 ...]
values = [float(i) for i in open(fname).readlines()[0].split()]
data = [values.count(i) for i in range(1, 6)]
@vpetro
vpetro / gist:1161360
Created August 21, 2011 23:55
git pre-commit hook for preventing commits that contain untested lines with python/nose
#!/usr/bin/env python
import os
import re
import sys
temp_filename = '/tmp/hook_file'
def _read_test_output():
total_output = {}
command = "nosetests --with-coverage"
@vpetro
vpetro / gist:1174019
Created August 26, 2011 18:10
Return multiple items from a mocked function with Python's mock.
import mock
def returnList(items):
def func():
for item in items:
yield item
yield mock.DEFAULT
generator = func()
@vpetro
vpetro / gist:1203926
Created September 8, 2011 17:02
function to put function parameters one-per-line.
" NOTES:
" ^M must be replaced with <ctrl-v><ctrl-m>
" ^[ must be replace with <ctl-v><esc>
function! BreakParameters()
" move the start of the line and set a mark 'a'
normal ^ma
" get break the parameters onto their own line
normal f(a^M^[
" replace the commas with linebreaks
@vpetro
vpetro / gist:1204166
Created September 8, 2011 18:17
add docstring for python function in vim
function! WriteParams()
python << endpython
import re
import vim
# get the function definition line
line = vim.eval("getline(line('.'))")
# get the number of spaces to add to the start of the line
num_spaces = 4 + len(line) - len(line.lstrip())
# get the line number wher to do the insertion
@vpetro
vpetro / .vimrc
Created October 19, 2011 17:33
my vimrc
filetype off
call pathogen#helptags()
call pathogen#runtime_append_all_bundles()
filetype on
set nocompatible
set hidden
set t_Co=256
" Indenting {
@vpetro
vpetro / gist:1322960
Created October 28, 2011 18:17
petro
#!/bin/bash
set -e
CURDIR=`pwd`
AUTHOR=$1
FORMAT="%C(yellow)%h%Creset -%C(red)%d%Creset %s %Cgreen(%ar) %C(bold blue)<%an>%Creset"
SINCE="15 months ago"
UNTIL="14 months ago"
DIRS=`find . -type d -name ".*git" -exec dirname {} \; | egrep -v "\w+/GazaroUtilities" | sed "s/^..//"`
@vpetro
vpetro / gist:1340269
Created November 4, 2011 19:33
remove some elements from stackoverflow.com when using pentadactyl
javascript <<EOF
function cleanStackOverflow() {
$("#sidebar, #content, #mainbar, #question, #answers, div.post-text").css('width', '100%');
}
EOF
autocmd DOMLoad stackoverflow.com -js cleanStackOverflow();
@vpetro
vpetro / add1.py
Created August 8, 2012 15:23
add1
def add1(lst, val, n):
start, stop, step = 0, len(lst), 1
cond = n == 0
if n < 0:
start, stop = stop-1, start-1
step = -1
n = abs(n)
for idx in range(start, stop, step):