sartography / cr-connect

0 stars 0 forks source link

TypeError:'NoneType' object is not subscriptable #704

Closed calexh-sar closed 2 years ago

calexh-sar commented 2 years ago

When running this code in a Script Task in my Sandbox workflow

which_workflow_spec_id = 'irb_pre_review_submission'

study_logs = get_logs_for_study(level='metrics', code='end_workflow')
latest_record = None
latest_timestamp = None
format = "%Y-%m-%dT%H:%M:%S.%f%z"

for log in study_logs:
    if log['workflow_spec_id'] == which_workflow_spec_id:
        timestamp = datetime.datetime.strptime(log['timestamp'], format)
        if latest_timestamp:
            if max(timestamp, latest_timestamp) == timestamp:
                latest_timestamp = timestamp
                latest_record = log
        else:
            latest_timestamp = timestamp
            latest_record = log

last_instance_timestamp = latest_record["timestamp"]
last_instance_date_time = get_localtime(timestamp=last_instance_timestamp)
last_instance_date_str = last_instance_date_time.strftime('%m-%d-%Y')
last_instance_time_str = last_instance_date_time.strftime('%I:%M %p')

delete_variables('timestamp')
delete_variables('latest_timestamp')
delete_variables('format')
delete_variables('last_instance_timestamp')
del(log)
del(study_logs)

I receive this error:

image.png

The code runs without error in the app.

cullerton commented 2 years ago

@calexh-sar It's possible for latest_record to be None. You should probably test to make sure it isn't None before running this code:

last_instance_timestamp = latest_record["timestamp"] last_instance_date_time = get_localtime(timestamp=last_instance_timestamp) last_instance_date_str = last_instance_date_time.strftime('%m-%d-%Y') last_instance_time_str = last_instance_date_time.strftime('%I:%M %p')

calexh-sar commented 2 years ago

Closing based on Mike's comment