summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsyt <syt@localhost.localdomain>2006-11-23 15:37:29 +0100
committersyt <syt@localhost.localdomain>2006-11-23 15:37:29 +0100
commit1bfddfdb575c57d6383efefdb13a44ccd56b3be1 (patch)
tree98c4d05d03664d0893bb672207dacc639bbc301f
parentc642a7e6d32438bc78947b6f967a6118f3fa0650 (diff)
downloadlogilab-common-1bfddfdb575c57d6383efefdb13a44ccd56b3be1.tar.gz
make Node iterable
-rw-r--r--ChangeLog3
-rw-r--r--__init__.py40
-rw-r--r--tree.py11
3 files changed, 29 insertions, 25 deletions
diff --git a/ChangeLog b/ChangeLog
index f2f1199..c4ff948 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,6 +1,9 @@
ChangeLog for logilab.common
============================
+ --
+ * tree: make Node iterable (iter on its children)
+
2006-11-14 -- 0.21.0
* db:
- new optional keepownership argument to backup|restore_database methods
diff --git a/__init__.py b/__init__.py
index 6c88683..55b1bb8 100644
--- a/__init__.py
+++ b/__init__.py
@@ -81,7 +81,27 @@ def union(list1, list2):
return tmp.keys()
-# XXX are functions below still used ?
+# XXX move in a specific module
+
+def flatten(iterable, tr_func=None, results=None):
+ """flatten a list of list with any level
+
+ if tr_func is not None, it should be a one argument function that'll be called
+ on each final element
+ """
+ if results is None:
+ results = []
+ for val in iterable:
+ if isinstance(val, (list, tuple)):
+ flatten(val, tr_func, results)
+ elif tr_func is None:
+ results.append(val)
+ else:
+ results.append(tr_func(val))
+ return results
+
+
+# XXX is function below still used ?
def make_domains(lists):
"""
@@ -108,21 +128,3 @@ def make_domains(lists):
i += 1
domains.append(new_domain)
return domains
-
-
-def flatten(iterable, tr_func=None, results=None):
- """flatten a list of list with any level
-
- if tr_func is not None, it should be a one argument function that'll be called
- on each final element
- """
- if results is None:
- results = []
- for val in iterable:
- if isinstance(val, (list, tuple)):
- flatten(val, tr_func, results)
- elif tr_func is None:
- results.append(val)
- else:
- results.append(tr_func(val))
- return results
diff --git a/tree.py b/tree.py
index f8c0828..a69a851 100644
--- a/tree.py
+++ b/tree.py
@@ -19,6 +19,7 @@
import sys
+from logilab.common import flatten
from logilab.common.visitor import VisitedMixIn, FilteredIterator, no_filter
## Exceptions #################################################################
@@ -182,16 +183,14 @@ class Node :
else:
return [self]
+ def __iter__(self):
+ return iter(self.children)
+
def flatten(self, _list=None):
"""
return a list with all the nodes descendant from this node
"""
- if _list is None:
- _list = []
- _list.append(self)
- for c in self.children:
- c.flatten(_list)
- return _list
+ return flatten(self)
def lineage(self):
"""