TypeError:'GitHubIterator'对象不支持索引 [英] TypeError: 'GitHubIterator' object does not support indexing

查看:219
本文介绍了TypeError:'GitHubIterator'对象不支持索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用github3.py,我想检索与pull请求关联的注释列表中的最后一条注释,然后在其中搜索字符串。我已经尝试了下面的代码,但是我收到错误 TypeError:'GitHubIterator'对象不支持索引(没有索引,我可以检索注释列表)。

Using github3.py, I want to retrieve the last comment in the list of comments associated with a pull request and then search it for a string. I've tried the code below, but I get the error TypeError: 'GitHubIterator' object does not support indexing (without indexing, I can retrieve the list of comments).

for comments in list(GitAuth.repo.issue(prs.number).comments()[-1]):
    if sign_off_regex_search_string.search(comments.body) and comments.user.login == githubID:
        sign_off_by_author_search_string_found = 'True'
        break


推荐答案

我很确定你的代码的第一行不能达到你想要的效果。您正在尝试索引(使用 [ - 1] )一个不支持索引的对象(它是某种迭代器)。你还可以在它周围进行一个列表调用,并在该列表上运行一个循环。我想你不需要循环。尝试:

I'm pretty sure the first line of your code doesn't do what you want. You're trying to index (with [-1]) an object that doesn't support indexing (it is some kind of iterator). You've also go a list call wrapped around it, and a loop running on that list. I think you don't need the loop. Try:

comments = list(GitAuth.repo.issue(prs.number).comments())[-1]

我已从列表移动了右括号在索引之前打电话来。这意味着索引发生在列表上,而不是迭代器上。然而它确实浪费了一点内存,因为在我们索引最后一个并扔掉列表之前,所有注释都存储在列表中。如果考虑内存使用情况,你可以带回循环并摆脱列表调用:

I've moved the closing parenthesis from the list call to come before the indexing. This means the indexing happens on the list, rather than on the iterator. It does however waste a bit of memory, since all the comments get stored in a list before we index the last one and throw the list away. If memory usage is a concern, you could bring back the loop and get rid of the list call:

for comments in GitAuth.repo.issue(prs.number).comments():
    pass # the purpose of this loop is to get the last `comments` value

其余代码不应该在此循环中。循环变量 comments (可能应该是 comment ,因为它引用单个项目)将保持绑定到循环结束后迭代器的最后一个值。这就是你想要搜索的内容。

The rest of the code should not be inside this loop. The loop variable comments (which should probably be comment, since it refers to a single item) will remain bound to the last value from the iterator after the loop ends. That's what you want to do your search on.

这篇关于TypeError:'GitHubIterator'对象不支持索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