Other Objects¶
-
class
zope.tales.tales.
Iterator
(name, seq, context)[source]¶ Bases:
object
TALES Iterator.
Default implementation of
zope.tales.interfaces.ITALESIterator
.Construct an iterator
Iterators are defined for a name, a sequence, or an iterator and a context, where a context simply has a setLocal method:
>>> context = Context(ExpressionEngine(), {}) >>> it = Iterator('foo', ("apple", "pear", "orange"), context)
A local variable is not set until the iterator is used:
>>> int("foo" in context.vars) 0
We can create an iterator on an empty sequence:
>>> it = Iterator('foo', (), context)
An iterator works as well:
>>> it = Iterator('foo', {"apple":1, "pear":1, "orange":1}, context) >>> it.next() True
>>> it = Iterator('foo', {}, context) >>> it.next() False
>>> it = Iterator('foo', iter((1, 2, 3)), context) >>> it.next() True >>> it.next() True
-
next
()¶ Advance the iterator, if possible.
>>> context = Context(ExpressionEngine(), {}) >>> it = Iterator('foo', ("apple", "pear", "orange"), context) >>> bool(it.next()) True >>> context.vars['foo'] 'apple' >>> bool(it.next()) True >>> context.vars['foo'] 'pear' >>> bool(it.next()) True >>> context.vars['foo'] 'orange' >>> bool(it.next()) False
>>> it = Iterator('foo', {"apple":1, "pear":1, "orange":1}, context) >>> bool(it.next()) True >>> bool(it.next()) True >>> bool(it.next()) True >>> bool(it.next()) False
>>> it = Iterator('foo', (), context) >>> bool(it.next()) False
>>> it = Iterator('foo', {}, context) >>> bool(it.next()) False
If we can advance, set a local variable to the new value.
-
index
()[source]¶ Get the iterator index
>>> context = Context(ExpressionEngine(), {}) >>> it = Iterator('foo', ("apple", "pear", "orange"), context) >>> it.index() Traceback (most recent call last): ... TypeError: No iteration position >>> int(bool(it.next())) 1 >>> it.index() 0 >>> int(bool(it.next())) 1 >>> it.index() 1 >>> int(bool(it.next())) 1 >>> it.index() 2
-
number
()[source]¶ Get the iterator position
>>> context = Context(ExpressionEngine(), {}) >>> it = Iterator('foo', ("apple", "pear", "orange"), context) >>> int(bool(it.next())) 1 >>> it.number() 1 >>> int(bool(it.next())) 1 >>> it.number() 2 >>> int(bool(it.next())) 1 >>> it.number() 3
-
even
()[source]¶ Test whether the position is even
>>> context = Context(ExpressionEngine(), {}) >>> it = Iterator('foo', ("apple", "pear", "orange"), context) >>> it.next() True >>> it.even() True >>> it.next() True >>> it.even() False >>> it.next() True >>> it.even() True
-
odd
()[source]¶ Test whether the position is odd
>>> context = Context(ExpressionEngine(), {}) >>> it = Iterator('foo', ("apple", "pear", "orange"), context) >>> it.next() True >>> it.odd() False >>> it.next() True >>> it.odd() True >>> it.next() True >>> it.odd() False
-
parity
()[source]¶ Return ‘odd’ or ‘even’ depending on the position’s parity
>>> context = Context(ExpressionEngine(), {}) >>> it = Iterator('foo', ("apple", "pear", "orange"), context) >>> it.next() True >>> it.parity() 'odd' >>> it.next() True >>> it.parity() 'even' >>> it.next() True >>> it.parity() 'odd'
-
letter
(base=97, radix=26)[source]¶ Get the iterator position as a lower-case letter
>>> context = Context(ExpressionEngine(), {}) >>> it = Iterator('foo', ("apple", "pear", "orange"), context) >>> it.letter() Traceback (most recent call last): ... TypeError: No iteration position >>> it.next() True >>> it.letter() 'a' >>> it.next() True >>> it.letter() 'b' >>> it.next() True >>> it.letter() 'c'
-
Letter
()[source]¶ Get the iterator position as an upper-case letter
>>> context = Context(ExpressionEngine(), {}) >>> it = Iterator('foo', ("apple", "pear", "orange"), context) >>> it.next() True >>> it.Letter() 'A' >>> it.next() True >>> it.Letter() 'B' >>> it.next() True >>> it.Letter() 'C'
-
Roman
(rnvalues=((1000, 'M'), (900, 'CM'), (500, 'D'), (400, 'CD'), (100, 'C'), (90, 'XC'), (50, 'L'), (40, 'XL'), (10, 'X'), (9, 'IX'), (5, 'V'), (4, 'IV'), (1, 'I')))[source]¶ Get the iterator position as an upper-case roman numeral
>>> context = Context(ExpressionEngine(), {}) >>> it = Iterator('foo', ("apple", "pear", "orange"), context) >>> it.next() True >>> it.Roman() 'I' >>> it.next() True >>> it.Roman() 'II' >>> it.next() True >>> it.Roman() 'III'
-
roman
()[source]¶ Get the iterator position as a lower-case roman numeral
>>> context = Context(ExpressionEngine(), {}) >>> it = Iterator('foo', ("apple", "pear", "orange"), context) >>> it.next() True >>> it.roman() 'i' >>> it.next() True >>> it.roman() 'ii' >>> it.next() True >>> it.roman() 'iii'
-
start
()[source]¶ Test whether the position is the first position
>>> context = Context(ExpressionEngine(), {}) >>> it = Iterator('foo', ("apple", "pear", "orange"), context) >>> it.next() True >>> it.start() True >>> it.next() True >>> it.start() False >>> it.next() True >>> it.start() False
>>> it = Iterator('foo', {}, context) >>> it.start() False >>> it.next() False >>> it.start() False
-
end
()[source]¶ Test whether the position is the last position
>>> context = Context(ExpressionEngine(), {}) >>> it = Iterator('foo', ("apple", "pear", "orange"), context) >>> it.next() True >>> it.end() False >>> it.next() True >>> it.end() False >>> it.next() True >>> it.end() True
>>> it = Iterator('foo', {}, context) >>> it.end() False >>> it.next() False >>> it.end() False
-
item
()[source]¶ Get the iterator value
>>> context = Context(ExpressionEngine(), {}) >>> it = Iterator('foo', ("apple", "pear", "orange"), context) >>> it.item() Traceback (most recent call last): ... TypeError: No iteration position >>> it.next() True >>> it.item() 'apple' >>> it.next() True >>> it.item() 'pear' >>> it.next() True >>> it.item() 'orange'
>>> it = Iterator('foo', {1:2}, context) >>> it.next() True >>> it.item() 1
-
length
()[source]¶ Get the length of the iterator sequence
>>> context = Context(ExpressionEngine(), {}) >>> it = Iterator('foo', ("apple", "pear", "orange"), context) >>> it.length() 3
You can even get the length of a mapping:
>>> it = Iterator('foo', {"apple":1, "pear":2, "orange":3}, context) >>> it.length() 3
But you can’t get the length of an iterable which doesn’t support len():
>>> class MyIter(object): ... def __init__(self, seq): ... self._iter = iter(seq) ... def __iter__(self): ... return self ... def __next__(self): ... return next(self._iter) ... next = __next__ >>> it = Iterator('foo', MyIter({"apple":1, "pear":2}), context) >>> try: ... it.length() ... except TypeError: ... pass ... else: ... print('Expected TypeError')
-