CMS 3D CMS Logo

iterators.py
Go to the documentation of this file.
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 from builtins import range
7 def addIterator(obj):
8  """function for adding iterators to objects"""
9  if not hasattr(obj, "__iter__"):
10  if hasattr(obj, "size"):
11  obj.__iter__ = iteratorForSizedObjects
12  else:
13  try:
14  begin, end = _findIterators(obj)
15  except:
16  return obj
17  if not hasattr(obj, "_begin") and hasattr(obj, "_end"):
18  obj._begin = begin
19  obj._end = end
20  obj.__iter__ = iteratorForBeginEnd
21  #else:
22  # obj.__iter__ = EmptyIterator
23  return obj
24 
25 
27  """dynamically added iterator"""
28  entries = container.size()
29  for entry in range(entries):
30  yield obj[entry]
31 
32 
34  """dynamically added iterator"""
35  it = self._begin
36  while (it != self.end):
37  yield begin.__deref__() #*b
38  begin.__preinc__() #++b
39 
40 
41 def emptyIterator(self):
42  """empty iterator"""
43  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 )
44 
45 
46 # automatic detection of iterators.
47 def _findIterators(obj):
48  objDict = obj.__dict__
49  _beginNames = [name for name in objDict.keys() if "begin" in
50 name.lower()]
51  _endNames = [name for name in objDict.keys() if "end" in name.lower()]
52  if len(_beginNames)==1 and len(_endNames)== 1 and _beginNames[0].lower().replace("begin","") == _endNames[0].lower().replace("end",""):
53  return objDict[_beginNames[0]], objDict[_endNames[0]]
54  else:
55  return False
56 
57 
58 
59 
60 if __name__ == "__main__":
61 
62  import unittest
63  class TestIterators(unittest.TestCase):
64 
65  def testFindIterators(self):
66  class A(object):
67  pass
68  a = A()
69  a.BeGin_foo = 1
70  a.EnD_foo = 100
71  self.assertEqual(_findIterators(a),(1,100))
72  a.begin_bar = 1
73  a.end_bar = 100
74  self.failIf(_findIterators(a))
75 
76  def testAddIterator(self):
77  # test for size types
78  class A(object):
79  size = 3
80  a = A()
81  a = addIterator(a)
82  self.assert_(hasattr(a, "__iter__"))
83  # test if __iter__ already there
84  class B(object):
85  def __iter__(self):
86  return True
87  b = B()
88  b = addIterator(b)
89  self.assert_(b.__iter__())
90 
91 
92  unittest.main()
93 
94 
FastTimerService_cff.range
range
Definition: FastTimerService_cff.py:34
resolutioncreator_cfi.object
object
Definition: resolutioncreator_cfi.py:4
python.cmstools.cmserror
Definition: cmstools.py:176
python.iterators.TestIterators.testAddIterator
def testAddIterator(self)
Definition: iterators.py:76
python.iterators.emptyIterator
def emptyIterator(self)
Definition: iterators.py:41
python.iterators._findIterators
def _findIterators(obj)
Definition: iterators.py:47
python.iterators.addIterator
def addIterator(obj)
Definition: iterators.py:7
python.iterators.TestIterators
Definition: iterators.py:63
python.iterators.TestIterators.testFindIterators
def testFindIterators(self)
Definition: iterators.py:65
A
TtFullHadDaughter::B
static const std::string B
Definition: TtFullHadronicEvent.h:9
python.iterators.iteratorForBeginEnd
def iteratorForBeginEnd(self)
Definition: iterators.py:33
python.iterators.iteratorForSizedObjects
def iteratorForSizedObjects(self)
Definition: iterators.py:26
python.rootplot.root2matplotlib.replace
def replace(string, replacements)
Definition: root2matplotlib.py:444