Skip to content

Commit 433766a

Browse files
Copilotpwwang
andcommitted
Move pipe() from datar/apis/dplyr.py to datar/misc.py
Co-authored-by: pwwang <[email protected]>
1 parent 9593194 commit 433766a

File tree

2 files changed

+44
-40
lines changed

2 files changed

+44
-40
lines changed

datar/apis/dplyr.py

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2478,43 +2478,3 @@ def any_of(_data, x, vars=None) -> Any:
24782478
in `_data` columns
24792479
"""
24802480
raise _NotImplementedByCurrentBackendError("any_of", _data)
2481-
2482-
2483-
@_register_verb(object)
2484-
def pipe(_data: T, func: _Callable, *args, **kwargs) -> Any:
2485-
"""Apply a function to the data
2486-
2487-
This function is similar to pandas.DataFrame.pipe() and allows you to
2488-
apply custom functions in a piping workflow. Works with any data type.
2489-
2490-
Args:
2491-
_data: The data object (can be any type)
2492-
func: Function to apply to the data. ``args`` and ``kwargs`` are
2493-
passed into ``func``.
2494-
*args: Positional arguments passed into ``func``
2495-
**kwargs: Keyword arguments passed into ``func``
2496-
2497-
Returns:
2498-
The return value of ``func``
2499-
2500-
Examples:
2501-
>>> import datar.all as dr
2502-
>>> # Works with lists
2503-
>>> [1, 2, 3] >> dr.pipe(lambda x: [i * 2 for i in x])
2504-
[2, 4, 6]
2505-
2506-
>>> # Works with dicts
2507-
>>> {'a': 1, 'b': 2} >> dr.pipe(lambda x: {k: v * 2 for k, v in x.items()})
2508-
{'a': 2, 'b': 4}
2509-
2510-
>>> # With additional arguments
2511-
>>> def add_value(data, value):
2512-
... return [x + value for x in data]
2513-
>>> [1, 2, 3] >> dr.pipe(add_value, 10)
2514-
[11, 12, 13]
2515-
2516-
>>> # Chain multiple operations
2517-
>>> [1, 2, 3] >> dr.pipe(lambda x: [i * 2 for i in x]) >> dr.pipe(sum)
2518-
12
2519-
"""
2520-
return func(_data, *args, **kwargs)

datar/misc.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,47 @@
1+
from typing import Any, Callable
2+
3+
from pipda import register_verb
4+
15
from .core.load_plugins import plugin as _plugin
26

37
locals().update(_plugin.hooks.misc_api())
8+
9+
10+
@register_verb(object)
11+
def pipe(data: Any, func: Callable, *args, **kwargs) -> Any:
12+
"""Apply a function to the data
13+
14+
This function is similar to pandas.DataFrame.pipe() and allows you to
15+
apply custom functions in a piping workflow. Works with any data type.
16+
17+
Args:
18+
data: The data object (can be any type)
19+
func: Function to apply to the data. ``args`` and ``kwargs`` are
20+
passed into ``func``.
21+
*args: Positional arguments passed into ``func``
22+
**kwargs: Keyword arguments passed into ``func``
23+
24+
Returns:
25+
The return value of ``func``
26+
27+
Examples:
28+
>>> import datar.all as dr
29+
>>> # Works with lists
30+
>>> [1, 2, 3] >> dr.pipe(lambda x: [i * 2 for i in x])
31+
[2, 4, 6]
32+
33+
>>> # Works with dicts
34+
>>> {'a': 1, 'b': 2} >> dr.pipe(lambda x: {k: v * 2 for k, v in x.items()})
35+
{'a': 2, 'b': 4}
36+
37+
>>> # With additional arguments
38+
>>> def add_value(data, value):
39+
... return [x + value for x in data]
40+
>>> [1, 2, 3] >> dr.pipe(add_value, 10)
41+
[11, 12, 13]
42+
43+
>>> # Chain multiple operations
44+
>>> [1, 2, 3] >> dr.pipe(lambda x: [i * 2 for i in x]) >> dr.pipe(sum)
45+
12
46+
"""
47+
return func(data, *args, **kwargs)

0 commit comments

Comments
 (0)