Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:33:14

0001 # decorator for adding iterators to container like objects
0002 # NOTE: EventBranch._readData has to be taken care of at another place!
0003 
0004 #import cmserror
0005 
0006 from builtins import range
0007 def addIterator(obj):
0008     """function for adding iterators to objects""" 
0009     if not hasattr(obj, "__iter__"):
0010         if hasattr(obj, "size"):
0011             obj.__iter__ = iteratorForSizedObjects
0012         else:
0013             try:
0014               begin, end = _findIterators(obj)
0015             except:
0016               return obj  
0017             if not hasattr(obj, "_begin") and hasattr(obj, "_end"):
0018                 obj._begin = begin
0019                 obj._end = end
0020                 obj.__iter__ = iteratorForBeginEnd
0021         #else:
0022         #    obj.__iter__ = EmptyIterator
0023     return obj
0024 
0025 
0026 def iteratorForSizedObjects(self):
0027     """dynamically added iterator"""
0028     entries = container.size()
0029     for entry in range(entries):
0030         yield obj[entry]
0031   
0032         
0033 def iteratorForBeginEnd(self):
0034     """dynamically added iterator"""
0035     it = self._begin
0036     while (it != self.end):
0037         yield begin.__deref__()  #*b
0038         begin.__preinc__()       #++b
0039 
0040 
0041 def emptyIterator(self):
0042     """empty iterator"""
0043     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 )
0044 
0045 
0046 # automatic detection of iterators.      
0047 def _findIterators(obj):
0048     objDict = obj.__dict__
0049     _beginNames = [name for name in objDict.keys() if "begin" in 
0050 name.lower()]
0051     _endNames = [name for name in objDict.keys() if "end" in name.lower()]
0052     if len(_beginNames)==1 and len(_endNames)== 1 and _beginNames[0].lower().replace("begin","") == _endNames[0].lower().replace("end",""):  
0053         return objDict[_beginNames[0]], objDict[_endNames[0]]
0054     else:
0055         return False
0056         
0057         
0058         
0059 ##########################
0060 if __name__ == "__main__":
0061 
0062     import unittest
0063     class TestIterators(unittest.TestCase):
0064     
0065         def testFindIterators(self):
0066             class A(object):
0067                 pass
0068             a = A()
0069             a.BeGin_foo = 1
0070             a.EnD_foo = 100
0071             self.assertEqual(_findIterators(a),(1,100))
0072             a.begin_bar = 1
0073             a.end_bar = 100
0074             self.failIf(_findIterators(a))
0075                   
0076         def testAddIterator(self):
0077             # test for size types
0078             class A(object):
0079                 size = 3
0080             a = A()
0081             a = addIterator(a)
0082             self.assert_(hasattr(a, "__iter__"))
0083             # test if __iter__ already there
0084             class B(object):
0085                 def __iter__(self):
0086                     return True
0087             b = B()
0088             b = addIterator(b)
0089             self.assert_(b.__iter__())
0090             
0091         
0092     unittest.main()
0093 
0094