Skip to content

Commit

Permalink
0.11.0 (#44)
Browse files Browse the repository at this point in the history
* πŸ’₯ 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
  • Loading branch information
pwwang authored Dec 8, 2022
1 parent 279b628 commit 1466005
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 38 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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`
Expand Down
11 changes: 4 additions & 7 deletions docs/expressions.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions pipda/__init__.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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()
30 changes: 16 additions & 14 deletions pipda/expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 (
Expand All @@ -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"""
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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 <pwwang@pwwang.com>"]
Expand Down
15 changes: 6 additions & 9 deletions tests/test_expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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)

0 comments on commit 1466005

Please sign in to comment.