Skip to content

Commit 19f79c4

Browse files
authored
Refactor Graph.filter to use list comprehension (#54)
1 parent c7cd3f6 commit 19f79c4

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

cog/torque.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -421,9 +421,7 @@ def filter(self, func):
421421
'''
422422
Applies a filter function to the vertices and removes any vertices that do not pass the filter.
423423
'''
424-
for v in self.last_visited_vertices:
425-
if not func(v.id):
426-
self.last_visited_vertices.remove(v)
424+
self.last_visited_vertices = [v for v in self.last_visited_vertices if func(v.id)]
427425
return self
428426

429427
def sim(self, word, operator, threshold, strict=False):

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33

44
setup(name='cogdb',
5-
version='3.0.8',
5+
version='3.0.9',
66
description='Persistent Embedded Graph Database',
77
url='http://github.com/arun1729/cog',
88
author='Arun Mahendra',

test/test_torque6.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,15 @@ def test_filter_int(self):
9797
self.assertTrue(expected == actual)
9898
g.close()
9999

100+
def test_filter_multiple_non_matches(self):
101+
g = Graph(graph_name="test6", cog_home=DIR_NAME)
102+
traversal = g.v(["greg", "tom", "alice"])
103+
returned = traversal.filter(func=lambda x: x == 'alice')
104+
self.assertIs(returned, traversal)
105+
expected = {'result': [{'id': 'alice'}]}
106+
self.assertTrue(expected == returned.all())
107+
g.close()
108+
100109
@classmethod
101110
def tearDownClass(cls):
102111
shutil.rmtree("/tmp/" + DIR_NAME)

0 commit comments

Comments
 (0)