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)
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t format
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