From 14660053e91a9d5813976175df333f59554a084b Mon Sep 17 00:00:00 2001 From: pwwang <1188067+pwwang@users.noreply.github.com> Date: Thu, 8 Dec 2022 09:30:01 -0700 Subject: [PATCH] 0.11.0 (#44) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 💥 Simplify register_expr_array_func and rename it to register_array_ufunc * 🔖 0.11.0 * 📝 [0.11.0] Update docs * 👷 [0.11.0] Use v3 actions * 👷 [0.11.0] Use v3 actions --- .github/workflows/build.yml | 10 +++++----- docs/CHANGELOG.md | 4 ++++ docs/expressions.md | 11 ++++------- pipda/__init__.py | 4 ++-- pipda/expression.py | 30 ++++++++++++++++-------------- pyproject.toml | 2 +- tests/test_expression.py | 15 ++++++--------- 7 files changed, 38 insertions(+), 38 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 15b731d..0ac8a02 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -14,9 +14,9 @@ jobs: python-version: [3.7, 3.8, 3.9, "3.10"] steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: Setup Python # Set Python version - uses: actions/setup-python@v2 + uses: actions/setup-python@v3 with: python-version: ${{ matrix.python-version }} - name: Install dependencies @@ -35,7 +35,7 @@ jobs: - name: Test with pytest run: poetry run pytest tests/ --junitxml=junit/test-results-${{ matrix.python-version }}.xml - name: Upload pytest test results - uses: actions/upload-artifact@v2 + uses: actions/upload-artifact@v3 with: name: pytest-results-${{ matrix.python-version }} path: junit/test-results-${{ matrix.python-version }}.xml @@ -56,9 +56,9 @@ jobs: matrix: python-version: [3.9] steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: Setup Python # Set Python version - uses: actions/setup-python@v2 + uses: actions/setup-python@v3 - name: Install dependencies run: | python -m pip install --upgrade pip diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index e08d2b2..d7b81f8 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -1,5 +1,9 @@ # Change Log +## 0.11.0 + +- 💥 Simplify `register_expr_array_func` and rename it to `register_array_ufunc` + ## 0.10.0 - 💥 Refactor the registered borrowed from `singledispatch` diff --git a/docs/expressions.md b/docs/expressions.md index 960bab4..29d7594 100644 --- a/docs/expressions.md +++ b/docs/expressions.md @@ -39,15 +39,12 @@ x._pipda_eval(4) # 2.0 ```python import numpy as np -from pipda import Symbolic, register_expr_array_ufunc +from pipda import Symbolic, register_array_ufunc - @register_expr_array_func - def my_ufunc(expr, ufunc, method, *inputs, **kwargs): - if method != "__call__": - ufunc = getattr(ufunc, method) - fun = Function(lambda x: ufunc(x) * 2, None, {}) - return FunctionCall(fun, *inputs, **kwargs) + @register_array_ufunc + def my_ufunc(ufunc, x, *args, **kwargs): + return ufunc(x, *args, **kwargs) * 2 f = Symbolic() x = np.sqrt(f) diff --git a/pipda/__init__.py b/pipda/__init__.py index 5f249f5..4745742 100644 --- a/pipda/__init__.py +++ b/pipda/__init__.py @@ -1,5 +1,5 @@ from .context import Context, ContextBase -from .expression import Expression, register_expr_array_func +from .expression import Expression, register_array_ufunc from .function import FunctionCall, register_func from .operator import Operator, OperatorCall, register_operator from .reference import ReferenceAttr, ReferenceItem @@ -8,7 +8,7 @@ from .verb import VerbCall, register_verb from .piping import register_piping, _patch_default_classes -__version__ = "0.10.0" +__version__ = "0.11.0" register_piping(">>") _patch_default_classes() diff --git a/pipda/expression.py b/pipda/expression.py index ca1c60d..ea0b112 100644 --- a/pipda/expression.py +++ b/pipda/expression.py @@ -57,20 +57,14 @@ class Expression(ABC): _pipda_operator = None - def _pipda_array_func( - self, + def _pipda_array_ufunc( ufunc: Callable, - method: str, - *inputs: Any, + x: Any, + *args: Any, **kwargs: Any, ) -> FunctionCall: """Allow numpy array function to work on Expression objects""" - from .function import FunctionCall - - if method != "__call__": - ufunc = getattr(ufunc, method) - - return FunctionCall(ufunc, *inputs, **kwargs) + return ufunc(x, *args, **kwargs) def __array_ufunc__( self, @@ -80,7 +74,7 @@ def __array_ufunc__( **kwargs: Any, ) -> FunctionCall: """Allow numpy ufunc to work on Expression objects""" - + from .function import FunctionCall from .piping import PIPING_OPS, PipeableCall if ( @@ -95,7 +89,15 @@ def __array_ufunc__( # work return inputs[1]._pipda_eval(inputs[0]) - return self._pipda_array_func(ufunc, method, *inputs, **kwargs) + if method != "__call__": + ufunc = getattr(ufunc, method) + + return FunctionCall( + self.__class__._pipda_array_ufunc, + ufunc, + *inputs, + **kwargs, + ) def __hash__(self) -> int: """Make it hashable""" @@ -207,7 +209,7 @@ def _pipda_eval( """Evaluate the expression using given data""" -def register_expr_array_func(func: Callable) -> Callable: +def register_array_ufunc(func: Callable) -> Callable: """Register a function to be used as __array_ufunc__ on Expression""" - Expression._pipda_array_func = func # type: ignore + Expression._pipda_array_ufunc = func # type: ignore return func diff --git a/pyproject.toml b/pyproject.toml index b96ff88..34ea569 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "pipda" -version = "0.10.0" +version = "0.11.0" readme = "README.md" description = "A framework for data piping in python" authors = ["pwwang "] diff --git a/tests/test_expression.py b/tests/test_expression.py index 09f2dd2..e5f4b20 100644 --- a/tests/test_expression.py +++ b/tests/test_expression.py @@ -2,7 +2,7 @@ import numpy as np from pipda.context import Context -from pipda.expression import Expression, register_expr_array_func +from pipda.expression import Expression, register_array_ufunc from pipda.function import FunctionCall from pipda.reference import ReferenceAttr, ReferenceItem from pipda.symbolic import Symbolic @@ -147,14 +147,11 @@ def test_ufunc(): def test_register_ufunc(): - old_ufunc = Expression._pipda_array_func + old_ufunc = Expression._pipda_array_ufunc - @register_expr_array_func - def my_ufunc(expr, ufunc, method, *inputs, **kwargs): - if method != "__call__": - ufunc = getattr(ufunc, method) - - return FunctionCall(lambda x: ufunc(x) * 2, *inputs, **kwargs) + @register_array_ufunc + def my_ufunc(ufunc, x, *args, **kwargs): + return ufunc(x, *args, **kwargs) * 2 f = Symbolic() x = np.sqrt(f) @@ -163,4 +160,4 @@ def my_ufunc(expr, ufunc, method, *inputs, **kwargs): out = x._pipda_eval(4, Context.EVAL) assert out == 4 - register_expr_array_func(old_ufunc) + register_array_ufunc(old_ufunc)