In Passing




follow icilalune at http://twitter.com

Saturday, July 10, 2010

Emily in Python


#! /usr/bin/python
# Emily translated by Alex Martelli & Anna Ravenscroft

import math

class EmilyWasWrong (Exception): pass

def do_compare (item):
action={sea:(hold,'blue'), sky:(put,'side'), God:(heft,'pound')}
verb, attribute = action [item]
return verb (getattr (brain, attribute), getattr (item, attribute))

def put (brain_side, sky_side):
if sky_side in brain_side and ease in brain_side and you in brain_side:
return 'wider'
else: raise EmilyWasWrong ('put')

def hold (brain_color, sea_color):
if absorb (brain_color, sea_color) == absorb (buckets, sponges):
return 'deeper'
else: raise EmilyWasWrong ('hold')

def heft (brain_weight, God_weight):
if abs (brain_weight - God_weight) <= abs (syllable - sound):
return 'just the weight'
else: raise EmilyWasWrong ('heft')

class Thing (object):
def __init__(self, **a): self.__dict__ = a

sea = Thing (blue='deep')
sky = Thing (side='up')
God = Thing (pound=(1.0+math.sqrt (5.0))/2.0)
ease = 'easy'
you = 'xx'

def absorb (container, item):
return item in container

sponges = 'absorb liquids'
buckets = [sponges]
syllable = 256.1
sound = 256.0

class Brain (object):
side = 'up easy xx'.split ()
blue = ['deep']
pound = 1.61

brain = Brain ()

for comparison in (sky, sea, God):
print do_compare (comparison),
print



********************************************************************
CONSOLE OUTPUT:

Python 2.6.3 (r263:75183, Nov 1 2009, 10:56:20)
[GCC 4.2.1 (Apple Inc. build 5646) (dot 1)] on darwin
Type "copyright", "credits" or "license()" for more information.

IDLE 2.6.3
>>>
wider deeper just the weight
>>>


*********************************************************************


Original poem:

The brain is wider than the sky,
For, put them side by side,
The one the other will include
With ease, and you beside.

The brain is deeper than the sea,
For, hold them, blue to blue,
The one the other will absorb,
As sponges, buckets do.

The brain is just the weight of God,
For, lift them, pound for pound,
And they will differ, if they do,
As syllable from sound.

-- Emily Dickinson (1924)

0 comments:

Template Designed by Douglas Bowman - Updated to Beta by: Blogger Team
Modified for 3-Column Layout by Hoctro