Skip to content

Commit 54ebbd4

Browse files
authored
Add support for using dot (.) to get subobjects from the OD (#426)
1 parent 3c1d86c commit 54ebbd4

File tree

4 files changed

+23
-2
lines changed

4 files changed

+23
-2
lines changed

canopen/objectdictionary/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ def __getitem__(
105105
"""Get object from object dictionary by name or index."""
106106
item = self.names.get(index) or self.indices.get(index)
107107
if item is None:
108+
if isinstance(index, str) and '.' in index:
109+
idx, sub = index.split('.', maxsplit=1)
110+
return self[idx][sub]
108111
name = "0x%X" % index if isinstance(index, int) else index
109112
raise KeyError("%s was not found in Object Dictionary" % name)
110113
return item

doc/od.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ You can access the objects using either index/subindex or names::
4848

4949
device_name_obj = node.object_dictionary['ManufacturerDeviceName']
5050
vendor_id_obj = node.object_dictionary[0x1018][1]
51-
51+
actual_speed = node.object_dictionary['ApplicationStatus.ActualSpeed']
52+
command_all = node.object_dictionary['ApplicationCommands.CommandAll']
5253

5354
API
5455
---

doc/sdo.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,14 @@ Examples
3030
--------
3131

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

3638
# Complex records
3739
command_all = node.sdo['ApplicationCommands']['CommandAll']
40+
command_all = node.sdo['ApplicationCommands.CommandAll']
3841
actual_speed = node.sdo['ApplicationStatus']['ActualSpeed']
3942
control_mode = node.sdo['ApplicationSetupParameters']['RequestedControlMode']
4043

test/test_od.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,20 @@ def test_add_array(self):
136136
self.assertEqual(test_od["Test Array"], array)
137137
self.assertEqual(test_od[0x1002], array)
138138

139+
def test_get_item_dot(self):
140+
test_od = od.ObjectDictionary()
141+
array = od.ODArray("Test Array", 0x1000)
142+
last_subindex = od.ODVariable("Last subindex", 0x1000, 0)
143+
last_subindex.data_type = od.UNSIGNED8
144+
member1 = od.ODVariable("Test Variable", 0x1000, 1)
145+
member2 = od.ODVariable("Test Variable 2", 0x1000, 2)
146+
array.add_member(last_subindex)
147+
array.add_member(member1)
148+
array.add_member(member2)
149+
test_od.add_object(array)
150+
self.assertEqual(test_od["Test Array.Last subindex"], last_subindex)
151+
self.assertEqual(test_od["Test Array.Test Variable"], member1)
152+
self.assertEqual(test_od["Test Array.Test Variable 2"], member2)
139153

140154
class TestArray(unittest.TestCase):
141155

0 commit comments

Comments
 (0)