打印json时如何避免类型检查?

时间:2017-03-15 23:38:58

标签: python json duck-typing

我使用Python将json文件转换为更易读的输出,json中的特定条目可以采用以下格式之一。我正在尝试创建一个方法来处理创建适当的输出而不检查类型。

"responseType": null

"responseType": "FEATURE MODEL"

"responseType": {
    "type": "array",
    "of": "Feature"
}

"responseType": {
    "type": "array",
     "of": {
             "type": "number",
             "format": "int32"
           }
}

在每种情况下我想要的输出是这样的:

The responseType is null
The responseType is a Feature Model
The responseType is an array of Feature
The responseType is an array of int32 numbers

我是Python的新手,我的第一个倾向是做很多类型检查和字符串连接。即:

str = "The response type is "
if type(obj["responseType"]) is str:
    str += obj["responseType"]
elif type(obj["responseType"]) is dict:
    str += obj["responseType"]["type"] + " "
    if type(obj["responseType"]["of"] is str
        str += obj["responseType"]["of"]
    else:
        #dict output
        #etc...
elif type(obj["responseType"] is None:
     print("The response type is null")

这样做非常幼稚和不正确,因为我反复阅读,你永远不应该检查类型。

那么,在没有进行所有类型检查的情况下处理这种情况的pythonic方法是什么?

2 个答案:

答案 0 :(得分:1)

您可以使用密钥访问并捕获异常,这种技术称为易于请求宽恕而不是权限,或 EAFP

rtype = obj["responseType"]
output = []
try:
    container_type = rtype['type']
except TypeError:
    # not a dictionary, so plain type
    output.extend(['null'] if rtype is None else ['a', rtype])
else:
    # Container type
    output.extend(['a', container_type, 'of'])
    # Check for subtype
    try:
        sub_type = rtype['of']['type']
    except TypeError:
        output.append(rtype['of'])
    else:
        output.extend([rtype['of']['format'], sub_type + 's'])
print('The response type is', *output)

我暂时没有使用正确的文章(aan),但您明白了。

它不一定比你正在做的更好,在你跳跃之前 LBYL );它取决于混合的分布哪一个更快(因为处理异常比使用if测试相对慢,但如果异常不太常见则更快),或者哪一个你发现更易读你的用例。

演示:

>>> def output(obj):
...     rtype = obj["responseType"]
...     output = []
...     try:
...         container_type = rtype['type']
...     except TypeError:
...         # not a dictionary, so plain type
...         output.extend(['null'] if rtype is None else ['a', rtype])
...     else:
...         # Container type
...         output.extend(['a', container_type, 'of'])
...         # Check for subtype
...         try:
...             sub_type = rtype['of']['type']
...         except TypeError:
...             output.append(rtype['of'])
...         else:
...             output.extend([rtype['of']['format'], sub_type + 's'])
...     print('The response type is', *output)
...
>>> responses = [
...     {"responseType": None},
...     {"responseType": "FEATURE MODEL"},
...     {"responseType": {"type": "array", "of": "Feature"}},
...     {"responseType": {"type": "array", "of": {"type": "number", "format": "int32"}}},
... ]
>>> for r in responses:
...     output(r)
...
The response type is null
The response type is a FEATURE MODEL
The response type is a array of Feature
The response type is a array of int32 numbers

请注意,如果您使用类型检查,我会使用isinstance(ob, class)而不是type(ob) is class。使用来自JSON的数据,不需要进行严格的检查(你不能接受子类,只有确切的类型),而isinstance()对Python的工作较少,对其他维护者来说更易读。

答案 1 :(得分:1)

使用异常会更“Pythonic”。有关详细信息,请参阅my answer至其他问题。

无论如何,这是将它们应用于您的问题的可运行示例:

import json

json_resp = '''[{"responseType": null},
                {"responseType": "FEATURE MODEL"},
                {"responseType": {"of": "Feature", "type": "array"}},
                {"responseType": {"type": "array",
                                  "of": {"format": "int32", "type": "number"}}}]'''
objs = json.loads(json_resp)

for obj in objs:
    try:
        kind = ('an ' + obj['responseType']['type']
                    + ' of ' + obj['responseType']['of']['format']
                        + ' ' + obj['responseType']['of']['type']
                            + 's')
    except TypeError:
        try:
            kind = obj['responseType']['of']
        except TypeError:
            kind = obj['responseType']

    print('The responseType is {}'.format(kind))

输出:

The responseType is None
The responseType is FEATURE MODEL
The responseType is Feature
The responseType is an array of int32 numbers