2016-07-27 72 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,因爲它引用了單個項目)將在循環結束後保持綁定到來自迭代器的最後一個值。這就是你想要做的搜索。