import ROOT
from ROOT import pythonization
ROOT.gInterpreter.Declare('''
class MyClass {};
''')
@pythonization('MyClass')
def pythonizor_of_myclass(klass):
klass.__str__ = lambda o : 'This is a MyClass object'
my_object = ROOT.MyClass()
print(my_object)
ROOT.gInterpreter.Declare('''
namespace NS {
class Class1 {};
class Class2 {};
}
''')
@pythonization(['Class1', 'Class2'], ns='NS')
def pythonize_two_classes(klass):
klass.new_attribute = 1
o1 = ROOT.NS.Class1()
o2 = ROOT.NS.Class2()
print("Printing new attribute")
for o in o1, o2:
print(o.new_attribute)
@pythonization('vector<', ns='std', is_prefix=True)
def vector_pythonizor(klass):
klass.first_elem = lambda v : v[0] if v else None
v_int = ROOT.std.vector['int']([1,2,3])
v_double = ROOT.std.vector['double']([4.,5.,6.])
print("First element of integer vector: {}".format(v_int.first_elem()))
print("First element of double vector: {}".format(v_double.first_elem()))
@pythonization('pair<', ns='std', is_prefix=True)
def pair_pythonizor(klass, name):
print('Pythonizing class ' + name)
p1 = ROOT.std.pair['int','int'](1,2)
p2 = ROOT.std.pair['int','double'](1,2.)
ROOT.gInterpreter.Declare('''
class FirstClass {};
namespace NS {
class SecondClass {};
}
''')
@pythonization('FirstClass')
@pythonization('SecondClass', ns='NS')
def pythonizor_for_first_and_second(klass, name):
print('Executed for class ' + name)
f = ROOT.FirstClass()
s = ROOT.NS.SecondClass()
ROOT.gInterpreter.Declare('''
class MyClass2 {};
''')
o = ROOT.MyClass2()
try:
print(o.pythonized)
except AttributeError:
print("Object has not been pythonized yet!")
@pythonization('MyClass2')
def pythonizor_for_myclass2(klass):
klass.pythonized = True
print(o.pythonized)
This is a MyClass object
Printing new attribute
1
1
First element of integer vector: 1
First element of double vector: 4.0
Pythonizing class std::pair<int,int>
Pythonizing class std::pair<int,double>
Executed for class FirstClass
Executed for class NS::SecondClass
Object has not been pythonized yet!
True