File tree 4 files changed +23
-2
lines changed
4 files changed +23
-2
lines changed Original file line number Diff line number Diff line change @@ -105,6 +105,9 @@ def __getitem__(
105
105
"""Get object from object dictionary by name or index."""
106
106
item = self .names .get (index ) or self .indices .get (index )
107
107
if item is None :
108
+ if isinstance (index , str ) and '.' in index :
109
+ idx , sub = index .split ('.' , maxsplit = 1 )
110
+ return self [idx ][sub ]
108
111
name = "0x%X" % index if isinstance (index , int ) else index
109
112
raise KeyError ("%s was not found in Object Dictionary" % name )
110
113
return item
Original file line number Diff line number Diff line change @@ -48,7 +48,8 @@ You can access the objects using either index/subindex or names::
48
48
49
49
device_name_obj = node.object_dictionary['ManufacturerDeviceName']
50
50
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']
52
53
53
54
API
54
55
---
Original file line number Diff line number Diff line change @@ -30,11 +30,14 @@ Examples
30
30
--------
31
31
32
32
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.
34
36
The code below only creates objects, no messages are sent or received yet::
35
37
36
38
# Complex records
37
39
command_all = node.sdo['ApplicationCommands']['CommandAll']
40
+ command_all = node.sdo['ApplicationCommands.CommandAll']
38
41
actual_speed = node.sdo['ApplicationStatus']['ActualSpeed']
39
42
control_mode = node.sdo['ApplicationSetupParameters']['RequestedControlMode']
40
43
Original file line number Diff line number Diff line change @@ -136,6 +136,20 @@ def test_add_array(self):
136
136
self .assertEqual (test_od ["Test Array" ], array )
137
137
self .assertEqual (test_od [0x1002 ], array )
138
138
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 )
139
153
140
154
class TestArray (unittest .TestCase ):
141
155
You can’t perform that action at this time.
0 commit comments