Search code examples
pythonrequesttry-except

How to reduce to just one try,except statements


I have a dictionary that contains multiple try-except statements. I tried to retrieve the values with r.get(), but it throws me an Type Error with NoneObject. I know the default argument for .get() is None, but it doesn't work. I have multiple lists where I append at each iteration data from different dictionary values.

How can I reduce the code to a single try-except statement? Thank you!

This is my code:

for num in issue_number:
    print(num)
    Response = requests.get(f'https://example.com/rest/api/2/issue/Proj-{num}?expand=changelog&maxResults =1000', auth=(example))
    r = Response.json()
    try:
        task_list.append(r['key'])
    except TypeError:
        task_list.append('NA')
    try:
        summary_list.append(r['fields']['summary'])
    except TypeError:
        summary_list.append('NA')
    try:
        assignee_list.append(r['fields']['assignee']['displayName'])
    except TypeError:
        assignee_list.append('NA')
    try:
        created_list.append(r['fields']['created'])
    except:
        created_list.append('NA')
    try:
        status_list.append(r['fields']['status']['name'])
    except:
        status_list.append('NA')
    try:
        due_date_list.append(r['fields']['duedate'])
    except:
        due_date_list.append('NA')
    try:
        resolution_list.append(r['fields']['resolution']['name'])
    except:
        resolution_list.append('NA')
    try:
        resolution_date_list.append(r['fields']['resolutiondate'])
    except:
        resolution_date_list.append('NA')

Solution

  • You can avoid all that by chaining multiple get calls:

    for num in issue_number:
        print(num)
        Response = requests.get(f'https://example.com/rest/api/2/issue/Proj-{num}?expand=changelog&maxResults =1000', auth=(example))
        r = Response.json()
        task_list.append(r.get('key', 'NA'))
        summary_list.append(r.get('fields', {}).get('summary', 'NA'))
        assignee_list.append(r.get('fields', {}).get('assignee', {}).get('displayName','NA'))
        created_list.append(r.get('fields', {}).get('created', 'NA'))
        status_list.append(r.get('fields', {}).get('status', {}).get('name', 'NA'))
        due_date_list.append(r.get('fields', {}).get('duedate', 'NA'))
        resolution_list.append(r.get('fields', {}).get('resolution', {}).get('name', 'NA'))
        resolution_date_list.append(r.get('fields', {}).get('resolutiondate', 'NA'))
    

    However, since you are doing the same a number of times, you may find more convenient using a function for that, for example something like this:

    def get_deep(d, *keys, default=None):
        for k in keys:
            if not d or k not in d:
                # Stop early if a key is not found
                return default
            d = d[k]
        return d if d is not None else default
    
    # ...
    
    for num in issue_number:
        print(num)
        Response = requests.get(f'https://example.com/rest/api/2/issue/Proj-{num}?expand=changelog&maxResults =1000', auth=(example))
        r = Response.json()
        task_list.append(get_deep(r, 'key', default='NA'))
        summary_list.append(get_deep(r, 'fields', 'summary', default='NA'))
        assignee_list.append(get_deep(r, 'fields', 'assignee', 'displayName', default='NA'))
        created_list.append(get_deep(r, 'fields', 'created', default='NA'))
        status_list.append(get_deep(r, 'fields', 'status', 'name', default='NA'))
        due_date_list.append(get_deep(r, 'fields', 'duedate', default='NA'))
        resolution_list.append(get_deep(r, 'fields', 'resolution', 'name', default='NA'))
        resolution_date_list.append(get_deep(r, 'fields', 'resolutiondate', default='NA'))