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