【问题标题】:How to avoid type checking when printing json?打印json时如何避免类型检查?
【发布时间】:2017-03-16 23:28:24
【问题描述】:

我正在使用 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")

考虑到我反复读到你永远不应该检查类型,这样做感觉非常幼稚和不正确。

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

【问题讨论】:

  • 我不认为你可以做很多事情来改善这一点。这是一个设计不佳的数据结构。
  • @Barmar:这并不意味着代码本身的结构不能更好。
  • @martineau 和 @martijn-pieters 的答案都提供了可运行的解决方案。但我不确定我能否真正判断哪个是“更好”的答案。作为 python 的新手,我会说@martineau 的解决方案更具可读性,但我还不知道@martijn-pieters 做了什么。对我有帮助,两者都利用了嵌套TypeErrors 的技术
  • 两个答案的本质是相同的:“使用异常而不是类型检查”这将使代码更加“Pythonic”。当然,我有偏见,但除此之外,你所说的可读性和能够理解代码似乎会打破僵局——因为这些也是这个概念的重要方面。

标签: python json duck-typing


【解决方案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 的工作量更少,对其他维护者来说更具可读性。

【讨论】:

  • 我将阅读 EAFP 技术。这对我来说是一个新名词。谢谢!
  • 很高兴能帮上忙!如果您觉得它对您有用,请随时 accept my answer。 :-)
【解决方案2】:

使用异常会更“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

【讨论】:

  • 为什么不在第一个try: 块中使用(更多)字符串格式?
  • @Martijn:本来可以并且经常这样做,但在这种情况下,它似乎不再具有可读性。
  • kind = '{0[type]} of {0[of][format]} {0[of][type]}s'.format(obj['responseType']) 对我来说更具可读性。
  • @Martijn:虽然这肯定更短,但在我看来,它隐藏了太多真正发生的事情。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-11-22
  • 2011-07-11
  • 1970-01-01
  • 2018-07-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多