0

I am making a list of string to function dictionary table basically so that I can invoke the function based on the string formed. For example , in this example - I am trying to form the string do_opCommand_start and then invoke the corresponding value of that key i.e. do_opCommand_start. I

import json


class TestAdapter:

    def __init__(self):
        self.testMessage = '{ "header": {"messageType": "opCommand",  "message": "start"}, "data": {"Ready": "sd"} }'
        
        self.testMethod = { 'opCommand_start': do_opCommand_start, 'opCommand_start': do_opCommand_stop }     
     
    def do_opCommand_start(self, data):
        print('opCommand Start invoked')
        print(data)
        
        
    def do_opCommand_stop(self, data):
        print('do_opCommand_stop invoked')
        print(data)
        
    def process_sb_message(self):
        msgDict = json.loads(self.testMessage)
        func_name = msgDict['header']['messageType'] + '_' + msgDict['header']['message']
        testMethod[func_name](msgDict['data'])
        
  
  
    
obj = TestAdapter()
obj.process_sb_message()

Here is the output.

Traceback (most recent call last):
  File "<string>", line 28, in <module>
  File "<string>", line 9, in __init__
NameError: name 'do_opCommand_start' is not defined
> 

0 Answers0