[Zope-Checkins] CVS: Zope/lib/python/Interface - Util.py:1.7.20.5

Shane Hathaway shane@digicool.com
Fri, 16 Nov 2001 15:22:44 -0500


Update of /cvs-repository/Zope/lib/python/Interface
In directory cvs.zope.org:/tmp/cvs-serv27598

Modified Files:
      Tag: ComponentArchitecture-branch
	Util.py 
Log Message:
Added a function for flattening a list of interfaces.


=== Zope/lib/python/Interface/Util.py 1.7.20.4 => 1.7.20.5 ===
     return r
 
+
+def _flatten(i, append):
+    append(i)
+    bases = i.getBases()
+    if bases:
+        for b in bases:
+            _flatten(b, append)
+
+
+def flattenInterfaces(interfaces, remove_duplicates=1):
+    res = []
+    for i in interfaces:
+        _flatten(i, res.append)
+    if remove_duplicates:
+        # Remove duplicates in reverse.
+        # Similar to Python 2.2's method resolution order.
+        seen = {}
+        index = len(res) - 1
+        while index >= 0:
+            i = res[index]
+            if seen.has_key(i):
+                del res[index]
+            else:
+                seen[i] = 1
+            index = index - 1
+    return res
+