Skip to content

Commit 50b0265

Browse files
committed
yes
Signed-off-by: Max Chesterfield <max.chesterfield@zepben.com>
1 parent b136555 commit 50b0265

4 files changed

Lines changed: 10 additions & 7 deletions

File tree

changelog.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@
9292
* None.
9393

9494
### Enhancements
95-
* None.
95+
* `EquipmentTreeBuilder` now has a `leaves` attribute which is a dict of all mrid -> IdentifiedObject contained in the tree
96+
* `TreeNode` uses a set to store `_children` instead of a set, massive performance gains.
9697

9798
### Fixes
9899
* Marked some extensions properties and classes with [ZBEX] that were missing them (might still be more). In addition to the ones moved into the extensions

src/zepben/ewb/services/network/tracing/networktrace/actions/equipment_tree_builder.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class EquipmentTreeBuilder(StepActionWithContextValue):
3535
"""
3636

3737
_roots: dict[ConductingEquipment, EquipmentTreeNode] = {}
38+
leaves: dict[str, EquipmentTreeNode] = {}
3839

3940
def __init__(self):
4041
super().__init__(key=str(uuid.uuid4()))
@@ -66,6 +67,7 @@ def _apply(self, item: NetworkTraceStep[Any], context: StepContext):
6667
current_node: TreeNode = self.get_context_value(context)
6768
if current_node.parent:
6869
current_node.parent.add_child(current_node)
70+
self.leaves[current_node.identified_object.mrid] = current_node
6971

7072
def clear(self):
7173
self._roots.clear()

src/zepben/ewb/services/network/tracing/networktrace/actions/tree_node.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
__all__ = ['TreeNode']
77

8-
from typing import List, TypeVar, Generic
8+
from typing import List, TypeVar, Generic, Set
99

1010
from zepben.ewb import IdentifiedObject
1111

@@ -20,7 +20,7 @@ class TreeNode(Generic[T]):
2020
def __init__(self, identified_object: IdentifiedObject, parent=None):
2121
self.identified_object = identified_object
2222
self._parent: TreeNode = parent
23-
self._children: List[TreeNode] = []
23+
self._children: Set[TreeNode] = set()
2424

2525
@property
2626
def parent(self) -> 'TreeNode[T]':
@@ -31,7 +31,7 @@ def children(self):
3131
return list(self._children)
3232

3333
def add_child(self, child: 'TreeNode'):
34-
self._children.append(child)
34+
self._children.add(child)
3535

3636
def __str__(self):
3737
return f"{{object: {self.identified_object}, parent: {self.parent or ''}, num children: {len(self.children)}}}"

test/services/network/tracing/networktrace/actions/test_equipment_tree_builder.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ async def test_downstream_tree():
5454
root = list(tree_builder.roots)[0]
5555

5656
assert root is not None
57-
_verify_tree_asset(root, n["j1"], None, [n["ac1"], n["ac3"]])
57+
_verify_tree_asset(root, n["j1"], None, [n["ac3"], n["ac1"]])
5858

59-
test_node = root.children[0]
59+
test_node = root.children[1]
6060
_verify_tree_asset(test_node, n["ac1"], n["j1"], [n["j2"]])
6161

6262
test_node = test_node.children[0]
@@ -74,7 +74,7 @@ async def test_downstream_tree():
7474
test_node = next(iter(test_node.children))
7575
_verify_tree_asset(test_node, n["j6"], n["ac4"], [])
7676

77-
test_node = list(root.children)[1]
77+
test_node = list(root.children)[0]
7878
_verify_tree_asset(test_node, n["ac3"], n["j1"], [n["j4"]])
7979

8080
test_node = next(iter(test_node.children))

0 commit comments

Comments
 (0)