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 all commits
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:
idx, sub = index.split('.', maxsplit=1)
return self[idx][sub]
name = "0x%X" % index if isinstance(index, int) else index
raise KeyError("%s was not found in Object Dictionary" % name)
return item
Expand Down
3 changes: 2 additions & 1 deletion doc/od.rst
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ You can access the objects using either index/subindex or names::

device_name_obj = node.object_dictionary['ManufacturerDeviceName']
vendor_id_obj = node.object_dictionary[0x1018][1]

actual_speed = node.object_dictionary['ApplicationStatus.ActualSpeed']
command_all = node.object_dictionary['ApplicationCommands.CommandAll']

API
---
Expand Down
5 changes: 4 additions & 1 deletion doc/sdo.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,14 @@ Examples
--------

SDO objects can be accessed using the ``.sdo`` member which works like a Python
dictionary. Indexes and subindexes can be identified by either name or number.
dictionary. Indexes can be identified by either name or number.
There are two ways to idenity subindexes, either by using the index and subindex
as separate arguments or by using a combined syntax using a dot.
The code below only creates objects, no messages are sent or received yet::

# Complex records
command_all = node.sdo['ApplicationCommands']['CommandAll']
command_all = node.sdo['ApplicationCommands.CommandAll']
actual_speed = node.sdo['ApplicationStatus']['ActualSpeed']
control_mode = node.sdo['ApplicationSetupParameters']['RequestedControlMode']

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