Skip to content

Commit fcf8a11

Browse files
committed
new: add find_by_key to Assembly
1 parent 1950955 commit fcf8a11

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

src/compas/datastructures/assembly/assembly.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,3 +201,22 @@ def find(self, guid):
201201

202202
return self.graph.node_attribute(key, "part")
203203

204+
def find_by_key(self, key):
205+
"""Find a part in the assembly by its key.
206+
207+
Parameters
208+
----------
209+
key : int | str, optional
210+
The identifier of the part in the assembly.
211+
212+
Returns
213+
-------
214+
:class:`~compas.datastructures.Part` | None
215+
The identified part,
216+
or None if the part can't be found.
217+
218+
"""
219+
if key not in self.graph.node:
220+
return None
221+
222+
return self.graph.node_attribute(key, "part")

tests/compas/datastructures/test_assembly.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,16 @@ def test_find():
4141

4242
assembly.add_part(part)
4343
assert assembly.find(part.guid) == part
44+
45+
46+
def test_find_by_key():
47+
assembly = Assembly()
48+
part = Part()
49+
assembly.add_part(part, key=2)
50+
assert assembly.find_by_key(2) == part
51+
52+
part = Part()
53+
assembly.add_part(part, key="6")
54+
assert assembly.find_by_key("6") == part
55+
56+
assert assembly.find_by_key("100") is None

0 commit comments

Comments
 (0)