ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/CMSSW/PhysicsTools/PythonAnalysis/python/iterators.py
Revision: 1.1
Committed: Sat Feb 24 03:03:12 2007 UTC (18 years, 2 months ago) by hegner
Content type: text/x-python
Branch: MAIN
CVS Tags: V00-01-04, V00-01-03, V00-01-02
Log Message:
decorator for adding iterators to container like objects

File Contents

# User Rev Content
1 hegner 1.1 # decorator for adding iterators to container like objects
2     # NOTE: EventBranch._readData has to be taken care of at another place!
3    
4     #import cmserror
5    
6     def addIterator(obj):
7     """function for adding iterators to objects"""
8     if not hasattr(obj, "__iter__"):
9     if hasattr(obj, "size"):
10     obj.__iter__ = iteratorForSizedObjects
11     else:
12     begin, end = _findIterators(obj)
13     if not hasattr(obj, "_begin") and hasattr(obj, "_end"):
14     obj._begin = begin
15     obj._end = end
16     obj.__iter__ = iteratorForBeginEnd
17     #else:
18     # obj.__iter__ = EmptyIterator
19     return obj
20    
21    
22     def iteratorForSizedObjects(self):
23     """dynamically added iterator"""
24     entries = container.size()
25     for entry in xrange(entries):
26     yield obj[entry]
27    
28    
29     def iteratorForBeginEnd(self):
30     """dynamically added iterator"""
31     it = self._begin
32     while (it != end):
33     yield begin.__deref__() #*b
34     begin.__preinc__() #++b
35    
36    
37     def emptyIterator(self):
38     """empty iterator"""
39     raise cmserror("Automatic iterator search failed for %s. Either it is no iterable or it has multiple iterator possibilites. Please use loop(begin, end) instead." %obj )
40    
41    
42     # automatic detection of iterators.
43     def _findIterators(obj):
44     objDict = obj.__dict__
45     _beginNames = [name for name in objDict.keys() if "begin" in
46     name.lower()]
47     _endNames = [name for name in objDict.keys() if "end" in name.lower()]
48     if len(_beginNames)==1 and len(_endNames)== 1 and _beginNames[0].lower().replace("begin","") == _endNames[0].lower().replace("end",""):
49     return objDict[_beginNames[0]], objDict[_endNames[0]]
50     else:
51     return False
52    
53    
54    
55     ##########################
56     if __name__ == "__main__":
57    
58     import unittest
59     class TestIterators(unittest.TestCase):
60    
61     def testFindIterators(self):
62     class A(object):
63     pass
64     a = A()
65     a.BeGin_foo = 1
66     a.EnD_foo = 100
67     self.assertEqual(_findIterators(a),(1,100))
68     a.begin_bar = 1
69     a.end_bar = 100
70     self.failIf(_findIterators(a))
71    
72     def testAddIterator(self):
73     # test for size types
74     class A(object):
75     size = 3
76     a = A()
77     a = addIterator(a)
78     self.assert_(hasattr(a, "__iter__"))
79     # test if __iter__ already there
80     class B(object):
81     def __iter__(self):
82     return True
83     b = B()
84     b = addIterator(b)
85     self.assert_(b.__iter__())
86    
87    
88     unittest.main()
89    
90