Skip to content

Instantly share code, notes, and snippets.

View justinabrahms's full-sized avatar

Justin Abrahms justinabrahms

View GitHub Profile
from flask import Module, request, redirect, url_for, render_template, abort
from formalchemy import FieldSet
from example import db
from example import Service, User, Forum, Category, Topic, Post
mod = Module(__name__)
models = {
# from __future__ import with_statement # py2.5
from contextlib import contextmanager
import logging
@contextmanager
def log_execution_time(log_msg):
"""
Context manager which logs the time spent doing a
particular activity.
# START_TIME is an instance of datetime.time()
datetime.combine(datetime.today(), START_TIME)
now = datetime.today()
time_list = now.timetuple()[:3] # get year,month,day
time_list.extend([START_TIME.hour, START_TIME.minute])
start_datetime = datetime(*time_list)
from collections import defaultdict
import logging
class ErrorDict(defaultdict):
"""
Stores a list of errors per host, when initialized with a list.
>>> ed = ErrorDict()
>>> ed['google.com'].append('first')
>>> ed['google.com'].append('another error')
class TestRound(object):
def _check_rounding(self, test, expected):
assert round(test) == expected
def test_rounding(self):
for x, y in [(1, 1),
(1.9, 2)]:
yield self._check_rounding, x, y
# VERSUS
#!/usr/bin/env python2.6
#
# Simple class I used for a little while to play my D&D character when I didn't have dice. Worked okay, but I then found dice.
#
from collections import defaultdict
from random import randint
def roll(dice_type):
rolls = []
mod = 0
#!/usr/bin/env python
import time
import nose
from nose.plugins.base import Plugin
class NoseTimer(Plugin):
"""
Times each individual test and outputs them in reverse order of
speed (highest first)
@justinabrahms
justinabrahms / pyc0n.js
Created December 3, 2010 03:38
bot to handle votes for pycon selection.
// pycon talk selection irc bot.
var irc = require('../../node-irc/lib/irc.js');
var sys = require('sys');
var imb0t = function () {
return {
pattern_list: [],
client: undefined,
position: -1, // starts at -1 so we can increment it on the first ,next to make it 0, which properly indexes
@justinabrahms
justinabrahms / gist:831856
Created February 17, 2011 14:53
Simple tree walking using goroutines.
package main
import (
"tour/tree"
"fmt"
)
func Walk(tree *tree.Tree, ch chan int) {
if tree != nil {
Walk(tree.Left, ch)