如何在循环中使用非类型函数调用生成器类型函数?

发布于 2025-01-07 02:36:59 字数 1085 浏览 0 评论 0原文

我在 Reader() 类中有一个生成器函数 getElements,它从 xml 文件中生成所有元素。我还想要一个函数 getFeatures ,它只生成带有功能标签的元素。

我的尝试是在调用 getFeatures 时将标志 featuresOnly 设置为 True,并在 getFeatures 中调用 self.getElements,如下所示:

def getFeatures(self):
    self.getFeaturesOnly = True
    self.getElements()

这样在 getElements() 中我只需要做

def getElements(self):
    inFile = open(self.path)
    for element in cElementTree.iterparse(inFile):
        if self.getFeaturesOnly == True:
            if element.tag == 'feature':
                yield element
        else:
            yield element
     inFile.close()

但是,当我这样做并运行时我

 features = parseFeatureXML.Reader(filePath)
 for element in features.getFeatures():#
       print element

得到: TypeError: 'NoneType' object is not iterable 这是因为 getFeatures 不包含收益。现在,我知道如何解决这个问题的方法是将 getElements 的代码复制到 getFeatures 中,并仅

if elementFunctions.getElmentTag(element) == 'feature':

在 getFeatures() 函数中使用,但我不想重复任何代码。那么我怎样才能保持生成器功能,并拥有一个不同的功能,我只指定我想要获得的标签?

I have a generator function getElements in a class Reader() that yields all the elements out of an xml file. I also want to have a function getFeatures that only yields the elements with a feature tag.

How I tried it is to have a flag featuresOnly that is set to True when getFeatures is called, and in getFeatures call self.getElements, like this:

def getFeatures(self):
    self.getFeaturesOnly = True
    self.getElements()

This way in getElements() I only have to do

def getElements(self):
    inFile = open(self.path)
    for element in cElementTree.iterparse(inFile):
        if self.getFeaturesOnly == True:
            if element.tag == 'feature':
                yield element
        else:
            yield element
     inFile.close()

However, when I do this and run it

 features = parseFeatureXML.Reader(filePath)
 for element in features.getFeatures():#
       print element

I get: TypeError: 'NoneType' object is not iterable
This is because getFeatures doesn't contain a yield. Now, the way that I know how to solve this is to copy the code of getElements into getFeatures and only use the

if elementFunctions.getElmentTag(element) == 'feature':

in the getFeatures() function, but I rather not duplicate any code. So how would I be able to keep on generator function, and have a different function where I only specefy which tag I would like to get?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笑饮青盏花 2025-01-14 02:36:59

首先要做的事情是:你有这个错误,因为你没有返回生成器

这意味着你必须更改:

def getFeatures(self):
    self.getFeaturesOnly = True
    self.getElements()

with:

def getFeatures(self):
    self.getFeaturesOnly = True
    return self.getElements()    # returning the generator

清除了这个,TBH我不会设计我的Reader()类就像这样。

我会让 getElement 产生所有元素:

def getElements(self):
    inFile = open(self.path)
    for element in cElementTree.iterparse(inFile):
        yield element
    inFile.close()

然后 getFeatures() 进行过滤:

def getFeatures(self):
    for element in self.getElements():
        if element.tag == 'feature':
            yield element

First things first: You have that error because you don't return the generator

Meaning that you have to change:

def getFeatures(self):
    self.getFeaturesOnly = True
    self.getElements()

with:

def getFeatures(self):
    self.getFeaturesOnly = True
    return self.getElements()    # returning the generator

Cleared this, TBH I wouldn't design my Reader() class like this.

I'd let the getElement yield all the elements:

def getElements(self):
    inFile = open(self.path)
    for element in cElementTree.iterparse(inFile):
        yield element
    inFile.close()

And then getFeatures() do the filtering:

def getFeatures(self):
    for element in self.getElements():
        if element.tag == 'feature':
            yield element
假装爱人 2025-01-14 02:36:59

您收到 TypeError 的原因并不是 getFeatures 不包含收益,而是因为 getFeatures 不返回任何内容< /em>.如果您希望 getFeatures 返回由 getElements 返回的迭代器,则必须使用 return

def getFeatures(self):
    self.getFeaturesOnly = True
    return self.getElements()

当您使用它时,您确实应该' t do if expr == True;只需执行 if expr 即可,即使 expr 为 true(概念)但不是 True (对象)。将仅功能支持提升到 getElements 中,更常见的方法是在 getFeatures 本身中执行此操作,如下所示:

def getFeatures(self):
    for element in self.getElements():
        if element.tag == 'feature':
            yield element

def getElements(self):
    inFile = open(self.path)
    for element in cElementTree.iterparse(inFile):
        yield element
    inFile.close()

The reason you get the TypeError is not that getFeatures doesn't contain a yield, it's because getFeatures doesn't return anything. If you want getFeatures to return the iterator returned by getElements, you have to use return:

def getFeatures(self):
    self.getFeaturesOnly = True
    return self.getElements()

While you're at it, you really shouldn't do if expr == True; just do if expr, which works even if expr is true (the concept) but not True (the object.) That said, instead of hoisting the features-only support into getElements, a more common approach is to do it in getFeatures itself, like so:

def getFeatures(self):
    for element in self.getElements():
        if element.tag == 'feature':
            yield element

def getElements(self):
    inFile = open(self.path)
    for element in cElementTree.iterparse(inFile):
        yield element
    inFile.close()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文