[Zope3-checkins] CVS: Zope3/src/zope/tales - interfaces.py:1.2 tales.py:1.10
Steve Alexander
steve@cat-box.net
Wed, 11 Jun 2003 05:12:40 -0400
Update of /cvs-repository/Zope3/src/zope/tales
In directory cvs.zope.org:/tmp/cvs-serv17680/src/zope/tales
Modified Files:
interfaces.py tales.py
Log Message:
Added an index method to TALES iterators, in line with the TALES spec.
Also, documented the undefined behaviour of many methods if you call them
before the first iteration.
Also, made item() raise a more appropriate error than AttributeError if
you use it before the first iteration.
=== Zope3/src/zope/tales/interfaces.py 1.1 => 1.2 ===
--- Zope3/src/zope/tales/interfaces.py:1.1 Tue May 20 16:29:38 2003
+++ Zope3/src/zope/tales/interfaces.py Wed Jun 11 05:12:39 2003
@@ -32,8 +32,15 @@
For example, with a TAL statement like: tal:repeat="item items",
an iterator will be assigned to "repeat/item". The iterator
provides a number of handy methods useful in writing TAL loops.
+
+ The results are undefined of calling any of the methods except
+ 'length' before the first iteration.
"""
+ def index():
+ """Return the position (starting with "0") within the iteration
+ """
+
def number():
"""Return the position (starting with "1") within the iteration
"""
@@ -46,6 +53,14 @@
"""Return whether the current position is odd
"""
+ def start():
+ """Return whether the current position is the first position
+ """
+
+ def end():
+ """Return whether the current position is the last position
+ """
+
def letter():
"""Return the position (starting with "a") within the iteration
"""
@@ -62,16 +77,8 @@
"""Return the position (starting with "I") within the iteration
"""
- def start():
- """Return whether the current position is the first position
- """
-
- def end():
- """Return whether the current position is the last position
- """
-
def item():
- """Return whether the item at the current position
+ """Return the item at the current position
"""
def length():
@@ -80,4 +87,3 @@
Note that this may fail if the TAL iterator was created on a Python
iterator.
"""
-
=== Zope3/src/zope/tales/tales.py 1.9 => 1.10 ===
--- Zope3/src/zope/tales/tales.py:1.9 Tue Jun 3 11:20:06 2003
+++ Zope3/src/zope/tales/tales.py Wed Jun 11 05:12:39 2003
@@ -164,6 +164,29 @@
self._setLocal(self._name, v)
return 1
+ def index(self):
+ """Get the iterator index
+
+ >>> context = Context(ExpressionEngine(), {})
+ >>> it = Iterator('foo', ("apple", "pear", "orange"), context)
+ >>> int(bool(it.next()))
+ 1
+ >>> it.index()
+ 0
+ >>> int(bool(it.next()))
+ 1
+ >>> it.index()
+ 1
+ >>> int(bool(it.next()))
+ 1
+ >>> it.index()
+ 2
+ """
+ index = self._nextIndex - 1
+ if index < 0:
+ raise TypeError("No iteration position")
+ return index
+
def number(self):
"""Get the iterator position
@@ -401,6 +424,8 @@
1
"""
+ if self._nextIndex == 0:
+ raise TypeError("No iteration position")
return self._item
def length(self):