Skip to content

Commit

Permalink
- wrap calls to map/filter/range/etc in list calls where needed
Browse files Browse the repository at this point in the history
  • Loading branch information
dataflake committed May 13, 2017
1 parent d0f1c29 commit cc96a5a
Show file tree
Hide file tree
Showing 12 changed files with 51 additions and 53 deletions.
28 changes: 14 additions & 14 deletions src/Products/PluginIndexes/BooleanIndex/tests.py
Expand Up @@ -108,26 +108,26 @@ def test_index_many_true(self):
for i in range(0, 100):
obj = Dummy(i, i < 80 and True or False)
index._index_object(obj.id, obj, attr='truth')
self.assertEqual(list(index._index), range(80, 100))
self.assertEqual(list(index._index), list(range(80, 100)))
self.assertEqual(len(index._unindex), 100)

res, idx = index._apply_index({'truth': True})
self.assertEqual(list(res), range(0, 80))
self.assertEqual(list(res), list(range(0, 80)))
res, idx = index._apply_index({'truth': False})
self.assertEqual(list(res), range(80, 100))
self.assertEqual(list(res), list(range(80, 100)))

def test_index_many_false(self):
index = self._makeOne()
for i in range(0, 100):
obj = Dummy(i, i >= 80 and True or False)
index._index_object(obj.id, obj, attr='truth')
self.assertEqual(list(index._index), range(80, 100))
self.assertEqual(list(index._index), list(range(80, 100)))
self.assertEqual(len(index._unindex), 100)

res, idx = index._apply_index({'truth': False})
self.assertEqual(list(res), range(0, 80))
self.assertEqual(list(res), list(range(0, 80)))
res, idx = index._apply_index({'truth': True})
self.assertEqual(list(res), range(80, 100))
self.assertEqual(list(res), list(range(80, 100)))

def test_index_many_change(self):
index = self._makeOne()
Expand All @@ -144,26 +144,26 @@ def add(i, value):
# Now add an equal number of False values
for i in range(4, 8):
add(i, False)
self.assertEqual(list(index._index), range(4, 8))
self.assertEqual(list(index._index), list(range(4, 8)))
self.assertEqual(len(index._unindex), 8)
# Once False gets to be more than 60% of the indexed set, we switch
add(8, False)
self.assertEqual(list(index._index), range(4, 9))
self.assertEqual(list(index._index), list(range(4, 9)))
add(9, False)
self.assertEqual(list(index._index), range(0, 4))
self.assertEqual(list(index._index), list(range(0, 4)))
res, idx = index._apply_index({'truth': True})
self.assertEqual(list(res), range(0, 4))
self.assertEqual(list(res), list(range(0, 4)))
res, idx = index._apply_index({'truth': False})
self.assertEqual(list(res), range(4, 10))
self.assertEqual(list(res), list(range(4, 10)))
# and we can again switch if the percentages change again
for i in range(6, 10):
index.unindex_object(i)
self.assertEqual(list(index._index), range(4, 6))
self.assertEqual(list(index._index), list(range(4, 6)))
self.assertEqual(len(index._unindex), 6)
res, idx = index._apply_index({'truth': True})
self.assertEqual(list(res), range(0, 4))
self.assertEqual(list(res), list(range(0, 4)))
res, idx = index._apply_index({'truth': False})
self.assertEqual(list(res), range(4, 6))
self.assertEqual(list(res), list(range(4, 6)))

def test_items(self):
index = self._makeOne()
Expand Down
Expand Up @@ -20,8 +20,8 @@
states = ['published', 'pending', 'private', 'intranet']
types = ['Document', 'News', 'File', 'Image']
default_pages = [True, False, False, False, False, False]
subjects = map(lambda x: 'subject_%s' % x, range(6))
keywords = map(lambda x: 'keyword_%s' % x, range(6))
subjects = list(map(lambda x: 'subject_%s' % x, range(6)))
keywords = list(map(lambda x: 'keyword_%s' % x, range(6)))


class TestObject(object):
Expand Down
4 changes: 2 additions & 2 deletions src/Products/PluginIndexes/PathIndex/PathIndex.py
Expand Up @@ -116,7 +116,7 @@ def index_object(self, docid, obj, threshold=100):
if isinstance(path, (list, tuple)):
path = '/' + '/'.join(path[1:])

comps = filter(None, path.split('/'))
comps = list(filter(None, path.split('/')))

old_value = self._unindex.get(docid, None)
if old_value == path:
Expand Down Expand Up @@ -280,7 +280,7 @@ def _search(self, path, default_level=0):
[self._search(path, level)
for level in range(self._depth + 1)])

