This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from contextlib import contextmanager | |
| from StringIO import StringIO | |
| import os | |
| import sys | |
| from unittest import TestCase | |
| class MyTest(TestCase): | |
| @contextmanager | |
| def discard_print(self): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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) | |
| ''' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | |
| )] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| 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) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 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){ |