Voting

Category

real language

Bookmarking

Del.icio.us Digg Diigo DZone Earthlink Google Kick.ie
Windows Live LookLater Ma.gnolia Reddit Rojo StumbleUpon Technorati

Language Grok

(Web application framework based on Zope3)

Date:08/02/08
Author:d2m
URL:http://blog.d2m.at
Comments:2
Info:http://grok.zope.org
Score: (3.00 in 1 votes)
import grok

class Wall(grok.Application,grok.Container):
    
    def add_99_bottles(self):
        for n in range(1,100):
            self[str(n)]=Bottle()

    def describe_me(self):
        num_bottles=len(self.items())
        if num_bottles == 0:
            text='No more bottles of beer'
        elif num_bottles == 1:
            text='1 bottle of beer'
        else:
            text='%d bottles of beer' % num_bottles
        return text
    
    def enum(self):
        return len(self.items())

class Bottle(grok.Model):
    pass

class Index(grok.View):
    grok.context(Wall)

    def still_beer(self):
        return self.context.enum() > 0
    
    def take_one_down(self):
        bottles=self.context.keys()
        del self.context[bottles[-1]]

    def buy_some_more(self):
        self.context.add_99_bottles()
        
    def render(self):
        context=self.context
        out=[]
        while self.still_beer():
            description=context.describe_me()
            out.append('%s on the wall, %s.' % (description, description.lower()))
            self.take_one_down()
            description=context.describe_me()
            out.append('Take one down and pass it around, %s.\n' % description.lower())
        description=context.describe_me()
        out.append('%s on the wall, %s.' % (description, description.lower()))
        self.buy_some_more()
        description=context.describe_me()
        out.append('Go to the store and buy some more, %s.' % description.lower())
        return '\n'.join(out)
        
@grok.subscribe(Wall, grok.IObjectAddedEvent)
def handle(obj, event):
    obj.add_99_bottles()

Download Source | Write Comment

Alternative Versions

Comments

>>  d2m said on 08/28/08 07:50:38

d2m Source for the complete web app is available at http://svn.zope.org/grokapps/gbe99bottles/

>>  RobbiePreston said on 05/30/10 16:00:18

RobbiePreston I received 1 st <a href="http://lowest-rate-loans.com">loans</a> when I was not very old and that helped my family a lot. However, I need the secured loan over again.

Download Source | Write Comment

Add Comment

Please provide a value for the fields Name, Comment and Security Code.
This is a gravatar-friendly website.
E-mail addresses will never be shown.
Enter your e-mail address to use your gravatar.

Please don't post large portions of code here! Use the form to submit new examples or updates instead!

Name:

eMail:

URL:

Security Code:
  
Comment: