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
410714c3
Commit
410714c3
authored
Dec 14, 2010
by
Andreas Zeidler
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
back-port c118863 from 2.13
parent
6a88eb97
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
17 additions
and
20 deletions
+17
-20
doc/CHANGES.rst
doc/CHANGES.rst
+1
-0
src/Products/ZCatalog/Lazy.py
src/Products/ZCatalog/Lazy.py
+6
-20
src/Products/ZCatalog/tests/testLazySequences.py
src/Products/ZCatalog/tests/testLazySequences.py
+10
-0
No files found.
doc/CHANGES.rst
View file @
410714c3
...
...
@@ -11,6 +11,7 @@ http://docs.zope.org/zope2/releases/.
Bugs Fixed
++++++++++
- Fix `LazyMap` to avoid unnecessary function calls.
2.12.14 (2010-12-07)
--------------------
...
...
src/Products/ZCatalog/Lazy.py
View file @
410714c3
...
...
@@ -134,35 +134,21 @@ class LazyMap(Lazy):
def
__init__
(
self
,
func
,
seq
,
length
=
None
):
self
.
_seq
=
seq
self
.
_data
=
[]
self
.
_func
=
func
if
length
is
not
None
:
self
.
_len
=
length
else
:
self
.
_len
=
len
(
seq
)
self
.
_marker
=
object
()
self
.
_data
=
[
self
.
_marker
]
*
self
.
_len
def
__getitem__
(
self
,
index
):
data
=
self
.
_data
try
:
s
=
self
.
_seq
except
AttributeError
:
return
data
[
index
]
i
=
index
if
i
<
0
:
i
=
len
(
self
)
+
i
if
i
<
0
:
raise
IndexError
,
index
ind
=
len
(
data
)
if
i
<
ind
:
return
data
[
i
]
ind
=
ind
-
1
func
=
self
.
_func
while
i
>
ind
:
try
:
ind
=
ind
+
1
data
.
append
(
func
(
s
[
ind
]))
except
IndexError
:
del
self
.
_func
del
self
.
_seq
raise
IndexError
,
index
return
data
[
i
]
value
=
data
[
index
]
if
value
is
self
.
_marker
:
value
=
data
[
index
]
=
self
.
_func
(
s
[
index
])
return
value
class
LazyFilter
(
Lazy
):
# Act like a sequence, but get data from a filtering process.
...
...
src/Products/ZCatalog/tests/testLazySequences.py
View file @
410714c3
...
...
@@ -98,6 +98,16 @@ class TestLazyMap(TestLazyCat):
lmap
=
self
.
_createLMap
(
filter
,
seq1
,
seq2
,
seq3
)
self
.
_compare
(
lmap
,
[
str
(
x
).
lower
()
for
x
in
(
seq1
+
seq2
+
seq3
)])
def
testMapFuncIsOnlyCalledAsNecessary
(
self
):
seq
=
range
(
10
)
count
=
[
0
]
# closure only works with list, and `nonlocal` in py3
def
func
(
x
):
count
[
0
]
+=
1
return
x
lmap
=
self
.
_createLMap
(
func
,
seq
)
self
.
assertEqual
(
lmap
[
5
],
5
)
self
.
assertEqual
(
count
[
0
],
1
)
class
TestLazyFilter
(
TestLazyCat
):
def
_createLSeq
(
self
,
*
seq
):
...
...
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