Skip to content

Commit 2a18643

Browse files
committed
address review
1 parent 92b8b88 commit 2a18643

File tree

2 files changed

+35
-7
lines changed

2 files changed

+35
-7
lines changed

redisgraph/execution_plan.py

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
class Operation:
2+
"""
3+
Operation, single operation of execution plan.
4+
"""
25
def __init__(self, name, args=None):
6+
"""
7+
Create a new operation.
8+
9+
Args:
10+
name: string that represents the name of the operation
11+
args: coperation arguments
12+
"""
313
self.name = name
414
self.args = args
515
self.children = []
@@ -26,23 +36,41 @@ def __eq__(self, o: object) -> bool:
2636

2737
def __str__(self) -> str:
2838
args_str = "" if self.args is None else f" | {self.args}"
29-
if len(self.children) == 0:
30-
return f"{self.name}{args_str}"
31-
else:
32-
children = "\n".join([" " + line for op in self.children for line in str(op).splitlines()])
33-
return f"{self.name}{args_str}\n{children}"
39+
return f"{self.name}{args_str}"
3440

3541

3642
class ExecutionPlan:
43+
"""
44+
ExecutionPlan, collection of operations.
45+
"""
3746
def __init__(self, plan):
47+
"""
48+
Create a new execution plan.
49+
50+
Args:
51+
plan: array of strings that represents the collection operations
52+
"""
3853
if not isinstance(plan, list):
3954
raise Exception("plan must be array")
4055

4156
self.plan = plan
4257
self.structured_plan = self._operation_tree()
4358

59+
def _operation_traverse(self, op, op_f, aggregate_f, combine_f):
60+
child_res = op_f(op)
61+
if len(op.children) == 0:
62+
return child_res
63+
else:
64+
children = [self._operation_traverse(op, op_f, aggregate_f, combine_f) for op in op.children]
65+
return combine_f(child_res, aggregate_f(children))
66+
4467
def __str__(self) -> str:
45-
return "\n".join(self.plan)
68+
def aggraget_str(str_ops):
69+
return "\n".join([" " + line for str_op in str_ops for line in str_op.splitlines()])
70+
71+
def combine_str(x, y):
72+
return f"{x}\n{y}"
73+
return self._operation_traverse(self.structured_plan, str, aggraget_str, combine_str)
4674

4775
def _operation_tree(self):
4876
i = 0

tests/functional/test_all.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ def test_execution_plan(self):
271271
Conditional Traverse | (t:Team)->(r:Rider)
272272
Filter
273273
Node By Label Scan | (t:Team)'''
274-
self.assertEquals(str(result.structured_plan), expected)
274+
self.assertEquals(str(result), expected)
275275
self.assertEqual(str(result), expected)
276276

277277
expected = Operation('Results') \

0 commit comments

Comments
 (0)