2
\$\begingroup\$

I am new to python and I am trying to write a function with various actions based on the parameters sent. I am checking if the data has valid format then it publishes to /accepted otherwise to /rejected. If the data is fine then it does the function and then publishes to /success otherwise to /failure

Here is what i am doing now -

def data_handler(event, context):
 try:
 input_topic = context.topic
 input_message = event
 response = f'Invoked on topic {input_topic} with message {input_message}'
 logging.info(response)
 client.publish(topic=input_topic + '/accepted', payload=json.dumps({'state': 'accepted'}))
 except Exception as e:
 logging.error(e)
 client.publish(topic=input_topic + '/rejected', payload=json.dumps({'state': 'rejected'}))
 try:
 print('Perform the function based on input message')
 client.publish(topic=input_topic + '/success', payload=json.dumps({'state': 'Execution succeeded'}))
 except Exception as e:
 logging.error(e)
 client.publish(topic=input_topic + '/failure', payload=json.dumps({'state': 'Execution failed'}))

This works as of now but i think there should be a better way to implement this. Any idea?

asked May 25, 2021 at 17:18
\$\endgroup\$
2
  • 1
    \$\begingroup\$ It would be nice to see this used in a __main__ context where example input is given. There are a lot of questions around this, like "What is client?" If your entire program isn't proprietary code, it would be best to just post the whole thing. \$\endgroup\$ Commented May 25, 2021 at 17:27
  • \$\begingroup\$ This is a big project for which this a small module i need to write. This is an AWS lambda function which is event triggered. The event parameter brings in the JSON payload. The client is the aws iot client which can publish to the specific topics based on MQTT \$\endgroup\$ Commented May 25, 2021 at 17:33

1 Answer 1

1
\$\begingroup\$

Given that there isn't much here to analyse purely based on performance since we don't know what those other function calls do, here are some minor changes:

def data_handler(message, topic):
 try:
 logging.debug(f'Invoked on topic {topic} with message {message}')
 client.publish(topic=topic + '/pass', payload=json.dumps({'state': 'pass'}))
 except Exception as e:
 logging.error(e)
 client.publish(topic=topic + '/fail', payload=json.dumps({'state': 'fail'}))

Both try/catch blocks in your code look identical, and you should have only one endpoint that needs to be communicated with. If you really need both, then there should be a separate function to have this code.

It also seems pretty redundant that you're publishing to a pass and fail endpoint and have a payload that says whether or not it passed or failed. You should know that status based on the endpoint that receives the message.

You can have the payload be more descriptive as well to remove multiple endpoints. Something like {'formatted': true, 'error': ''} is far more informative.

Lastly, you can just pass your one-off variables into this function, like so: data_handler(event, context.topic). If your event really is just a post message and not a posting event object, then that should be clarified somewhere in the code base.

answered May 25, 2021 at 17:47
\$\endgroup\$
1
  • \$\begingroup\$ The different topics are used in order to convey different situations - /accepted - to publish a message when the payload is correctly formatted and can be parsed /rejected - incorrect formatting and cannot be parsed If the publish on accepted is successful then try to publish to the /success topic to convey that the task was correctly performed (such as some OS configuration) If the publish on /success is somehow not successful then publish on /failure. Seems like a separate function for the success / failure would work. \$\endgroup\$ Commented May 25, 2021 at 21:56

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.