From: W. Trevor King Date: Thu, 23 Jul 2009 13:25:34 +0000 (-0400) Subject: Added libbe.tree.Tree.has_descendant(). X-Git-Tag: 1.0.0~63^2~4 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=678fccaf505eca6d816b859e6d90d728d6426a02;p=be.git Added libbe.tree.Tree.has_descendant(). Tree equality is now based on instance id. It had previously used the default list "equal if all elements are equal", which meant that all the leaves matched each other. --- diff --git a/libbe/tree.py b/libbe/tree.py index 0256407..45ae085 100644 --- a/libbe/tree.py +++ b/libbe/tree.py @@ -68,7 +68,18 @@ class Tree(list): f h i + >>> a.has_descendant(g) + True + >>> c.has_descendant(g) + False + >>> a.has_descendant(a) + False + >>> a.has_descendant(a, match_self=True) + True """ + def __eq__(self, other): + return id(self) == id(other) + def branch_len(self): """ Exhaustive search every time == SLOW. @@ -157,4 +168,12 @@ class Tree(list): yield (depth,node) stack.append(node) + def has_descendant(self, descendant, depth_first=True, match_self=False): + if descendant == self: + return match_self + for d in self.traverse(depth_first): + if descendant == d: + return True + return False + suite = doctest.DocTestSuite()