[Python-checkins] r56371 - in sandbox/trunk/2to3: fixes/util.py
collin.winter
python-checkins at python.org
Sat Jul 14 20:23:00 CEST 2007
Author: collin.winter
Date: Sat Jul 14 20:23:00 2007
New Revision: 56371
Modified:
sandbox/trunk/2to3/ (props changed)
sandbox/trunk/2to3/fixes/util.py
Log:
Add an attr_chain() utility function.
Modified: sandbox/trunk/2to3/fixes/util.py
==============================================================================
--- sandbox/trunk/2to3/fixes/util.py (original)
+++ sandbox/trunk/2to3/fixes/util.py Sat Jul 14 20:23:00 2007
@@ -152,6 +152,29 @@
return l[::-1]
###########################################################
+### Misc
+###########################################################
+
+def attr_chain(obj, attr):
+ """Follow an attribute chain.
+
+ If you have a chain of objects where a.foo -> b, b.foo-> c, etc,
+ use this to iterate over all objects in the chain. Iteration is
+ terminated by getattr(x, attr) is None.
+
+ Args:
+ obj: the starting object
+ attr: the name of the chaining attribute
+
+ Yields:
+ Each successive object in the chain.
+ """
+ next = getattr(obj, attr)
+ while next:
+ yield next
+ next = getattr(next, attr)
+
+###########################################################
### The following functions are to find bindings in a suite
###########################################################
More information about the Python-checkins
mailing list