如何在循环中使用非类型函数调用生成器类型函数?
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先要做的事情是:你有这个错误,因为你没有返回生成器
这意味着你必须更改:
with:
清除了这个,TBH我不会设计我的
Reader()类就像这样。
我会让
getElement
产生所有元素:然后
getFeatures()
进行过滤:First things first: You have that error because you don't return the generator
Meaning that you have to change:
with:
Cleared this, TBH I wouldn't design my
Reader()
class like this.I'd let the
getElement
yield all the elements:And then
getFeatures()
do the filtering:您收到
TypeError
的原因并不是getFeatures
不包含收益,而是因为getFeatures
不返回任何内容< /em>.如果您希望getFeatures
返回由getElements
返回的迭代器,则必须使用return
:当您使用它时,您确实应该' t do
if expr == True
;只需执行if expr
即可,即使expr
为 true(概念)但不是True
(对象)。将仅功能支持提升到 getElements 中,更常见的方法是在 getFeatures 本身中执行此操作,如下所示:The reason you get the
TypeError
is not thatgetFeatures
doesn't contain a yield, it's becausegetFeatures
doesn't return anything. If you wantgetFeatures
to return the iterator returned bygetElements
, you have to usereturn
:While you're at it, you really shouldn't do
if expr == True
; just doif expr
, which works even ifexpr
is true (the concept) but notTrue
(the object.) That said, instead of hoisting the features-only support intogetElements
, a more common approach is to do it ingetFeatures
itself, like so: