2016-09-22 68 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

我编辑了答案以跟踪两个特殊评论类型发生的顺序。 –

相关问题