【问题标题】:Verifying cElementTree or elementTree node type验证 cElementTree 或 elementTree 节点类型
【发布时间】:2015-12-22 12:01:05
【问题描述】:

我在我的应用程序中使用 cElementTree 来解析 XML 文件,它运行良好,但是,现在我想编写一些单元测试来检查我的方法的结果是否返回预期结果。

在我的一项测试中,我想检查从树中检索到的元素是否确实是 Element 类型:

self.assertIsInstance(myElem, Element, 'Incorrect type for myElem')

当我执行测试时,我收到以下错误:

TypeError: isinstance() arg 2 must be a class, type or tuple of classes and types.

如果元素不是类或类型,那么它是什么?我在文档中找不到很多关于此的详细信息。

【问题讨论】:

    标签: python elementtree python-unittest celementtree


    【解决方案1】:

    我从源代码中找到了这段摘录, 文件:ElementTree.py

    ##
    # Element factory.  This function returns an object implementing the
    # standard Element interface.  The exact class or type of that object
    # is implementation dependent, but it will always be compatible with
    # the {@link #_ElementInterface} class in this module.
    # <p>
    # The element name, attribute names, and attribute values can be
    # either 8-bit ASCII strings or Unicode strings.
    #
    # @param tag The element name.
    # @param attrib An optional dictionary, containing element attributes.
    # @param **extra Additional attributes, given as keyword arguments.
    # @return An element instance.
    # @defreturn Element
    
    def Element(tag, attrib={}, **extra):
        attrib = attrib.copy()
        attrib.update(extra)
        return _ElementInterface(tag, attrib)
    

    所以,Element 最终是一个工厂方法。

    【讨论】:

      猜你喜欢
      • 2012-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-11
      • 1970-01-01
      • 2010-09-27
      • 1970-01-01
      相关资源
      最近更新 更多