| The Gaudi Framework v29r1 (5cd95fa2)

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

Pythonizations.py
Go to the documentation of this file.
1 # File: GaudiPython/Pythonizations.py
2 # Author: Pere Mato (pere.mato@cern.ch)
3
4 """ This Pythonizations module provides a number of useful pythonizations
5 of adaptation of some classes.
6 """
7
8 __all__ = []
9
10 try:
11 from cppyy import gbl
12 except ImportError:
13 # FIXME: backward compatibility
14 print "# WARNING: using PyCintex as cppyy implementation"
15 from PyCintex import gbl
16
17 if not hasattr(gbl, 'ostream'):
18 gbl.gROOT.ProcessLine("#include <ostream>")
19 if not hasattr(gbl, 'stringstream'):
20 gbl.gROOT.ProcessLine("#include <sstream>")
21
22 #--- Adding extra functionality to C++ raw classes------------------------------------
23
24
25 def _printHisto1D(h):
26 x = h.axis()
27 return 'Histogram 1D "%s" %d bins [%f,%f]' % (h.title(), x.bins(), x.lowerEdge(), x.upperEdge())
28
29
30 def _contentsHisto1D(h):
31 x = h.axis()
32 return map(h.binEntries, range(x.bins()))
33
34
35 def _printHisto2D(h):
36 x, y = h.xAxis(), h.yAxis()
37 return 'Histogram 2D "%s" %d xbins [%f,%f], %d ybins [%f,%f]' % \
38 (h.title(), x.bins(), x.lowerEdge(), x.upperEdge(),
39 y.bins(), y.lowerEdge(), y.upperEdge())
40
41
42 def _printStatusCode(s):
43 if s.isSuccess():
44 return 'SUCCESS'
45 else:
46 return 'FAILURE'
47
48
49 def _printBitReference(b):
50 return str(1 == b.bool())
51
52
53 def _printFillStream(o):
54 if o:
55 s = gbl.stringstream()
56 o.fillStream(s)
57 out = str(s.str())
58 if out == '':
59 out = o.__class__.__name__ + ' object'
60 if hasattr(o, 'hasKey') and o.hasKey():
61 out += ' key = ' + str(o.key())
62 else:
63 out = o.__class__.__name__ + ' NULL object'
64 return out
65
66
67 def _container__getitem__(self, k):
68 return self.containedObject(k)
69
70
71 def _container__len__(self):
72 return self.numberOfObjects()
73
74
75 def _container__iter__(self):
76 if hasattr(self, 'containedObjects'):
77 sequential = self.containedObjects()
78 else:
79 sequential = self
80 count = 0
81 limit = self.__len__()
82 while count < limit:
83 yield sequential.__getitem__(count)
84 count += 1
85
86
87 def _draw_aida_(self, *args):
88 """
89 Draw AIDA histogram (through access to internal ROOT histogram
90
91 >>> aida = ... # get the historgam
92 >>> aida.Draw()
93
94 """
95 _fun = gbl.Gaudi.Utils.Aida2ROOT.aida2root
96 _root = _fun(self)
97 return _root.Draw(*args)
98
99
100 gbl.AIDA.IHistogram1D.__repr__ = _printHisto1D
101 gbl.AIDA.IHistogram1D.contents = _contentsHisto1D
102 gbl.AIDA.IHistogram2D.__repr__ = _printHisto2D
103 for h in (gbl.AIDA.IHistogram,
104 gbl.AIDA.IHistogram1D,
105 gbl.AIDA.IHistogram2D,
106 gbl.AIDA.IHistogram3D,
107 gbl.AIDA.IProfile1D,
108 gbl.AIDA.IProfile2D):
109 h.Draw = _draw_aida_
110 h.plot = _draw_aida_
111
112 gbl.StatusCode.__repr__ = _printStatusCode
113 try:
114 gbl._Bit_reference.__repr__ = _printBitReference
115 except:
116 pass
117 gbl.ContainedObject.__repr__ = _printFillStream
118 gbl.DataObject.__repr__ = _printFillStream
119 gbl.ObjectContainerBase.__getitem__ = _container__getitem__
120 gbl.ObjectContainerBase.__len__ = _container__len__
121 gbl.ObjectContainerBase.__iter__ = _container__iter__
122
123 gbl.IUpdateManagerSvc.update = lambda self, obj: gbl.IUpdateManagerSvc.PythonHelper.update(
124 self, obj)
125 gbl.IUpdateManagerSvc.invalidate = lambda self, obj: gbl.IUpdateManagerSvc.PythonHelper.invalidate(
126 self, obj)
127
128 #---Globals--------------------------------------------------------------------
129 if not hasattr(gbl.StatusCode, 'SUCCESS'):
130 # emulate enums
131 gbl.StatusCode.SUCCESS = 1
132 gbl.StatusCode.FAILURE = 0
133
134 # - string key, equality
135 if hasattr(gbl.Gaudi.StringKey, '__cpp_eq__'):
136 _eq = gbl.Gaudi.StringKey.__cpp_eq__
137 setattr(gbl.Gaudi.StringKey, '__eq__', _eq)
138
139 # - string key, non-equality
140 if hasattr(gbl.Gaudi.StringKey, '__cpp_ne__'):
141 _ne = gbl.Gaudi.StringKey.__cpp_ne__
142 setattr(gbl.Gaudi.StringKey, '__ne__', _ne)
143
144
145 #---Enabling Pickle support----------------------------------------------------
146 if gbl.gROOT.GetVersionInt() <= 51800:
147 import libPyROOT
148 gbl.GaudiPython.PyROOTPickle.Initialize(libPyROOT, libPyROOT.ObjectProxy)
149
150 # =============================================================================
151 # decorate some map-like objects
152 # =============================================================================
153 # The iterator for MapBase class
154 #
155 # @code
156 #
157 # >>> m = ... ## the map
158 # >>> for key in m : print key , m[key]
159 #
160 # @endcode
161 # @see Gaudi::Utils::MapBase
162 # @see GaudiUtils::Map
163 # @see GaudiUtils::HashMap
164 # @see GaudiUtils::VectorMap
165 # @see GaudiUtils::Map::key_at
166 # @see GaudiUtils::HashMap::key_at
167 # @see GaudiUtils::VectorMap::key_at
168 # @author Vanya BELYAEV Ivan.Belyaev@itep.ru
169 # @date 2010-02-20
170
171
172 def __mapbase_iter__(self):
173 """
174 The iterator for MapBase-based containers
175
176 >>> m = ... ## the map
177 >>> for key in m : print key , m[key]
178
179 """
180 _size = len(self)
181 _index = 0
182 while _index < _size:
183 yield self.key_at(_index)
184 _index += 1
185
186 # =============================================================================
187 # The iterator for MapBase class
188 #
189 # @code
190 #
191 # >>> m = ... ## the map
192 # >>> for key,value in m.iteritems() : print key , value
193 #
194 # @endcode
195 # @see Gaudi::Utils::MapBase
196 # @see GaudiUtils::Map
197 # @see GaudiUtils::HashMap
198 # @see GaudiUtils::VectorMap
199 # @see GaudiUtils::Map::key_at
200 # @see GaudiUtils::HashMap::key_at
201 # @see GaudiUtils::VectorMap::key_at
202 # @see GaudiUtils::Map::value_at
203 # @see GaudiUtils::HashMap::value_at
204 # @see GaudiUtils::VectorMap::value_at
205 # @author Vanya BELYAEV Ivan.Belyaev@itep.ru
206 # @date 2010-02-20
207
208
209 def __mapbase_iteritems__(self):
210 """
211 The iterator for MapBase-based containers
212
213 >>> m = ... ## the map
214 >>> for key,value in m.iteritems() : print key, value
215
216 """
217 _size = len(self)
218 _index = 0
219 while _index < _size:
220 _key = self.key_at(_index)
221 yield (_key, self.at(_key))
222 _index += 1
223
224 # ============================================
225 # Get the list of keys for the map
226 #
227 # @code
228 #
229 # >>> m = ... ## the map
230 # >>> keys = m.keys() ## get the list of keys
231 #
232 # @endcode
233 # @see Gaudi::Utils::MapBase
234 # @see GaudiUtils::Map
235 # @see GaudiUtils::HashMap
236 # @see GaudiUtils::VectorMap
237 # @see GaudiUtils::Map::key_at
238 # @see GaudiUtils::HashMap::key_at
239 # @ see GaudiUtils::VectorMap::key_at
240 # @author Vanya BELYAEV Ivan.BElyaev@itep.ru
241 # @date 2010-02-20
242
243
244 def __mapbase_keys__(self):
245 """
246 Get the list of keys
247
248 >>> m = ... ## the map
249 >>> keys = m.keys() ## get the list of keys
250
251 """
252 _size = len(self)
253 _keys = []
254 for i in range(0, _size):
255 _keys.append(self.key_at(i))
256 return _keys
257
258 # ============================================
259 # Get the list of items for the map
260 #
261 # @code
262 #
263 # >>> m = ... ## the map
264 # >>> items = m.items() ## get the list of items
265 #
266 # @endcode
267 # @see Gaudi::Utils::MapBase
268 # @see GaudiUtils::Map
269 # @see GaudiUtils::HashMap
270 # @see GaudiUtils::VectorMap
271 # @see GaudiUtils::Map::key_at
272 # @see GaudiUtils::HashMap::key_at
273 # @ see GaudiUtils::VectorMap::key_at
274 # @author Vanya BELYAEV Ivan.BElyaev@itep.ru
275 # @date 2010-02-20
276
277
278 def __mapbase_items__(self):
279 """
280 Get the list of items
281
282 >>> m = ... ## the map
283 >>> items = m.keys() ## get the list of items
284
285 """
286 _size = len(self)
287 _items = []
288 for i in range(0, _size):
289 _key = self.key_at(i)
290 _value = self.at(_key)
291 _items.append((_key, _value))
292 return _items
293
294 # ============================================
295 # Get the list of values for the map
296 #
297 # @code
298 #
299 # >>> m = ... ## the map
300 # >>> values = m.values () ## get the list of values
301 #
302 # @endcode
303 # @see Gaudi::Utils::MapBase
304 # @see GaudiUtils::Map
305 # @see GaudiUtils::HashMap
306 # @see GaudiUtils::VectorMap
307 # @see GaudiUtils::Map::value_at
308 # @see GaudiUtils::HashMap::value_at
309 # @ see GaudiUtils::VectorMap::value_at
310 # @author Vanya BELYAEV Ivan.Belyaev@itep.ru
311 # @date 2010-02-20
312
313
314 def __mapbase_values__(self):
315 """
316 Get the list of values
317
318 >>> m = ... ## the map
319 >>> values = m.values() ## get the list of values
320
321 """
322 _size = len(self)
323 _values = []
324 for i in range(0, _size):
325 _value = self.value_at(i)
326 _values.append(_value)
327 return _values
328
329 # ============================================
330 # Check if the certain key is in the map
331 #
332 # @code
333 #
334 # >>> m = ... ## the map
335 # >>> if 'a' in m : print 'key is in the map!'
336 #
337 # @endcode
338 # @see Gaudi::Utils::MapBase
339 # @see GaudiUtils::Map
340 # @see GaudiUtils::HashMap
341 # @see GaudiUtils::VectorMap
342 # @see GaudiUtils::Map::count
343 # @see GaudiUtils::HashMap::count
344 # @ see GaudiUtils::VectorMap::count
345 # @author Vanya BELYAEV Ivan.BElyaev@itep.ru
346 # @date 2010-02-20
347
348
349 def __mapbase_contains__(self, key):
350 """
351 Check if the certainkey is in the map
352
353 >>> m = ... ## the map
354 >>> if 'a' in m : ... ## chekc the presence of the key in the map
355
356 """
357 _num = self.count(key)
358 return False if 0 == _num else True
359
360 # ============================================
361 # Get the value for certain key,
362 # return predefined value otherwise
363 #
364 # @code
365 #
366 # >>> m = ... ## the map
367 # >>> v = m.get( key , 15 ) ## return the value[key] for existing key, else 15
368 #
369 # @endcode
370 # @see Gaudi::Utils::MapBase
371 # @see GaudiUtils::Map
372 # @see GaudiUtils::HashMap
373 # @see GaudiUtils::VectorMap
374 # @see GaudiUtils::Map::count
375 # @see GaudiUtils::HashMap::count
376 # @ see GaudiUtils::VectorMap::count
377 # @author Vanya BELYAEV Ivan.BElyaev@itep.ru
378 # @date 2010-02-20
379
380
381 def __mapbase_get__(self, key, value=None):
382 """
383 Get the value for the certain key, or 'value' otherwise
384
385 >>> m = ... ## the map
386 >>> v = m.get ( key , 15 )
387
388 """
389 if key in self:
390 return self.at(key)
391 return value
392
393 # ============================================
394 # Representation of MapBase-based maps
395 #
396 # @code
397 #
398 # >>> m = ... ## the map
399 # >>> print m
400 #
401 # @endcode
402 # @see Gaudi::Utils::MapBase
403 # @see GaudiUtils::Map
404 # @see GaudiUtils::HashMap
405 # @see GaudiUtils::VectorMap
406 # @author Vanya BELYAEV Ivan.BElyaev@itep.ru
407 # @date 2010-02-20
408
409
410 def __mapbase_str__(self):
411 """
412 Representation of MapBase-based maps:
413
414 >>> m = ... ## the map
415 >>> print map
416
417 """
418 _result = ' { '
419 _size = len(self)
420 for i in range(0, _size):
421 _key = self.key_at(i)
422 _val = self.at(_key)
423 if 0 != i:
424 _result += ' , '
425 _result += " %s : %s " % (str(_key), str(_val))
426 _result += ' } '
427 return _result
428
429 # ============================================
430 # "Setitem" for MapBase-based maps:
431 #
432 # @code
433 #
434 # >>> m = ... ## the map
435 # >>> m [ key] = value ## set the item
436 #
437 # @endcode
438 # @see Gaudi::Utils::MapBase
439 # @see GaudiUtils::Map
440 # @see GaudiUtils::HashMap
441 # @see GaudiUtils::VectorMap
442 # @see GaudiUtils::Map::update
443 # @see GaudiUtils::HashMap::update
444 # @see GaudiUtils::VectorMap::update
445 # @author Vanya BELYAEV Ivan.BElyaev@itep.ru
446 # @date 2010-02-20
447
448
449 def __mapbase_setitem__(self, key, value):
450 """
451 'Set-item' for MapBase-based maps:
452
453 >>> m = ... ## the map
454 >>> m[key] = value ## set the item
455
456 """
457 _replaced = True if key in self else False
458 self.update(key, value)
459 return _replaced
460
461 # ============================================
462 # "Del-item" for MapBase-based maps:
463 #
464 # @code
465 #
466 # >>> m = ... ## the map
467 # >>> del m [ key] ## del th eitem
468 #
469 # @endcode
470 #
471 # @see Gaudi::Utils::MapBase
472 # @see GaudiUtils::Map
473 # @see GaudiUtils::HashMap
474 # @see GaudiUtils::VectorMap
475 # @see GaudiUtils::Map::erase
476 # @see GaudiUtils::HashMap::erase
477 # @see GaudiUtils::VectorMap::erase
478 # @author Vanya BELYAEV Ivan.BElyaev@itep.ru
479 # @date 2010-02-20
480
481
482 def __mapbase_delitem__(self, key):
483 """
484 'Del-item' for MapBase-based maps:
485
486 >>> m = ... ## the map
487 >>> del m[key]
488
489 """
490 _erased = True if key in self else False
491 self.erase(key)
492 return _erased
493
494
495 gbl.Gaudi.Utils.MapBase . __len__ = lambda s: s.size()
496 gbl.Gaudi.Utils.MapBase . __iter__ = __mapbase_iter__
497 gbl.Gaudi.Utils.MapBase . keys = __mapbase_keys__
498 gbl.Gaudi.Utils.MapBase . __iteritems__ = __mapbase_iteritems__
499 gbl.Gaudi.Utils.MapBase . iteritems = __mapbase_iteritems__
500 gbl.Gaudi.Utils.MapBase . items = __mapbase_items__
501 gbl.Gaudi.Utils.MapBase . values = __mapbase_values__
502 gbl.Gaudi.Utils.MapBase . __contains__ = __mapbase_contains__
503 gbl.Gaudi.Utils.MapBase . has_key = __mapbase_contains__
504 gbl.Gaudi.Utils.MapBase . get = __mapbase_get__
505 gbl.Gaudi.Utils.MapBase . __str__ = __mapbase_str__
506 gbl.Gaudi.Utils.MapBase . __repr__ = __mapbase_str__
507 gbl.Gaudi.Utils.MapBase . __setitem__ = __mapbase_setitem__
508 gbl.Gaudi.Utils.MapBase . __delitem__ = __mapbase_delitem__
509 gbl.Gaudi.Utils.MapBase . __getitem__ = lambda s, key: s.at(key)
GaudiPython.Pythonizations.__mapbase_values__
def __mapbase_values__(self)
Definition: Pythonizations.py:314
GaudiPython.Pythonizations.__mapbase_setitem__
def __mapbase_setitem__(self, key, value)
Definition: Pythonizations.py:449
GaudiPython.Pythonizations.__mapbase_contains__
def __mapbase_contains__(self, key)
Definition: Pythonizations.py:349
GaudiPython.Pythonizations._printStatusCode
def _printStatusCode(s)
Definition: Pythonizations.py:42
GaudiPython.Pythonizations._container__getitem__
def _container__getitem__(self, k)
Definition: Pythonizations.py:67
GaudiPython.Pythonizations.__mapbase_iteritems__
def __mapbase_iteritems__(self)
Definition: Pythonizations.py:209
GaudiPython.Pythonizations._printFillStream
def _printFillStream(o)
Definition: Pythonizations.py:53
GaudiPython.Pythonizations._printBitReference
def _printBitReference(b)
Definition: Pythonizations.py:49
GaudiPython.Pythonizations.__mapbase_items__
def __mapbase_items__(self)
Definition: Pythonizations.py:278
GaudiPython.Pythonizations._container__iter__
def _container__iter__(self)
Definition: Pythonizations.py:75
GaudiPython.Pythonizations.__mapbase_keys__
def __mapbase_keys__(self)
Definition: Pythonizations.py:244
Containers::map
struct GAUDI_API map
Parametrisation class for map-like implementation.
Definition: KeyedObjectManager.h:26
GaudiPython.Pythonizations._draw_aida_
def _draw_aida_(self, args)
Definition: Pythonizations.py:87
GaudiPython.Pythonizations._printHisto1D
def _printHisto1D(h)
Definition: Pythonizations.py:25
GaudiPython.Pythonizations.__mapbase_get__
def __mapbase_get__(self, key, value=None)
Definition: Pythonizations.py:381
GaudiPython.Pythonizations.__mapbase_iter__
def __mapbase_iter__(self)
Definition: Pythonizations.py:172
Gaudi::Functional::details::zip::range
decltype(auto) range(Args &&...args)
Zips multiple containers together to form a single range.
Definition: FunctionalDetails.h:85
GaudiPython.Pythonizations.__mapbase_delitem__
def __mapbase_delitem__(self, key)
Definition: Pythonizations.py:482
GaudiPython.Pythonizations._container__len__
def _container__len__(self)
Definition: Pythonizations.py:71
GaudiPython.Pythonizations._printHisto2D
def _printHisto2D(h)
Definition: Pythonizations.py:35
GaudiPython.Pythonizations._contentsHisto1D
def _contentsHisto1D(h)
Definition: Pythonizations.py:30
GaudiPython.Pythonizations.__mapbase_str__
def __mapbase_str__(self)
Definition: Pythonizations.py:410