From 678fccaf505eca6d816b859e6d90d728d6426a02 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Thu, 23 Jul 2009 09:25:34 -0400 Subject: [PATCH] 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. --- libbe/tree.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) 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() -- 2.26.2