如何减少到一个尝试,语句除外

时间:2018-11-28 12:45:54

标签: python request try-except

我有一个包含多个try-except语句的字典。 我尝试使用r.get()检索值,但使用NoneObject引发类型错误。我知道.get()的默认参数是None,但是它不起作用。我有多个列表,我会在每个列表中附加来自不同字典值的数据。

如何将代码简化为单个try-except语句?谢谢!

这是我的代码:

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')

4 个答案:

答案 0 :(得分:2)

您可以通过链接多个Sheets("Sheetname").Cells(.Rows.Count, "A").End(xlUp).Row 调用来避免所有这些事情:

get

但是,由于您多次这样做,因此使用此功能可能会发现更方便,例如:

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'))

答案 1 :(得分:2)

我倾向于将这类代码重构为类似的代码:

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)

如果您真的想获取部分条目,则可以使用@jdehesa中的get_deep

答案 2 :(得分:1)

您可以将密钥作为元组存储在某个列表中,然后检查这些密钥对是否存在于r中,如果存在,请将它们添加到您的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)

输出:

['NA', 'test1', 'NA', 'NA', 'test2', 'NA', 'NA', 'NA']

答案 3 :(得分:1)

您可以使用字典来避免数量可变的变量。另外,您可以使用functools.reduce来迭代嵌套字典。这是一个完整的示例:

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']