2016-07-27 70 views
0

使用github3.py,我想检索与拉取请求相关联的注释列表中的最后一个注释,然后在字符串中搜索它。我试过下面的代码,但是我得到错误TypeError: 'GitHubIterator' object does not support indexing(没有索引,我可以检索评论列表)。TypeError:'GitHubIterator'对象不支持索引

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

我敢肯定,你的代码的第一行不会做你想做的。你试图索引(与[-1])不支持索引的对象(它是某种迭代器)。你也会围绕它进行一个列表调用,并在该列表上运行一个循环。我认为你不需要循环。尝试:

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

我已经将list调用中的右括号移到索引之前。这意味着索引发生在列表上,而不是迭代器上。但是,它会浪费一点内存,因为在我们为最后一个索引并将列表丢弃之前,所有的评论都存储在一个列表中。如果内存使用是一个问题,你可以带回循环,摆脱list呼叫:

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

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