Skip to content

Commit

Permalink
split pythonization callback to predicate and modifcation
Browse files Browse the repository at this point in the history
  • Loading branch information
m-fila committed Apr 24, 2024
1 parent a489371 commit d438c5d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
9 changes: 6 additions & 3 deletions python/podio/pythonizations/collection_subscript.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@ def priority(cls):
return 50

@classmethod
def callback(cls, class_, name):
def filter(cls, class_, name):
return issubclass(class_, cppyy.gbl.podio.CollectionBase)

@classmethod
def modify(cls, class_, name):
def get_item(self, i):
try:
return self.at(i)
except cppyy.gbl.std.out_of_range:
raise IndexError("collection index out of range") from None

if issubclass(class_, cppyy.gbl.podio.CollectionBase):
class_.__getitem__ = get_item
class_.__getitem__ = get_item
42 changes: 37 additions & 5 deletions python/podio/pythonizations/utils/pythonizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,46 @@ class Pythonizer(metaclass=ABCMeta):
@classmethod
@abstractmethod
def priority(cls):
"""Order in which the pythonizations are applied"""
"""Order in which the pythonizations are applied
Returns:
int: Priority
"""

@classmethod
@abstractmethod
def filter(cls, class_, name):
"""
Abstract classmethod to filter classes to which the pythonizations should be applied
Args:
class_ (type): Class object.
name (str): Name of the class.
Returns:
bool: True if class should be pythonized.
"""

@classmethod
@abstractmethod
def callback(cls, class_, name):
"""Pythonization callback"""
def modify(cls, class_, name):
"""Abstract classmethod modifying classes to be pythonized
Args:
class_ (type): Class object.
name (str): Name of the class.
"""

@classmethod
def register(cls, namespace):
"""Helper method to apply the pythonization to the given namespace"""
cppyy.py.add_pythonization(cls.callback, namespace)
"""Helper method to apply the pythonization to the given namespace
Args:
namespace (str): Namespace to by pythonized
"""

def pythonization_callback(class_, name):
if cls.filter(class_, name):
cls.modify(class_, name)

cppyy.py.add_pythonization(pythonization_callback, namespace)

0 comments on commit d438c5d

Please sign in to comment.