From c33dc24beac40987bad0cd3a083727a49857166b Mon Sep 17 00:00:00 2001 From: Enric Tejedor Saavedra <enric.tejedor.saavedra@cern.ch> Date: Fri, 22 Jun 2018 14:05:41 +0200 Subject: [PATCH] Use a decorator for pythonizor functions As suggested by Stefan Wunsch, decorators provide a nicer syntax to mark that a function implements some pythonization. --- .../pyroot_experimental/PyROOT/python/ROOT/__init__.py | 8 +++++--- .../PyROOT/python/ROOT/pythonization/_ttree.py | 10 ++++------ 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/bindings/pyroot_experimental/PyROOT/python/ROOT/__init__.py b/bindings/pyroot_experimental/PyROOT/python/ROOT/__init__.py index 960f2a8ee69..e8dfed1355b 100644 --- a/bindings/pyroot_experimental/PyROOT/python/ROOT/__init__.py +++ b/bindings/pyroot_experimental/PyROOT/python/ROOT/__init__.py @@ -5,12 +5,14 @@ import ROOT.pythonization as pyz import pkgutil import importlib -# Add pythonizations +# Pythonizor decorator to be used in pythonization modules +def pythonization(fn): + cppyy.py.add_pythonization(fn) + +# Trigger the addition of the pythonizations for _, module_name, _ in pkgutil.walk_packages(pyz.__path__): module = importlib.import_module(pyz.__name__ + '.' + module_name) - cppyy.py.add_pythonization(module.get_pythonizor()) # Redirect ROOT to cppyy.gbl import sys sys.modules['ROOT'] = cppyy.gbl - diff --git a/bindings/pyroot_experimental/PyROOT/python/ROOT/pythonization/_ttree.py b/bindings/pyroot_experimental/PyROOT/python/ROOT/pythonization/_ttree.py index a5fa41171c5..3b3d3e1ea59 100644 --- a/bindings/pyroot_experimental/PyROOT/python/ROOT/pythonization/_ttree.py +++ b/bindings/pyroot_experimental/PyROOT/python/ROOT/pythonization/_ttree.py @@ -1,6 +1,8 @@ from libROOTPython import PythonizeTTree +from ROOT import pythonization + # TTree iterator def _TTree__iter__(self): i = 0 @@ -14,7 +16,8 @@ def _TTree__iter__(self): raise RuntimeError( "TTree I/O error" ) # Pythonizor function -def ttree_pythonizor(klass, name): +@pythonization +def pythonize_ttree(klass, name): if name == 'TTree': # Pythonic iterator klass.__iter__ = _TTree__iter__ @@ -24,8 +27,3 @@ def ttree_pythonizor(klass, name): PythonizeTTree(klass) return True - - -def get_pythonizor(): - return ttree_pythonizor - -- GitLab