Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Unwanted automatic type conversion

The following code

"""
Gets the error cause from a pipeline execution status.

:param execution_status: is the pipeline execution status.
:type execution_status: dictionary.
:returns: the pipeline execution error cause and the name of the step.
:rtype: dictionary.
"""
def get_error_cause(execution_status):
  error_cause = "Unknown"
  error_step = "Unknown"
  steps = execution_status['Steps']
  if steps:
    for step in steps:
      if 'FailureReason' in step:
        print(str(type(step['Name'])))
        error_step = step['Name'],
        print(str(type(error_step)))
        error_cause = step['FailureReason']
        break
  else:
    error_cause = execution_status['FailureReason']
  return {
    'Step': error_step,
    'Cause': error_cause
  }

when executed in AWS Lambda environment, is automatically converting a str into a tuple. The outputs for the two "print" methods that appear in the code are the following:

<class ‘str’>

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<class ‘tuple’>

This is causing that when serializing the dictionary returned by the method as a JSON, instead of serializing the value of the ‘Step’ key as a string, it is serialized as a list:

{
  "Step": ["ConcatenateColumns"],
  "Cause": "AccessDenied"
}

The input of the method is a JSON deserialized into a dictionary, like this:

{
    "ExecutionID": "some_execution_ID",
    "Status": "Failed",
    "ElapsedMinutes": 13.0,
    "Steps": [{
        "Name": "ConcatenateColumns",
        "Status": "Failed",
        "ElapsedMinutes": 13.0,
        "FailureReason": "AccessDenied"
    }],
    "FailureReason": "Step failure: One or multiple steps failed."
}

>Solution :

It’s not automatic type conversion, you are defining error_step as a tuple.
Remove the comma at the end of the line:

error_step = step['Name']

See python docs: https://docs.python.org/3/tutorial/datastructures.html#tuples-and-sequences

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading