2016-09-22 53 views
0

我想檢查一個值是否在列表中的另一個之前。我問了一會兒這個問題:TypeError: 'GitHubIterator' object does not support indexing,它允許我訪問列表中的最後一條評論。我想展開此查看拉請求中的所有評論,看#hold-off評論是否在#sign-off comment之後。我可以使用print語句打印註釋,但是它錯誤地查看錯誤消息的值的順序:AttributeError: 'IssueComment' object has no attribute 'index'檢查一個值是否在列表中的另一個之前

我想我需要以某種方式獲得評論的主體列表,然後使用索引來確定順序,因爲迭代器不支持索引。但是我一直沒有成功實現這個目標。

hold_off_regex_search_string = re.compile(r"\B#hold-off\b", re.IGNORECASE) 
sign_off_regex_search_string = re.compile(r"\B#sign-off\b", re.IGNORECASE) 
for comments in list(GitAuth.repo.issue(prs.number).comments()): 
    print (comments.body) 
    if comments.index(hold_off_regex_search_string.search(comments.body)) > comments.index(sign_off_regex_search_string.search(comments.body)): 
     print('True') 
+0

你不能只是做一個're.search(R '#拖延[\ W \ S] +#簽收',comments.body)'這將返回匹配你的病情?我假設評論將以逗號分隔的單詞。 –

回答

1

看起來你很迷惑自己。 for循環已經按順序遍歷了註釋。您需要做的就是測試#hold-off#sign-off模式的每條評論,並報告您首先看到哪一條。

hold_off_regex_search_string = re.compile(r"\B#hold-off\b", re.IGNORECASE) 
sign_off_regex_search_string = re.compile(r"\B#sign-off\b", re.IGNORECASE) 
special_comments = [] 
for comments in list(GitAuth.repo.issue(prs.number).comments()): 
    if hold_off_regex_search_string.search(comments.body): 
     special_comments.append('HOLD OFF') 
    elif sign_off_regex_search_string.search(comments.body): 
     special_comments.append('SIGN OFF') 
if special_comments == ['HOLD OFF', 'SIGN OFF']: 
    # add label 
elif special_comments == ['SIGN OFF', 'HOLD OFF']: 
    # remove label 
elif special_comments == ['HOLD OFF']: 
    # handle it 
elif special_comments == ['SIGN OFF']: 
    # handle it 
elif special_comments == []: 
    # handle it 
else: 
    # maybe multiple sign offs or hold offs? 
+0

我確實想出了這個解決方案,但我需要知道哪一個先來,因爲這將被擴展爲執行添加和刪除標籤的操作,這些操作基於'#hold-off'和'#sign-off'的順序。在列表中。如果'#hold-off'在'#sign-off'之後出現,請刪除一個標籤;如果'#sign-off'出現在'#hold-off'之後,添加一個標籤。在上面的例子中,如果兩者都存在,它總是會在'#hold-off'中斷開... – DBS

+1

我編輯了答案以跟蹤兩個特殊評論類型發生的順序。 –

相關問題