[Checkins] SVN: grok/trunk/doc/examples/a imported adder and
animaltree examples
Luciano Ramalho
luciano at ramalho.org
Sun Jul 15 09:54:35 EDT 2007
Log message for revision 78003:
imported adder and animaltree examples
Changed:
A grok/trunk/doc/examples/adder/
A grok/trunk/doc/examples/adder/src/
A grok/trunk/doc/examples/adder/src/Adder.egg-info/
A grok/trunk/doc/examples/adder/src/Adder.egg-info/PKG-INFO
A grok/trunk/doc/examples/adder/src/Adder.egg-info/SOURCES.txt
A grok/trunk/doc/examples/adder/src/Adder.egg-info/dependency_links.txt
A grok/trunk/doc/examples/adder/src/Adder.egg-info/entry_points.txt
A grok/trunk/doc/examples/adder/src/Adder.egg-info/not-zip-safe
A grok/trunk/doc/examples/adder/src/Adder.egg-info/paster_plugins.txt
A grok/trunk/doc/examples/adder/src/Adder.egg-info/requires.txt
A grok/trunk/doc/examples/adder/src/Adder.egg-info/top_level.txt
A grok/trunk/doc/examples/adder/src/adder/
A grok/trunk/doc/examples/adder/src/adder/README.txt
A grok/trunk/doc/examples/adder/src/adder/__init__.py
A grok/trunk/doc/examples/adder/src/adder/app.py
A grok/trunk/doc/examples/adder/src/adder/app_templates/
A grok/trunk/doc/examples/adder/src/adder/app_templates/index.pt
A grok/trunk/doc/examples/adder/src/adder/configure.zcml
A grok/trunk/doc/examples/adder/src/adder/ftests/
A grok/trunk/doc/examples/adder/src/adder/ftests/__init__.py
A grok/trunk/doc/examples/adder/src/adder/ftests/index_view.py
A grok/trunk/doc/examples/adder/src/adder/ftests/test_adder_functional.py
A grok/trunk/doc/examples/adder/src/adder/static/
A grok/trunk/doc/examples/adder/src/adder/static/adder.css
A grok/trunk/doc/examples/adder/src/adder/tests/
A grok/trunk/doc/examples/adder/src/adder/tests/__init__.py
A grok/trunk/doc/examples/adder/src/adder/tests/test_adder.py
A grok/trunk/doc/examples/adder/src/runtests.sh
A grok/trunk/doc/examples/animaltree/
A grok/trunk/doc/examples/animaltree/src/
A grok/trunk/doc/examples/animaltree/src/AnimalTree.egg-info/
A grok/trunk/doc/examples/animaltree/src/AnimalTree.egg-info/PKG-INFO
A grok/trunk/doc/examples/animaltree/src/AnimalTree.egg-info/SOURCES.txt
A grok/trunk/doc/examples/animaltree/src/AnimalTree.egg-info/dependency_links.txt
A grok/trunk/doc/examples/animaltree/src/AnimalTree.egg-info/entry_points.txt
A grok/trunk/doc/examples/animaltree/src/AnimalTree.egg-info/not-zip-safe
A grok/trunk/doc/examples/animaltree/src/AnimalTree.egg-info/paster_plugins.txt
A grok/trunk/doc/examples/animaltree/src/AnimalTree.egg-info/requires.txt
A grok/trunk/doc/examples/animaltree/src/AnimalTree.egg-info/top_level.txt
A grok/trunk/doc/examples/animaltree/src/animaltree/
A grok/trunk/doc/examples/animaltree/src/animaltree/README.txt
A grok/trunk/doc/examples/animaltree/src/animaltree/__init__.py
A grok/trunk/doc/examples/animaltree/src/animaltree/app.py
A grok/trunk/doc/examples/animaltree/src/animaltree/app_templates/
A grok/trunk/doc/examples/animaltree/src/animaltree/app_templates/correct.pt
A grok/trunk/doc/examples/animaltree/src/animaltree/app_templates/guess.pt
A grok/trunk/doc/examples/animaltree/src/animaltree/app_templates/index.pt
A grok/trunk/doc/examples/animaltree/src/animaltree/app_templates/learn.pt
A grok/trunk/doc/examples/animaltree/src/animaltree/configure.zcml
-=-
Added: grok/trunk/doc/examples/adder/src/Adder.egg-info/PKG-INFO
===================================================================
--- grok/trunk/doc/examples/adder/src/Adder.egg-info/PKG-INFO (rev 0)
+++ grok/trunk/doc/examples/adder/src/Adder.egg-info/PKG-INFO 2007-07-15 13:54:35 UTC (rev 78003)
@@ -0,0 +1,10 @@
+Metadata-Version: 1.0
+Name: Adder
+Version: 0.0
+Summary: UNKNOWN
+Home-page: UNKNOWN
+Author: UNKNOWN
+Author-email: UNKNOWN
+License: UNKNOWN
+Description: UNKNOWN
+Platform: UNKNOWN
Added: grok/trunk/doc/examples/adder/src/Adder.egg-info/SOURCES.txt
===================================================================
--- grok/trunk/doc/examples/adder/src/Adder.egg-info/SOURCES.txt (rev 0)
+++ grok/trunk/doc/examples/adder/src/Adder.egg-info/SOURCES.txt 2007-07-15 13:54:35 UTC (rev 78003)
@@ -0,0 +1,11 @@
+setup.py
+src/Adder.egg-info/PKG-INFO
+src/Adder.egg-info/SOURCES.txt
+src/Adder.egg-info/dependency_links.txt
+src/Adder.egg-info/entry_points.txt
+src/Adder.egg-info/not-zip-safe
+src/Adder.egg-info/paster_plugins.txt
+src/Adder.egg-info/requires.txt
+src/Adder.egg-info/top_level.txt
+src/adder/__init__.py
+src/adder/app.py
Added: grok/trunk/doc/examples/adder/src/Adder.egg-info/dependency_links.txt
===================================================================
--- grok/trunk/doc/examples/adder/src/Adder.egg-info/dependency_links.txt (rev 0)
+++ grok/trunk/doc/examples/adder/src/Adder.egg-info/dependency_links.txt 2007-07-15 13:54:35 UTC (rev 78003)
@@ -0,0 +1 @@
+
Added: grok/trunk/doc/examples/adder/src/Adder.egg-info/entry_points.txt
===================================================================
--- grok/trunk/doc/examples/adder/src/Adder.egg-info/entry_points.txt (rev 0)
+++ grok/trunk/doc/examples/adder/src/Adder.egg-info/entry_points.txt 2007-07-15 13:54:35 UTC (rev 78003)
@@ -0,0 +1,3 @@
+
+ # -*- Entry points: -*-
+
\ No newline at end of file
Added: grok/trunk/doc/examples/adder/src/Adder.egg-info/not-zip-safe
===================================================================
--- grok/trunk/doc/examples/adder/src/Adder.egg-info/not-zip-safe (rev 0)
+++ grok/trunk/doc/examples/adder/src/Adder.egg-info/not-zip-safe 2007-07-15 13:54:35 UTC (rev 78003)
@@ -0,0 +1 @@
+
Added: grok/trunk/doc/examples/adder/src/Adder.egg-info/paster_plugins.txt
===================================================================
--- grok/trunk/doc/examples/adder/src/Adder.egg-info/paster_plugins.txt (rev 0)
+++ grok/trunk/doc/examples/adder/src/Adder.egg-info/paster_plugins.txt 2007-07-15 13:54:35 UTC (rev 78003)
@@ -0,0 +1 @@
+PasteScript
Added: grok/trunk/doc/examples/adder/src/Adder.egg-info/requires.txt
===================================================================
--- grok/trunk/doc/examples/adder/src/Adder.egg-info/requires.txt (rev 0)
+++ grok/trunk/doc/examples/adder/src/Adder.egg-info/requires.txt 2007-07-15 13:54:35 UTC (rev 78003)
@@ -0,0 +1,2 @@
+setuptools
+grok
\ No newline at end of file
Added: grok/trunk/doc/examples/adder/src/Adder.egg-info/top_level.txt
===================================================================
--- grok/trunk/doc/examples/adder/src/Adder.egg-info/top_level.txt (rev 0)
+++ grok/trunk/doc/examples/adder/src/Adder.egg-info/top_level.txt 2007-07-15 13:54:35 UTC (rev 78003)
@@ -0,0 +1 @@
+adder
Added: grok/trunk/doc/examples/adder/src/adder/README.txt
===================================================================
--- grok/trunk/doc/examples/adder/src/adder/README.txt (rev 0)
+++ grok/trunk/doc/examples/adder/src/adder/README.txt 2007-07-15 13:54:35 UTC (rev 78003)
@@ -0,0 +1,8 @@
+Put your application code in this Python package.
+
+app_templates
+ Place Page Templates (file extension '.pt') in this directory. They
+ will automatically be associated as views with the model in app.py.
+
+static
+ Place static resources such as CSS, JS, images, etc. in here.
Added: grok/trunk/doc/examples/adder/src/adder/__init__.py
===================================================================
--- grok/trunk/doc/examples/adder/src/adder/__init__.py (rev 0)
+++ grok/trunk/doc/examples/adder/src/adder/__init__.py 2007-07-15 13:54:35 UTC (rev 78003)
@@ -0,0 +1 @@
+# this directory is a package
Added: grok/trunk/doc/examples/adder/src/adder/app.py
===================================================================
--- grok/trunk/doc/examples/adder/src/adder/app.py (rev 0)
+++ grok/trunk/doc/examples/adder/src/adder/app.py 2007-07-15 13:54:35 UTC (rev 78003)
@@ -0,0 +1,49 @@
+import grok
+from persistent.list import PersistentList
+
+class Adder(grok.Application, grok.Model):
+ """An adding machine with tape
+
+ >>> adder = Adder()
+ >>> adder.total
+ 0.0
+ >>> adder.addTerm(0)
+ 0.0
+ >>> '%.2f' % adder.addTerm(1.2)
+ '1.20'
+ >>> '%.2f' % adder.addTerm(-1)
+ '0.20'
+
+ Besides adding, Adder also contains a history of the added terms
+
+ >>> ['%.2f' % term for term in adder.terms]
+ ['0.00', '1.20', '-1.00']
+
+ """
+ def __init__(self):
+ super(Adder, self).__init__()
+ self.message = None
+ self.clear()
+
+ def clear(self):
+ self.terms = PersistentList()
+ self.total = 0.0
+
+ def addTerm(self, term):
+ self.terms.append(term)
+ self.total += term
+ return self.total
+
+class Index(grok.View):
+ message = None
+
+ def update(self, term=None):
+ if self.request.has_key('bt_clear'):
+ self.context.clear()
+ elif term:
+ try:
+ term = float(term)
+ except ValueError:
+ self.message = "Invalid number."
+ else:
+ self.context.addTerm(term)
Added: grok/trunk/doc/examples/adder/src/adder/app_templates/index.pt
===================================================================
--- grok/trunk/doc/examples/adder/src/adder/app_templates/index.pt (rev 0)
+++ grok/trunk/doc/examples/adder/src/adder/app_templates/index.pt 2007-07-15 13:54:35 UTC (rev 78003)
@@ -0,0 +1,29 @@
+<html>
+ <head>
+ <link rel="stylesheet" type="text/css" tal:attributes="href static/adder.css">
+ <title>Grok Adder</title>
+ <script type="text/javascript">
+ function setfocus() { document.forms[0].term.focus() }
+ </script>
+ </head>
+ <body onload=setfocus()>
+ <h1>Adder</h1>
+ <span tal:condition="view/message" tal:content="view/message" />
+ <form tal:attributes="action view/url">
+ <input type="text" name="term">
+ <input type="submit" value="+" name="bt_add">
+ <hr>
+ <input tal:condition="context/terms" type="submit" value="Clear" name="bt_clear">
+ </form>
+ <table tal:condition="context/terms">
+ <tr>
+ <th>Total</th>
+ <th tal:content="context/total"></th>
+ </tr>
+ <tr tal:repeat="term context/terms" tal:attributes="class repeat/term/even">
+ <td class="count" tal:content="repeat/term/number">11</td>
+ <td tal:content="term">11</td>
+ </tr>
+ </table>
+ </body>
+</html>
Added: grok/trunk/doc/examples/adder/src/adder/configure.zcml
===================================================================
--- grok/trunk/doc/examples/adder/src/adder/configure.zcml (rev 0)
+++ grok/trunk/doc/examples/adder/src/adder/configure.zcml 2007-07-15 13:54:35 UTC (rev 78003)
@@ -0,0 +1 @@
+<grok package="." xmlns="http://namespaces.zope.org/grok" />
Added: grok/trunk/doc/examples/adder/src/adder/ftests/__init__.py
===================================================================
Added: grok/trunk/doc/examples/adder/src/adder/ftests/index_view.py
===================================================================
--- grok/trunk/doc/examples/adder/src/adder/ftests/index_view.py (rev 0)
+++ grok/trunk/doc/examples/adder/src/adder/ftests/index_view.py 2007-07-15 13:54:35 UTC (rev 78003)
@@ -0,0 +1,52 @@
+"""
+The Index view of the Adder app has a form for entering items to be added
+
+ >>> from adder.app import Adder
+ >>> getRootFolder()['adder'] = adder = Adder()
+
+ >>> from zope.testbrowser.testing import Browser
+ >>> browser = Browser()
+ >>> browser.open('http://localhost/adder')
+
+Since we've just started the Adder, there shouldn't be a "Clear" button
+
+ >>> browser.getControl('bt_clear')
+ Traceback (innermost last):
+ ...
+ LookupError: name 'bt_clear'
+
+There should be an entry field for the number to be added
+and a "+" button.
+
+ >>> browser.getControl('term').value = '1.234'
+ >>> browser.getControl('bt_add').value
+ '+'
+ >>> browser.getControl('bt_add').click()
+
+We should find the '1.234' value twice: one in the total, one in the "tape"
+
+ >>> browser.contents.count('1.234')
+ 2
+
+We can add another number:
+
+ >>> browser.getControl('term').value = '2'
+ >>> browser.getControl('bt_add').click()
+
+And see the total and the "tape" numbers:
+
+ >>> '3.234' in browser.contents
+ True
+ >>> '1.234' in browser.contents
+ True
+ >>> '2.0' in browser.contents
+ True
+
+Also, now that we have some numbers in the "tape", there should be a "Clear"
+button, which clears the tape and the total:
+
+ >>> browser.getControl('bt_clear').click()
+ >>> '3.234' in browser.contents
+ False
+
+"""
Added: grok/trunk/doc/examples/adder/src/adder/ftests/test_adder_functional.py
===================================================================
--- grok/trunk/doc/examples/adder/src/adder/ftests/test_adder_functional.py (rev 0)
+++ grok/trunk/doc/examples/adder/src/adder/ftests/test_adder_functional.py 2007-07-15 13:54:35 UTC (rev 78003)
@@ -0,0 +1,51 @@
+import unittest
+from pkg_resources import resource_listdir
+from zope.testing import doctest
+from zope.app.testing.functional import (HTTPCaller, getRootFolder,
+ FunctionalTestSetup, sync, Functional)
+
+# XXX bastardized from zope.app.testing.functional.FunctionalDocFileSuite :-(
+def FunctionalDocTestSuite(*paths, **kw):
+ globs = kw.setdefault('globs', {})
+ globs['http'] = HTTPCaller()
+ globs['getRootFolder'] = getRootFolder
+ globs['sync'] = sync
+
+ #kw['package'] = doctest._normalize_module(kw.get('package'))
+
+ kwsetUp = kw.get('setUp')
+ def setUp(test):
+ FunctionalTestSetup().setUp()
+
+ if kwsetUp is not None:
+ kwsetUp(test)
+ kw['setUp'] = setUp
+
+ kwtearDown = kw.get('tearDown')
+ def tearDown(test):
+ if kwtearDown is not None:
+ kwtearDown(test)
+ FunctionalTestSetup().tearDown()
+ kw['tearDown'] = tearDown
+
+ if 'optionflags' not in kw:
+ old = doctest.set_unittest_reportflags(0)
+ doctest.set_unittest_reportflags(old)
+ kw['optionflags'] = (old
+ | doctest.ELLIPSIS
+ | doctest.REPORT_NDIFF
+ | doctest.NORMALIZE_WHITESPACE)
+
+ suite = doctest.DocTestSuite(*paths, **kw)
+ suite.layer = Functional
+ return suite
+
+def test_suite():
+ suite = unittest.TestSuite()
+ from adder.ftests import index_view
+ test = FunctionalDocTestSuite(index_view)
+ suite.addTest(test)
+ return suite
+
+if __name__ == '__main__':
+ unittest.main(defaultTest='test_suite')
Added: grok/trunk/doc/examples/adder/src/adder/static/adder.css
===================================================================
--- grok/trunk/doc/examples/adder/src/adder/static/adder.css (rev 0)
+++ grok/trunk/doc/examples/adder/src/adder/static/adder.css 2007-07-15 13:54:35 UTC (rev 78003)
@@ -0,0 +1,22 @@
+body {
+ background-color: #FFE29A;
+ font-family: courier, fixed, monospace;
+}
+span {
+ font-size: 9pt;
+ color: #D00;
+}
+table {
+ font-size: 12pt;
+ text-align: right;
+}
+tr.False {
+ background-color: #FFEFC7;
+}
+tr.True {
+ background-color: white;
+}
+td.count {
+ font-size: 9pt;
+ color: #888;
+}
Added: grok/trunk/doc/examples/adder/src/adder/tests/__init__.py
===================================================================
Added: grok/trunk/doc/examples/adder/src/adder/tests/test_adder.py
===================================================================
--- grok/trunk/doc/examples/adder/src/adder/tests/test_adder.py (rev 0)
+++ grok/trunk/doc/examples/adder/src/adder/tests/test_adder.py 2007-07-15 13:54:35 UTC (rev 78003)
@@ -0,0 +1,20 @@
+import unittest
+from pkg_resources import resource_listdir
+from zope.testing import doctest, cleanup
+
+from adder import app
+
+def cleanUpZope(test):
+ cleanup.cleanUp()
+
+def test_suite():
+ suite = unittest.TestSuite()
+ test = doctest.DocTestSuite(app,
+ tearDown=cleanUpZope,
+ optionflags=doctest.ELLIPSIS+
+ doctest.NORMALIZE_WHITESPACE)
+ suite.addTest(test)
+ return suite
+
+if __name__ == '__main__':
+ unittest.main(defaultTest='test_suite')
Added: grok/trunk/doc/examples/adder/src/runtests.sh
===================================================================
--- grok/trunk/doc/examples/adder/src/runtests.sh (rev 0)
+++ grok/trunk/doc/examples/adder/src/runtests.sh 2007-07-15 13:54:35 UTC (rev 78003)
@@ -0,0 +1 @@
+ ./parts/instance/bin/test --path=/usr/local/Zope-3.3.1/lib/python --path=src `for i in eggs/*; do echo --path=$i; done` -s adder
\ No newline at end of file
Added: grok/trunk/doc/examples/animaltree/src/AnimalTree.egg-info/PKG-INFO
===================================================================
--- grok/trunk/doc/examples/animaltree/src/AnimalTree.egg-info/PKG-INFO (rev 0)
+++ grok/trunk/doc/examples/animaltree/src/AnimalTree.egg-info/PKG-INFO 2007-07-15 13:54:35 UTC (rev 78003)
@@ -0,0 +1,10 @@
+Metadata-Version: 1.0
+Name: AnimalTree
+Version: 0.0
+Summary: UNKNOWN
+Home-page: UNKNOWN
+Author: UNKNOWN
+Author-email: UNKNOWN
+License: UNKNOWN
+Description: UNKNOWN
+Platform: UNKNOWN
Added: grok/trunk/doc/examples/animaltree/src/AnimalTree.egg-info/SOURCES.txt
===================================================================
--- grok/trunk/doc/examples/animaltree/src/AnimalTree.egg-info/SOURCES.txt (rev 0)
+++ grok/trunk/doc/examples/animaltree/src/AnimalTree.egg-info/SOURCES.txt 2007-07-15 13:54:35 UTC (rev 78003)
@@ -0,0 +1,11 @@
+setup.py
+src/AnimalTree.egg-info/PKG-INFO
+src/AnimalTree.egg-info/SOURCES.txt
+src/AnimalTree.egg-info/dependency_links.txt
+src/AnimalTree.egg-info/entry_points.txt
+src/AnimalTree.egg-info/not-zip-safe
+src/AnimalTree.egg-info/paster_plugins.txt
+src/AnimalTree.egg-info/requires.txt
+src/AnimalTree.egg-info/top_level.txt
+src/animaltree/__init__.py
+src/animaltree/app.py
Added: grok/trunk/doc/examples/animaltree/src/AnimalTree.egg-info/dependency_links.txt
===================================================================
--- grok/trunk/doc/examples/animaltree/src/AnimalTree.egg-info/dependency_links.txt (rev 0)
+++ grok/trunk/doc/examples/animaltree/src/AnimalTree.egg-info/dependency_links.txt 2007-07-15 13:54:35 UTC (rev 78003)
@@ -0,0 +1 @@
+
Added: grok/trunk/doc/examples/animaltree/src/AnimalTree.egg-info/entry_points.txt
===================================================================
--- grok/trunk/doc/examples/animaltree/src/AnimalTree.egg-info/entry_points.txt (rev 0)
+++ grok/trunk/doc/examples/animaltree/src/AnimalTree.egg-info/entry_points.txt 2007-07-15 13:54:35 UTC (rev 78003)
@@ -0,0 +1,3 @@
+
+ # -*- Entry points: -*-
+
\ No newline at end of file
Added: grok/trunk/doc/examples/animaltree/src/AnimalTree.egg-info/not-zip-safe
===================================================================
--- grok/trunk/doc/examples/animaltree/src/AnimalTree.egg-info/not-zip-safe (rev 0)
+++ grok/trunk/doc/examples/animaltree/src/AnimalTree.egg-info/not-zip-safe 2007-07-15 13:54:35 UTC (rev 78003)
@@ -0,0 +1 @@
+
Added: grok/trunk/doc/examples/animaltree/src/AnimalTree.egg-info/paster_plugins.txt
===================================================================
--- grok/trunk/doc/examples/animaltree/src/AnimalTree.egg-info/paster_plugins.txt (rev 0)
+++ grok/trunk/doc/examples/animaltree/src/AnimalTree.egg-info/paster_plugins.txt 2007-07-15 13:54:35 UTC (rev 78003)
@@ -0,0 +1 @@
+PasteScript
Added: grok/trunk/doc/examples/animaltree/src/AnimalTree.egg-info/requires.txt
===================================================================
--- grok/trunk/doc/examples/animaltree/src/AnimalTree.egg-info/requires.txt (rev 0)
+++ grok/trunk/doc/examples/animaltree/src/AnimalTree.egg-info/requires.txt 2007-07-15 13:54:35 UTC (rev 78003)
@@ -0,0 +1,2 @@
+setuptools
+grok
\ No newline at end of file
Added: grok/trunk/doc/examples/animaltree/src/AnimalTree.egg-info/top_level.txt
===================================================================
--- grok/trunk/doc/examples/animaltree/src/AnimalTree.egg-info/top_level.txt (rev 0)
+++ grok/trunk/doc/examples/animaltree/src/AnimalTree.egg-info/top_level.txt 2007-07-15 13:54:35 UTC (rev 78003)
@@ -0,0 +1 @@
+animaltree
Added: grok/trunk/doc/examples/animaltree/src/animaltree/README.txt
===================================================================
--- grok/trunk/doc/examples/animaltree/src/animaltree/README.txt (rev 0)
+++ grok/trunk/doc/examples/animaltree/src/animaltree/README.txt 2007-07-15 13:54:35 UTC (rev 78003)
@@ -0,0 +1,8 @@
+This is a version of the classic app used to demonstrate a tree data structure.
+It is a hierarchical database of animals arranged by distinguishing questions.
+Initially, it knows only one animal. But as users interact with it, it learns
+more.
+
+This example has a grok.Container subclass called Node. Leaf nodes are empty and
+contain the name of one animal. Branch nodes contain a question and two
+sub-nodes named 'yes' and 'no'.
Added: grok/trunk/doc/examples/animaltree/src/animaltree/__init__.py
===================================================================
--- grok/trunk/doc/examples/animaltree/src/animaltree/__init__.py (rev 0)
+++ grok/trunk/doc/examples/animaltree/src/animaltree/__init__.py 2007-07-15 13:54:35 UTC (rev 78003)
@@ -0,0 +1 @@
+# this directory is a package
Added: grok/trunk/doc/examples/animaltree/src/animaltree/app.py
===================================================================
--- grok/trunk/doc/examples/animaltree/src/animaltree/app.py (rev 0)
+++ grok/trunk/doc/examples/animaltree/src/animaltree/app.py 2007-07-15 13:54:35 UTC (rev 78003)
@@ -0,0 +1,68 @@
+import grok
+
+class AnimalTree(grok.Application, grok.Container):
+ def __init__(self):
+ super(AnimalTree, self).__init__()
+ self['start'] = Node(u'mammoth')
+
+class Node(grok.Container):
+ def __init__(self, text):
+ super(Node, self).__init__()
+ self.text = text
+
+ def learn(self, new_animal, new_question, new_answer):
+ if new_answer == 'yes':
+ old_answer = 'no'
+ else:
+ old_answer = 'yes'
+ self[new_answer] = Node(new_animal.strip())
+ self[old_answer] = Node(self.text)
+ self.text = new_question.strip()
+
+ def isLeaf(self):
+ '''return whether there are subnodes'''
+ return self.has_key('yes')
+
+ def question(self):
+ if self.isLeaf():
+ return self.text # the text is the question
+ else: # otherwise the text is the name of the animal
+ if self.text[0].lower() in 'aeiou':
+ article = 'an'
+ else:
+ article = 'a'
+ return 'Is it %s %s?' % (article, self.text)
+
+class Index(grok.View):
+ grok.context(AnimalTree)
+
+class Guess(grok.View):
+ grok.context(Node)
+
+ def yes_url(self):
+ if self.context.isLeaf():
+ return self.url('yes/guess')
+ else:
+ return self.url('correct')
+
+ def no_url(self):
+ if self.context.isLeaf():
+ return self.url('no/guess')
+ else:
+ return self.url('learn')
+
+class Correct(grok.View):
+ grok.context(Node)
+
+class Learn(grok.View):
+ grok.context(Node)
+
+ def update(self, new_animal=None, new_question=None):
+ if new_animal:
+ if self.request.has_key('bt_yes'):
+ new_answer = 'no'
+ else:
+ new_answer = 'yes'
+ self.context.learn(new_animal, new_question, new_answer)
+ self.redirect(self.application_url())
+
\ No newline at end of file
Added: grok/trunk/doc/examples/animaltree/src/animaltree/app_templates/correct.pt
===================================================================
--- grok/trunk/doc/examples/animaltree/src/animaltree/app_templates/correct.pt (rev 0)
+++ grok/trunk/doc/examples/animaltree/src/animaltree/app_templates/correct.pt 2007-07-15 13:54:35 UTC (rev 78003)
@@ -0,0 +1,14 @@
+<html>
+<head>
+</head>
+<body>
+ <h2>Grok!</h2>
+
+ <p>Once more the PaleoBase has proven correct.</p>
+
+ <p>If you want to test it again, think of another animal then click start.</p>
+
+ <!-- XXX: is there a better way of building a url to a certain view of another model? -->
+ <a tal:attributes="href python:view.application_url()+'/start/guess'">Start</a>
+
+</body>
Added: grok/trunk/doc/examples/animaltree/src/animaltree/app_templates/guess.pt
===================================================================
--- grok/trunk/doc/examples/animaltree/src/animaltree/app_templates/guess.pt (rev 0)
+++ grok/trunk/doc/examples/animaltree/src/animaltree/app_templates/guess.pt 2007-07-15 13:54:35 UTC (rev 78003)
@@ -0,0 +1,10 @@
+<html>
+<head>
+</head>
+<body>
+ <h2 tal:content="context/question" />
+
+ <p><a tal:attributes="href view/yes_url">Yes</a></p>
+ <p><a tal:attributes="href view/no_url">No</a></p>
+
+</body>
\ No newline at end of file
Added: grok/trunk/doc/examples/animaltree/src/animaltree/app_templates/index.pt
===================================================================
--- grok/trunk/doc/examples/animaltree/src/animaltree/app_templates/index.pt (rev 0)
+++ grok/trunk/doc/examples/animaltree/src/animaltree/app_templates/index.pt 2007-07-15 13:54:35 UTC (rev 78003)
@@ -0,0 +1,17 @@
+<html>
+<head>
+</head>
+<body>
+ <h1>Animal Tree</h1>
+
+ <p>Think of an animal.</p>
+
+ <p>Based on a paleolithic knowledge base, this system will
+ guess the animal you are thinking about.</p>
+
+ <p>Click start when you have a specific animal in mind.</p>
+
+ <!-- XXX: is there a better way of building a url to a certain view of another model? -->
+ <a tal:attributes="href python:view.application_url()+'/start/guess'">Start</a>
+
+</body>
Added: grok/trunk/doc/examples/animaltree/src/animaltree/app_templates/learn.pt
===================================================================
--- grok/trunk/doc/examples/animaltree/src/animaltree/app_templates/learn.pt (rev 0)
+++ grok/trunk/doc/examples/animaltree/src/animaltree/app_templates/learn.pt 2007-07-15 13:54:35 UTC (rev 78003)
@@ -0,0 +1,30 @@
+<html>
+<head>
+</head>
+<body>
+ <h2>Time to learn a new animal</h2>
+
+ <form tal:attributes="action view/url">
+
+ <p>Which animal did you have in mind?</p>
+ <input type="text" name="new_animal" />
+
+ <p>What question would distinguish that from a
+ <strong tal:content="context/text" />?
+ </p>
+ <input type="text" name="new_question" size="70" />
+
+ <p>And the answer for a
+ <strong tal:content="context/text" /> would be?
+ </p>
+ <p><input type="submit" name="bt_yes" value="Yes" />
+ <input type="submit" name="bt_no" value="No" />
+ <span> </span>
+ <em>I am not sure. Let's
+ <!-- XXX: is there a better way of building a url to a certain view of another model? -->
+ <a tal:attributes="href python:view.application_url()+'/start/guess'">restart</a>.</em>
+ </p>
+
+ </form>
+
+</body>
\ No newline at end of file
Added: grok/trunk/doc/examples/animaltree/src/animaltree/configure.zcml
===================================================================
--- grok/trunk/doc/examples/animaltree/src/animaltree/configure.zcml (rev 0)
+++ grok/trunk/doc/examples/animaltree/src/animaltree/configure.zcml 2007-07-15 13:54:35 UTC (rev 78003)
@@ -0,0 +1 @@
+<grok package="." xmlns="http://namespaces.zope.org/grok" />
More information about the Checkins
mailing list