comps = filter(None, path.split('/'))
comps = list(filter(None, path.split('/')))

if level + len(comps) - 1 > self._depth:
# Our search is for a path longer than anything in the index
Expand Down
4 changes: 2 additions & 2 deletions src/Products/PluginIndexes/PathIndex/tests.py
Expand Up @@ -280,14 +280,14 @@ def test___apply_index_root_levelO_dict(self):
_populateIndex(index)
query = {'path': {'query': '/', 'level': 0}}
res = index._apply_index(query)
self.assertEqual(list(res[0].keys()), range(1, 19))
self.assertEqual(list(res[0].keys()), list(range(1, 19)))

def test___apply_index_root_levelO_tuple(self):
index = self._makeOne()
_populateIndex(index)
query = {'path': (('/', 0),)}
res = index._apply_index(query)
self.assertEqual(list(res[0].keys()), range(1, 19))
self.assertEqual(list(res[0].keys()), list(range(1, 19)))

def test__apply_index_simple(self):
index = self._makeOne()
Expand Down
2 changes: 1 addition & 1 deletion src/Products/PluginIndexes/unindex.py
Expand Up @@ -466,7 +466,7 @@ def query_index(self, record, resultset=None):
record.keys = [k for k in index.keys() if k not in not_parm]
else:
# convert query arguments into indexed format
record.keys = map(self._convert, record.keys)
record.keys = list(map(self._convert, record.keys))

# Range parameter
range_parm = record.get('range', None)
Expand Down
2 changes: 1 addition & 1 deletion src/Products/ZCTextIndex/BaseIndex.py
Expand Up @@ -219,7 +219,7 @@ def search_phrase(self, phrase):
return result

def _remove_oov_wids(self, wids):
return filter(self._wordinfo.has_key, wids)
return list(filter(self._wordinfo.has_key, wids))

# Subclass must override.
# The workhorse. Return a list of (IIBucket, weight) pairs, one pair
Expand Down
2 changes: 1 addition & 1 deletion src/Products/ZCTextIndex/Lexicon.py
Expand Up @@ -71,7 +71,7 @@ def sourceToWordIds(self, text):
last = _text2list(text)
for element in self._pipeline:
last = element.process(last)
return map(self._getWordIdCreate, last)
return list(map(self._getWordIdCreate, last))

def termToWordIds(self, text):
last = _text2list(text)
Expand Down
2 changes: 1 addition & 1 deletion src/Products/ZCTextIndex/NBest.py
Expand Up @@ -68,7 +68,7 @@ def addmany(self, sequence):
assert n == len(scores)

def getbest(self):
result = zip(self._items, self._scores)
result = list(zip(self._items, self._scores))
result.reverse()
return result

Expand Down
6 changes: 2 additions & 4 deletions src/Products/ZCTextIndex/PipelineFactory.py
Expand Up @@ -34,13 +34,11 @@ def registerFactory(self, group, name, factory):
elements[name] = factory

def getFactoryGroups(self):
groups = self._groups.keys()
groups.sort()
groups = sorted(self._groups.keys())
return groups

def getFactoryNames(self, group):
names = self._groups[group].keys()
names.sort()
names = sorted(self._groups[group].keys())
return names

