If it won't be simple, it simply won't be. [Hire me, source code] by Miki Tebeka, CEO, 353Solutions

Tuesday, March 17, 2009


New style classes have a __subclasses__ method which I find useful the problem of "find me all instances to run" - without any metaclass black voodoo :)

A trivial example:
#!/usr/bin/env python

class Animal(object):

class Dog(Animal):
def talk(self):
return "whoof whoof"

class Cat(Animal):
def talk(self):
return "miao"

class Pig(Animal):
def talk(self):
return "oink oink"

def all_animals():
return Animal.__subclasses__()

if __name__ == "__main__":
for animal in all_animals():
print "%s says: %s" % (animal.__name__, animal().talk())

This way, if we add a new animal all_animals will work without any change from us.

Wednesday, March 11, 2009


Sometime you need to produce a quick prototype for a site that is an aggregation of several other sites.

One quick way is to glue the parts you want from each site into the page.
We'll use a more relaxed version of trampoline to overcome cross site scripting.


#!/usr/bin/env python
'''Get content of "foreign" web pages, enable cross-site AJAX'''

from urllib2 import urlopen, URLError
from cgi import FieldStorage

if __name__ == "__main__":
import cgitb; cgitb.enable()

form = FieldStorage()
url = form.getvalue("url", "")
if not url:
raise SystemExit("no url")

o = urlopen(url)
except (URLError, ValueError), e:
raise SystemExit("error: %s" % e)

print "Content-type: %(content-type)s\n" % o.headers
print o.read()
And then in our page we'll glue the parts. I'm using the same part from Weather Underground but with different area. (Ignoring the fact they have a good API :)


h1.title {
text-align: center;
.header {
background: blue;
color: white;
font-size: 1.3em;
font-weight: bold;
font-variant: small-caps;
div.left {
float: left;
width: 50%;
height: 400px;
overflow: auto;
div.right {
float: right;
width: 50%;
height: 400px;
overflow: auto;
<h1 class="title">Weather Meshup</h1>

<div class="left">
<div class="header">Beverly Hills</div>
<div id="bh">Loading ...</div>

<div class="right">
<div class="header">New York</div>
<div id="ny">Loading ...</div>
<div class="left">
<div class="header">Chicago</div>
<div id="ch">Loading ...</div>

<div class="right">
<div class="header">Washington</div>
<div id="wa">Loading ...</div>
<script src="jquery.js"></script>
function weather_url(zipcode) {
return 'http://www.wund.com/cgi-bin/findweather/getForecast' +
'?query=' + zipcode;

function load_foreign(id, url, selector) {
$('#' + id).load('redirect.cgi ' + selector, {url: url});

function load_weather(id, zipcode) {
var url = weather_url(zipcode);
load_foreign(id, url, 'div#conditions');

function on_ready()
load_weather('bh', '90210');
load_weather('ny', '10001');
load_weather('ch', '60290');
load_weather('wa', '20001');


Blog Archive