From c3037a5ec35b0918afaf3d2d99bf863f20c16eae Mon Sep 17 00:00:00 2001 From: Timo Koch <timo.koch@iws.uni-stuttgart.de> Date: Sat, 24 Jul 2021 06:11:49 +0200 Subject: [PATCH] [python] Add helpers for creating class-like aliases for the code generators --- python/dumux/CMakeLists.txt | 1 + python/dumux/wrapping/CMakeLists.txt | 3 +++ python/dumux/wrapping/__init__.py | 40 ++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+) create mode 100644 python/dumux/wrapping/CMakeLists.txt create mode 100644 python/dumux/wrapping/__init__.py diff --git a/python/dumux/CMakeLists.txt b/python/dumux/CMakeLists.txt index db25915644..e45fd12763 100644 --- a/python/dumux/CMakeLists.txt +++ b/python/dumux/CMakeLists.txt @@ -4,6 +4,7 @@ add_subdirectory(discretization) add_subdirectory(material) add_subdirectory(io) add_subdirectory(porousmediumflow) +add_subdirectory(wrapping) add_python_targets(dumux __init__ diff --git a/python/dumux/wrapping/CMakeLists.txt b/python/dumux/wrapping/CMakeLists.txt new file mode 100644 index 0000000000..9b2e59b4e0 --- /dev/null +++ b/python/dumux/wrapping/CMakeLists.txt @@ -0,0 +1,3 @@ +add_python_targets(wrapping + __init__ +) diff --git a/python/dumux/wrapping/__init__.py b/python/dumux/wrapping/__init__.py new file mode 100644 index 0000000000..c32b764f05 --- /dev/null +++ b/python/dumux/wrapping/__init__.py @@ -0,0 +1,40 @@ +"""Helper classes and function related Python <-> C++ interaction""" + +import functools +from typing import Callable, Type + + +def cppWrapperCreator(creator: Callable) -> Callable: + """ + Decorator for creator functions that return a C++ type with Python bindings + resulting from C++ code generation and just-in-time compilation + """ + + def makeCreator(aliasClass: Type) -> Callable: + @functools.wraps(creator) + def _makeCreator(*args, **kwargs): + return creator(*args, **kwargs) + + # make the creator assume the name of the alias class + _makeCreator.__name__ = aliasClass.__name__ + return _makeCreator + + return makeCreator + + +def cppWrapperClassAlias(creator: Callable) -> Callable: + """ + Decorator for a class alias corresponding to a creator function + This makes the creator function appear like a class constructor + + Args: + creator (Callable): The corresponding creator function the + decorated class is an alias for + Returns: + Callable: The creator function with the alias name + """ + + def makeCreator(aliasClass: Type) -> Callable: + return creator(aliasClass) + + return makeCreator -- GitLab