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')
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'))
I would tend to refactor this sort of code into something like:
from collections import namedtuple
Issue = namedtuple('Issue', [
'task_key', 'summary', 'assignee', 'created'
])
import logging
logger = logging.getLogger(__name__)
def get_issue(num):
res = requests.get(f'https://example.com/rest/api/2/issue/Proj-{num}?expand=changelog&maxResults=1000', auth=(example))
res.raise_for_status()
r = res.json()
return Issue(
r['key'], r['fields']['summary'],
r['fields']['assignee']['displayName'],
r['fields']['created'],
)
issues = {}
for num in issue_number:
try:
issues[num] = get_issue(num)
except:
logger.exception("unable to process issue %s", num)
if you really want to get partial entries, then using get_deep
from @jdehesa might be useful
You could store your keys as tuples in some list and then check if these key pairs exist in r
, if so add them to your status_list
.
necessary_keys = [('key'), ('fields', 'summary'), ('fields', 'assignee', 'displayName'),
('fields', 'created'), ('fields', 'status', 'name'),
('fields', 'duedate'), ('fields', 'resolution', 'name'),
('fields', 'resolutiondate')]
def check_keys(dict_, keys):
if keys[0] in dict_:
if len(keys) == 1:
return dict_[keys[0]]
else:
return check_keys(dict_[keys[0]], keys[1:])
return 'NA'
issue_number = [0]
status_list = []
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()
r = {'fields': {'summary': 'test1', 'status': {'name': 'test2'}}}
for key in necessary_keys:
status_list.append(check_keys(r, key))
print(status_list)
Output:
['NA', 'test1', 'NA', 'NA', 'test2', 'NA', 'NA', 'NA']
You can use a dictionary to avoid a variable number of variables. In addition, you can use functools.reduce
to iterate a nested dictionary. Here's a complete example:
from collections import defaultdict
from functools import reduce
from operator import getitem
def getitem_from_dict(dataDict, mapList):
"""Iterate nested dictionary"""
return reduce(getitem, mapList, dataDict)
d = {1: {'key': 'key1', 'fields': {'summary': 'summary1'}},
2: {'key': 'key2', 'fields': {'summary': 'summary2'}},
3: {'key': 'key3', 'fields': {'summary': 'summary3'}}}
res = defaultdict(list)
lsts = ['task', 'summary']
keys = [['key'], ['fields', 'summary']]
for num in [1, 2, 3]:
r = d[num]
for lst, key in zip(lsts, keys):
try:
res[lst].append(getitem_from_dict(r, key))
except TypeError:
res[lst].append('NA')
print(res['task'])
# ['key1', 'key2', 'key3']
print(res['summary'])
# ['summary1', 'summary2', 'summary3']
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.