Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 打印json时如何避免类型检查?_Python_Json_Duck Typing - Fatal编程技术网

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

Python 打印json时如何避免类型检查?,python,json,duck-typing,Python,Json,Duck Typing,我正在使用Python将json文件转换为更易于阅读的输出,json中的特定条目可以采用以下格式之一。我正在尝试创建一个方法,该方法可以在不检查类型的情况下创建适当的输出 "responseType": null 或 或 或 在每种情况下,我的期望输出如下: 响应类型为空 responseType是一个特征模型 responseType是一个特性数组 responseType是一个int32数字数组 我是Python新手,我的第一个爱好是做大量的类型检查和字符串连接。i、 e: str =

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

"responseType": null

在每种情况下,我的期望输出如下:

响应类型为空
responseType是一个特征模型
responseType是一个特性数组
responseType是一个int32数字数组
我是Python新手,我的第一个爱好是做大量的类型检查和字符串连接。i、 e:

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")
这样做感觉非常幼稚和不正确,因为我反复读到你不应该检查类型

"responseType": null

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

您可以使用密钥访问并捕获异常,这是一种称为“比权限更容易请求原谅”或EAFP的技术:

现在我忽略了使用正确的文章(
a
vs.
a
),但你明白了

这并不一定比你在做的事更好,三思而后行(LBYL);它将取决于混合的分布,哪一个更快(因为处理异常相对比使用
if
测试慢,但如果异常不太常见,则更快),或者您发现哪一个更适合您的用例

演示:

请注意,如果您使用类型检查,我将使用
isinstance(ob,class)
over
type(ob)is class
。对于来自JSON的数据,不需要进行严格的检查(在这种情况下,您不能接受子类,只能接受确切的类型),而且
isinstance()
对于Python来说工作量更少,对于其他维护人员来说更具可读性。

使用异常更“Pythonic”。有关更多详细信息,请参阅另一个问题

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

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))
输出:

响应类型为无
响应类型为特征模型
响应类型是Feature
responseType是一个int32数字数组

我认为您没有什么可以改进的。这是一个设计糟糕的数据结构。@Barmar:这并不意味着代码本身的结构就不能更好。@martineau和@martijn pieters answers都提供了可运行的解决方案。但我不确定我是否真的能判断哪一个是“更好”的答案。作为python的新手,我要说@martineau的解决方案更具可读性,我还不知道@martijn pieters做了什么。这两个答案的本质是一样的:“使用异常而不是类型检查”,这将使代码更加“Pythonic”。当然,我是有偏见的,但除此之外,你所说的可读性和理解代码似乎会打破僵局,因为这些东西也是这个概念的重要方面。我将阅读EAFP技术。这对我来说是个新名词。非常感谢。很高兴能帮上忙!如果您觉得它对您有用,请随时使用。:-)为什么不在第一个
try:
block中使用(更多)字符串格式呢?@Martijn:本来可以而且经常这样做,但在这种情况下,它看起来没有任何可读性。
kind={0[type]}{0[of][format]}{0[of][type]}s.format(obj['responseType'])
对我来说看起来可读性更强。@Martijn:虽然它确实更短,在我看来,它隐藏了太多的真实情况。
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")
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)
>>> 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
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))