def instantiate(self, group, name):
Expand Down
4 changes: 2 additions & 2 deletions src/Products/ZCTextIndex/QueryParser.py
Expand Up @@ -193,7 +193,7 @@ def _parseOrExpr(self):
L.append(self._parseAndExpr())
while self._check(_OR):
L.append(self._parseAndExpr())
L = filter(None, L)
L = list(filter(None, L))
if not L:
return None # Only stopwords
elif len(L) == 1:
Expand Down Expand Up @@ -241,7 +241,7 @@ def _parseTerm(self):
nodes = [self._parseAtom()]
while self._peek(_ATOM):
nodes.append(self._parseAtom())
nodes = filter(None, nodes)
nodes = list(filter(None, nodes))
if not nodes:
return None # Only stopwords
structure = [(isinstance(nodes[i], ParseTree.NotNode), i, nodes[i])
Expand Down
8 changes: 4 additions & 4 deletions src/Products/ZCTextIndex/tests/testZCTextIndex.py
Expand Up @@ -480,27 +480,27 @@ def _checkRelativeScores(self):
self.assertEqual(num, 9)
self.assertEqual(len(r), 9)
# The more twos in a doc, the better the score should be.
self.assertEqual([doc for doc, score in r], range(9, 0, -1))
self.assertEqual([doc for doc, score in r], list(range(9, 0, -1)))

# Search for "two" alone shouldn't make any difference to relative
# results.
r, num = self.zc_index.query("two")
self.assertEqual(num, 9)
self.assertEqual(len(r), 9)
self.assertEqual([doc for doc, score in r], range(9, 0, -1))
self.assertEqual([doc for doc, score in r], list(range(9, 0, -1)))

# Searching for xyz should skip doc 9, and favor the lower-numbered
# docs (they have more instances of xyz).
r, num = self.zc_index.query("xyz")
self.assertEqual(num, 8)
self.assertEqual(len(r), 8)
self.assertEqual([doc for doc, score in r], range(1, 9))
self.assertEqual([doc for doc, score in r], list(range(1, 9)))

# And relative results shouldn't change if we add "one".
r, num = self.zc_index.query("xyz one")
self.assertEqual(num, 8)
self.assertEqual(len(r), 8)
self.assertEqual([doc for doc, score in r], range(1, 9))
self.assertEqual([doc for doc, score in r], list(range(1, 9)))

# But if we search for all the words, it's much muddier. The boost
# in going from i instances to i+1 of a given word is smaller than
Expand Down
38 changes: 19 additions & 19 deletions src/Products/ZCatalog/tests/test_catalog.py
Expand Up @@ -203,7 +203,7 @@ class TestCatalog(unittest.TestCase):

upper = 10

nums = range(upper)
nums = list(range(upper))
for i in range(upper):
j = random.randrange(0, upper)
tmp = nums[i]
Expand Down Expand Up @@ -355,7 +355,7 @@ class TestCatalogSortBatch(unittest.TestCase):

upper = 100

nums = range(upper)
nums = list(range(upper))
for i in range(upper):
j = random.randrange(0, upper)
tmp = nums[i]
Expand Down Expand Up @@ -427,7 +427,7 @@ def test_sortResults(self):
rs = IISet([b.getRID() for b in brains])
si = catalog.getIndex('num')
result = catalog.sortResults(rs, si)
self.assertEqual([r.num for r in result], range(100))
self.assertEqual([r.num for r in result], list(range(100)))

def test_sortResults_reversed(self):
catalog = self._make_one()
Expand All @@ -445,7 +445,7 @@ def test_sortResults_limit(self):
result = catalog.sortResults(rs, si, limit=10)
self.assertEqual(len(result), 10)
self.assertEqual(result.actual_result_count, 100)
self.assertEqual([r.num for r in result], range(10))
self.assertEqual([r.num for r in result], list(range(10)))

def test_sortResults_limit_reversed(self):
catalog = self._make_one()
Expand All @@ -469,7 +469,7 @@ def testLargeSortedResultSetWithSmallIndex(self):

def testSortLimit(self):
catalog = self._make_one()
full = catalog(att1='att1', sort_on='num')
full = list(catalog(att1='att1', sort_on='num'))
a = catalog(att1='att1', sort_on='num', sort_limit=10)
self.assertEqual([r.num for r in a], [r.num for r in full[:10]])
self.assertEqual(a.actual_result_count, self.upper)
Expand All @@ -496,102 +496,102 @@ def testSortLimitViaBatchingArgsBeforeStart(self):
query = dict(att1='att1', sort_on='num', b_start=-5, b_size=8)
result = catalog(query)
self.assertEqual(result.actual_result_count, 100)
self.assertEqual([r.num for r in result], range(0, 3))
self.assertEqual([r.num for r in result], list(range(0, 3)))

def testSortLimitViaBatchingArgsStart(self):
catalog = self._make_one()
query = dict(att1='att1', sort_on='num', b_start=0, b_size=5)
result = catalog(query)
self.assertEqual(result.actual_result_count, 100)
self.assertEqual([r.num for r in result], range(0, 5))
self.assertEqual([r.num for r in result], list(range(0, 5)))

def testSortLimitViaBatchingEarlyFirstHalf(self):
catalog = self._make_one()
query = dict(att1='att1', sort_on='num', b_start=11, b_size=17)
result = catalog(query)
self.assertEqual(result.actual_result_count, 100)
self.assertEqual([r.num for r in result], range(11, 28))
self.assertEqual([r.num for r in result], list(range(11, 28)))

def testSortLimitViaBatchingArgsLateFirstHalf(self):
catalog = self._make_one()
query = dict(att1='att1', sort_on='num', b_start=30, b_size=15)
result = catalog(query)
self.assertEqual(result.actual_result_count, 100)
self.assertEqual([r.num for r in result], range(30, 45))
self.assertEqual([r.num for r in result], list(range(30, 45)))

def testSortLimitViaBatchingArgsLeftMiddle(self):
catalog = self._make_one()
query = dict(att1='att1', sort_on='num', b_start=45, b_size=8)
result = catalog(query)
self.assertEqual(result.actual_result_count, 100)
self.assertEqual([r.num for r in result], range(45, 53))
self.assertEqual([r.num for r in result], list(range(45, 53)))

def testSortLimitViaBatchingArgsRightMiddle(self):
catalog = self._make_one()
query = dict(att1='att1', sort_on='num', b_start=48, b_size=8)
result = catalog(query)
self.assertEqual(result.actual_result_count, 100)
self.assertEqual([r.num for r in result], range(48, 56))
self.assertEqual([r.num for r in result], list(range(48, 56)))

def testSortLimitViaBatchingArgsRightMiddleSortOnTwoSecond(self):
catalog = self._make_one()
query = dict(att1='att1', sort_on=('att1', 'num'),
sort_order=('', 'reverse'), b_start=48, b_size=8)
result = catalog(query)
self.assertEqual(result.actual_result_count, 100)
self.assertEqual([r.num for r in result], range(51, 43, -1))
self.assertEqual([r.num for r in result], list(range(51, 43, -1)))

def testSortLimitViaBatchingArgsEarlySecondHalf(self):
catalog = self._make_one()
query = dict(att1='att1', sort_on='num', b_start=55, b_size=15)
result = catalog(query)
self.assertEqual(result.actual_result_count, 100)
self.assertEqual([r.num for r in result], range(55, 70))
self.assertEqual([r.num for r in result], list(range(55, 70)))

def testSortLimitViaBatchingArgsEarlySecondHalfSortOnTwoFirst(self):
catalog = self._make_one()
query = dict(att1='att1', sort_on=('att1', 'num'),
sort_order=('reverse', ''), b_start=55, b_size=15)
result = catalog(query)
self.assertEqual(result.actual_result_count, 100)
self.assertEqual([r.num for r in result], range(55, 70))
self.assertEqual([r.num for r in result], list(range(55, 70)))

def testSortLimitViaBatchingArgsEarlySecondHalfSortOnTwoSecond(self):
catalog = self._make_one()
query = dict(att1='att1', sort_on=('att1', 'num'),
sort_order=('', 'reverse'), b_start=55, b_size=15)
result = catalog(query)
self.assertEqual(result.actual_result_count, 100)
self.assertEqual([r.num for r in result], range(44, 29, -1))
self.assertEqual([r.num for r in result], list(range(44, 29, -1)))

def testSortLimitViaBatchingArgsEarlySecondHalfSortOnTwoBoth(self):
catalog = self._make_one()
query = dict(att1='att1', sort_on=('att1', 'num'),
sort_order=('reverse', 'reverse'), b_start=55, b_size=15)
result = catalog(query)
self.assertEqual(result.actual_result_count, 100)
self.assertEqual([r.num for r in result], range(44, 29, -1))
self.assertEqual([r.num for r in result], list(range(44, 29, -1)))

def testSortLimitViaBatchingArgsSecondHalf(self):
catalog = self._make_one()
query = dict(att1='att1', sort_on='num', b_start=70, b_size=15)
result = catalog(query)
self.assertEqual(result.actual_result_count, 100)
self.assertEqual([r.num for r in result], range(70, 85))
self.assertEqual([r.num for r in result], list(range(70, 85)))

def testSortLimitViaBatchingArgsEnd(self):
catalog = self._make_one()
query = dict(att1='att1', sort_on='num', b_start=90, b_size=10)
result = catalog(query)
self.assertEqual(result.actual_result_count, 100)
self.assertEqual([r.num for r in result], range(90, 100))
self.assertEqual([r.num for r in result], list(range(90, 100)))

def testSortLimitViaBatchingArgsOverEnd(self):
catalog = self._make_one()
query = dict(att1='att1', sort_on='num', b_start=90, b_size=15)
result = catalog(query)
self.assertEqual(result.actual_result_count, 100)
self.assertEqual([r.num for r in result], range(90, 100))
self.assertEqual([r.num for r in result], list(range(90, 100)))

def testSortLimitViaBatchingArgsOutside(self):
catalog = self._make_one()
Expand Down

0 comments on commit cc96a5a

Please sign in to comment.