Skip to content

Instantly share code, notes, and snippets.

View diogobaeder's full-sized avatar

Diogo Baeder diogobaeder

View GitHub Profile
@diogobaeder
diogobaeder / gist:3738165
Created September 17, 2012 15:56
Alternative to discard stdout
from contextlib import contextmanager
from StringIO import StringIO
import os
import sys
from unittest import TestCase
class MyTest(TestCase):
@contextmanager
def discard_print(self):
class UbuntuServer(Role):
def provision(self):
self.log('Starting provisioning for Cumbuca Chic...')
self._check_user()
self._setup_postgres()
self._install_deb_packages()
self._install_python_packages()
self.log('Finished provisioning for Cumbuca Chic')
def _check_user(self):
@diogobaeder
diogobaeder / observer.py
Created June 8, 2012 06:02
Observer example for GOOS discussion list
#!/usr/bin/env python
'''
Just a silly example for an Observer implementation using the "push event" method. See discussion about it:
https://groups.google.com/forum/?fromgroups#!topic/growing-object-oriented-software/KwJPxUZ0l_M
'''
class OrderTrackingEvent(object):
def __init__(self, order_number, status):
self.order_number = order_number
self.status = status
@diogobaeder
diogobaeder / sorter.py
Created May 27, 2012 06:45
Quicksort memorization
#!/usr/bin/env python
'''
Just another quicksort implementation, to see if I memorized the algorithmn.
Apparently, yes. :-)
Gotchas during the implementation:
- forgot to check min and max in the beginning, got IndexError
- choosing nonsense as pivot led me to almost-sorted list (worked when
I stopped being stupid)
'''
@diogobaeder
diogobaeder / frange.py
Created September 24, 2011 23:16
Floating-point range generator (IEEE-754-proof)
#!/usr/bin/env python
'''
This is an attempt of an alternative to
http://code.activestate.com/recipes/577068-floating-point-range/
but generating the expected floating-point elements in the range,
avoiding floating-point arithmetic problems.
Currently it works rather well, although without the original
validation steps (which seem to be overengineering to me), considering
@diogobaeder
diogobaeder / tornado-zmq.py
Created July 11, 2011 06:24
Little dumb experiment with Tornado and 0MQ
# This was a little experiment I've put together in some minutes while in TDC2011 (The Developers Conference), at São Paulo
# It's just a silly almost-Hello-World, just to show the assynchronous communication working with non-blocking I/O through event loops, mixed with a simple HTTP server
# First, put this on a file called 'server.py', and run:
#$ ./server.py 8001 &
#$ ./server.py 8002 &
#$ ./server.py 8003 &
#!/usr/bin/env python
@diogobaeder
diogobaeder / factorialdigitssum.py
Created June 22, 2011 16:57
Soma dos dígitos do fatorial
from math import factorial
from operator import add
def factorial_digits_sum(number):
return reduce(
add,
[int(string_digit) for string_digit in str(
factorial(number)
)]
/*
A simple helper for cleaning MongoDB collections in NodeJS, using mongoose.
They act as setup/teardown cleaners
*/
// lib/tools.js
exports.chainedCalls = function() {
var funcs = Array.prototype.slice.call(arguments, 0);
function callNext() {
if (funcs.length) {
// Just a simple array division to share with a friend
function divideArray(array, parts) {
if (parts == 0) return [];
var length = array.length,
blockSize = ((length + (length % parts)) / parts),
blocks = [], i;
for (i = 0; i < length; i += blockSize) {
blocks.push(array.slice(i, i + blockSize));
}
return blocks;
/**
* Little trick to list the MP3 paths
* from the great Jim Beard's web pages
* (one of the best Jazz composers I've known).
* Enjoy.
*/
var pattern = /Beard\smp3s\/.+\.mp3/,
base = "http://www.jimbeard.com/",
paths = [];
$$('a').forEach(function(el, i){