TypeError: 'GitHubIterator' object does not support indexing [duplicate]

 ̄綄美尐妖づ 提交于 2020-01-07 02:51:47

问题


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

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.



来源:https://stackoverflow.com/questions/38603449/typeerror-githubiterator-object-does-not-support-indexing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!