Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Z
Zope
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
Zope
Commits
31674e78
Commit
31674e78
authored
Oct 09, 2002
by
Evan Simpson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Close Collector #385, #577 and simplify ZTUtils.Iterator implementation.
parent
a62b6382
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
46 additions
and
45 deletions
+46
-45
doc/CHANGES.txt
doc/CHANGES.txt
+4
-0
lib/python/ZTUtils/Iterator.py
lib/python/ZTUtils/Iterator.py
+11
-42
lib/python/ZTUtils/tests/testIterator.py
lib/python/ZTUtils/tests/testIterator.py
+31
-3
No files found.
doc/CHANGES.txt
View file @
31674e78
...
...
@@ -8,6 +8,10 @@ Zope Changes
Features Added
- ZTUtils.Iterator now handles any object that has a Python
iterator, resolving Collector #385 and #577. The
implementation is somwhat simpler, as well.
- ZTUtils.Tree.TreeMaker now has additional methods for setting
various flags and attributes that influence how the tree is built,
making these aspects accessible to PythonScripts.
...
...
lib/python/ZTUtils/Iterator.py
View file @
31674e78
...
...
@@ -18,8 +18,8 @@ The Iterator() function accepts either a sequence or a Python
iterator. The next() method fetches the next item, and returns
true if it succeeds.
$Id: Iterator.py,v 1.
8 2002/09/26 17:29:45
evan Exp $'''
__version__
=
'$Revision: 1.
8
$'
[
11
:
-
2
]
$Id: Iterator.py,v 1.
9 2002/10/09 17:33:49
evan Exp $'''
__version__
=
'$Revision: 1.
9
$'
[
11
:
-
2
]
class
Iterator
:
'''Simple Iterator class'''
...
...
@@ -29,11 +29,10 @@ class Iterator:
nextIndex
=
0
def
__init__
(
self
,
seq
):
self
.
seq
=
seq
for
inner
in
seqInner
,
iterInner
:
if
inner
.
_supports
(
seq
):
self
.
_inner
=
inner
self
.
_prep_next
=
inner
.
prep_next
return
if
iterInner
.
_supports
(
seq
,
self
):
self
.
_inner
=
iterInner
self
.
_prep_next
=
iterInner
.
prep_next
return
raise
TypeError
,
"Iterator does not support %s"
%
`seq`
def
__getattr__
(
self
,
name
):
...
...
@@ -130,48 +129,19 @@ class InnerBase:
return
0
return
not
self
.
prep_next
(
it
)
class
SeqInner
(
InnerBase
):
'''Inner class for sequence Iterators'''
def
_supports
(
self
,
ob
):
try
:
ob
[
0
]
except
(
TypeError
,
AttributeError
):
return
0
except
:
pass
return
1
def
prep_next
(
self
,
it
):
i
=
it
.
nextIndex
try
:
it
.
_next
=
it
.
seq
[
i
]
except
IndexError
:
it
.
_prep_next
=
self
.
no_next
it
.
end
=
1
return
0
it
.
end
=
0
return
1
def
it_length
(
self
,
it
):
it
.
length
=
l
=
len
(
it
.
seq
)
return
l
try
:
StopIteration
=
StopIteration
except
NameError
:
StopIteration
=
"StopIteration"
class
IterInner
(
InnerBase
):
'''Iterator inner class for Python iterators'''
'''Iterator inner class for
objects with
Python iterators'''
def
_supports
(
self
,
ob
):
def
_supports
(
self
,
ob
,
it
):
try
:
i
f
hasattr
(
ob
,
'next'
)
and
(
ob
is
iter
(
ob
)):
return
1
i
t
.
iter
=
iter
(
ob
)
return
1
except
:
return
0
def
prep_next
(
self
,
it
):
try
:
it
.
_next
=
it
.
seq
.
next
()
it
.
_next
=
it
.
iter
.
next
()
except
StopIteration
:
it
.
_prep_next
=
self
.
no_next
it
.
end
=
1
...
...
@@ -192,5 +162,4 @@ class IterIter:
return
it
.
item
raise
StopIteration
seqInner
=
SeqInner
()
iterInner
=
IterInner
()
lib/python/ZTUtils/tests/testIterator.py
View file @
31674e78
from
__future__
import
generators
import
os
,
sys
,
unittest
from
ZTUtils
import
Iterator
...
...
@@ -8,6 +9,26 @@ try:
except
NameError
:
do_piter_test
=
0
class
itemIterator
:
'Ignore the __getitem__ argument in order to catch non-monotonic access.'
def
__init__
(
self
,
n
):
self
.
n
=
n
self
.
i
=
0
def
__getitem__
(
self
,
i
):
if
self
.
i
>=
self
.
n
:
raise
IndexError
i
=
self
.
i
self
.
i
=
self
.
i
+
1
return
i
class
genIterator
:
'Generator-based iteration'
def
__init__
(
self
,
n
):
self
.
n
=
n
def
__iter__
(
self
):
for
i
in
range
(
self
.
n
):
yield
i
class
IteratorTests
(
unittest
.
TestCase
):
def
testIterator0
(
self
):
...
...
@@ -48,14 +69,21 @@ class IteratorTests(unittest.TestCase):
assert
not
it
.
end
,
(
"End false on element %s of %s"
%
(
el
,
size
))
def
testIndex
(
self
):
it
=
Iterator
(
range
(
5
)
)
for
el
in
range
(
5
):
def
assertRangeMatch
(
self
,
ob
,
n
):
it
=
Iterator
(
ob
)
for
el
in
range
(
n
):
assert
it
.
next
(),
"Iterator stopped too soon"
assert
it
.
index
==
el
,
"Incorrect index"
assert
it
.
number
()
==
el
+
1
,
"Incorrect number"
assert
it
.
item
==
el
,
"Incorrect item"
def
testIndex
(
self
):
self
.
assertRangeMatch
(
range
(
5
),
5
)
self
.
assertRangeMatch
((
0
,
1
,
2
,
3
,
4
),
5
)
self
.
assertRangeMatch
({
0
:
0
,
1
:
1
,
2
:
2
,
3
:
3
,
4
:
4
},
5
)
self
.
assertRangeMatch
(
itemIterator
(
5
),
5
)
self
.
assertRangeMatch
(
genIterator
(
5
),
5
)
def
testFirstLast
(
self
):
it
=
Iterator
([
1
])
it
.
next
()
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment