python - 遍历图表(查找链接的所有问题)

时间:2013-04-12 09:59:53

标签: python recursion graph tail-recursion

我试图解决一个简单的任务来解决所有相关问题。这基本上是一个图形任务我想 - 我接受一个jira问题,找到它的所有链接然后转到他们链接的链接问题,直到我处理完所有问题。

我正在测试的jira isues的布局如下图所示: enter image description here

但结果如下:

PMO-100 -> SA-300
SA-100 -> SA-300
SA-100 -> SA-200

缺少一个链接,但它会错过更多,因为它只处理所有找到的第一个链接:

__init__       6996   Starting...
_new_conn      6996   Starting new HTTP connection (1): 10.48.34.174
collect_links  6996   Looking for links for issue PMO-100
collect_links  6996   Found 2 links: 27846, 27843
collect_links  6996   processing link 27846
put_link       6996   Appending link 27846 to link register
collect_links  6996   Looking for links for issue SA-300
collect_links  6996   Found 2 links: 27845, 27846
collect_links  6996   processing link 27845
put_link       6996   Appending link 27845 to link register
collect_links  6996   Looking for links for issue SA-100
collect_links  6996   Found 3 links: 27844, 27845, 27843
collect_links  6996   processing link 27844
put_link       6996   Appending link 27844 to link register
collect_links  6996   Looking for links for issue SA-200
collect_links  6996   Found 1 links: 27844
collect_links  6996   processing link 27844
put_link       6996   Link 27844 already exists in link register
collect_links  6996   issue SA-100 is already in tracked issues

这是因为for ... each_links方法中的每个循环 - 只要它被调用它将覆盖for ...每个集合:

def collect_links(self, key):
        logging.info('Looking for links for issue %s' % key)
        self.tracked.append(key)
        links = self.jac.issue(key).fields.issuelinks
        logging.info('Found %s links: %s' % (len(links), ', '.join(i.id for i in links)))
        for link in links:
            logging.info('processing link %s' % link.id)
            self.put_link(link)
            rel = None
            if hasattr(link, 'outwardIssue'):
                rel = link.outwardIssue.key
            else:
                rel = link.inwardIssue.key
            linked_issue = self.jac.issue(rel)
            if linked_issue.key not in self.tracked:
                return self.collect_links(linked_issue.key)
            else:
                logging.info('issue %s is already in tracked issues' % linked_issue.key)

看起来当你从方法本身调用self.method时,它不会为它创建一个新的数据块,而是覆盖你已经在方法中创建的数据 - 我该如何克服它? / p>

会发生什么:

for link in links: #links contains the links linked to current issue
    if linked_issue.key not in self.tracked:
        return self.collect_links(linked_issue.key) #links changes

在最后一行我希望它能保持我的链接集合不变,但它会将其更改为linked_issue.key

的链接集合

I've pasted the whole code on pastebin

1 个答案:

答案 0 :(得分:1)

呃,不确定我理解你的问题,但万一我这样做了:

for link in links: #links contains the links linked to current issue
    if linked_issue.key not in self.tracked:
        return self.collect_links(linked_issue.key) #links changes

这当然会在第一个未跟踪链接处停止for循环。如果您想继续遍历links的其余元素,则不得在此处使用return语句。