Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for using dot (.) to get subobjects from the OD #426

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions canopen/objectdictionary/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ def __getitem__(
"""Get object from object dictionary by name or index."""
item = self.names.get(index) or self.indices.get(index)
if item is None:
if isinstance(index, str) and '.' in index:
parts = index.split('.')
return self[parts[0]][".".join(parts[1:])]
sveinse marked this conversation as resolved.
Show resolved Hide resolved
name = "0x%X" % index if isinstance(index, int) else index
raise KeyError("%s was not found in Object Dictionary" % name)
return item
Expand Down
14 changes: 14 additions & 0 deletions test/test_od.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,20 @@ def test_add_array(self):
self.assertEqual(test_od["Test Array"], array)
self.assertEqual(test_od[0x1002], array)

def test_get_item_dot(self):
test_od = od.ObjectDictionary()
array = od.ODArray("Test Array", 0x1000)
last_subindex = od.ODVariable("Last subindex", 0x1000, 0)
last_subindex.data_type = od.UNSIGNED8
member1 = od.ODVariable("Test Variable", 0x1000, 1)
member2 = od.ODVariable("Test Variable 2", 0x1000, 2)
array.add_member(last_subindex)
array.add_member(member1)
array.add_member(member2)
test_od.add_object(array)
self.assertEqual(test_od["Test Array.Last subindex"], last_subindex)
self.assertEqual(test_od["Test Array.Test Variable"], member1)
self.assertEqual(test_od["Test Array.Test Variable 2"], member2)

class TestArray(unittest.TestCase):

Expand Down
Loading