-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add `py_udf` operator (#792) feature-gates `py_udf` behind `python` feature, does not run in wasm tests * feat: Minor extension to `py_udf` to pass multiple arguments (#792) --------- Co-authored-by: Tiemo Bang <[email protected]>
- Loading branch information
1 parent
6323980
commit 7dbd5e2
Showing
14 changed files
with
363 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
use hydroflow_macro::hydroflow_syntax; | ||
use pyo3::{Py, PyAny, PyResult, Python}; | ||
|
||
#[hydroflow::main] | ||
async fn main() { | ||
eprintln!("Vec sender starting..."); | ||
|
||
let v = vec![1, 2, 3, 4, 5]; | ||
|
||
let mut df = hydroflow_syntax! { | ||
source_iter(v) -> inspect( | ||
|x| println!("input:\t{:?}", x) | ||
) | ||
// Map to tuples | ||
-> map(|x| (x, 1)) | ||
-> py_udf(r#" | ||
def add(a, b): | ||
return a + 1 | ||
"#, "add") | ||
-> map(|x: PyResult<Py<PyAny>>| -> i32 {Python::with_gil(|py| { | ||
x.unwrap().extract(py).unwrap() | ||
})}) | ||
-> for_each(|x| println!("output:\t{:?}", x)); | ||
}; | ||
|
||
df.run_available(); | ||
} |
21 changes: 21 additions & 0 deletions
21
hydroflow/tests/snapshots/surface_python__python_basic@graphvis_dot.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
--- | ||
source: hydroflow/tests/surface_python.rs | ||
expression: hf.meta_graph().unwrap().to_dot() | ||
--- | ||
digraph { | ||
subgraph "cluster n1v1" { | ||
fillcolor="#dddddd" | ||
style=filled | ||
label = "sg_1v1\nstratum 0" | ||
n1v1 [label="(n1v1) source_iter(0..10)", fontname=Monaco, shape=invhouse, style = filled, color = "#0022ff", fontcolor = "#ffffff"] | ||
n2v1 [label="(n2v1) map(|x| (x,))", fontname=Monaco, shape=invhouse, style = filled, color = "#0022ff", fontcolor = "#ffffff"] | ||
n3v1 [label="(n3v1) py_udf(\l r#\"\ldef fib(n):\lif n < 2:\l return n\lelse:\l return fib(n - 2) + fib(n - 1)\l \"#,\l \"fib\",\l)\l", fontname=Monaco, shape=invhouse, style = filled, color = "#0022ff", fontcolor = "#ffffff"] | ||
n4v1 [label="(n4v1) map(|x: PyResult<Py<PyAny>>| Python::with_gil(|py| {\l usize::extract(x.unwrap().as_ref(py)).unwrap()\l}))\l", fontname=Monaco, shape=invhouse, style = filled, color = "#0022ff", fontcolor = "#ffffff"] | ||
n5v1 [label="(n5v1) assert([0, 1, 1, 2, 3, 5, 8, 13, 21, 34])", fontname=Monaco, shape=house, style = filled, color = "#ffff00"] | ||
n1v1 -> n2v1 | ||
n2v1 -> n3v1 | ||
n3v1 -> n4v1 | ||
n4v1 -> n5v1 | ||
} | ||
} | ||
|
21 changes: 21 additions & 0 deletions
21
hydroflow/tests/snapshots/surface_python__python_basic@graphvis_mermaid.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
--- | ||
source: hydroflow/tests/surface_python.rs | ||
expression: hf.meta_graph().unwrap().to_mermaid() | ||
--- | ||
%%{init:{'theme':'base','themeVariables':{'clusterBkg':'#ddd','clusterBorder':'#888'}}}%% | ||
flowchart TD | ||
classDef pullClass fill:#8af,stroke:#000,text-align:left,white-space:pre | ||
classDef pushClass fill:#ff8,stroke:#000,text-align:left,white-space:pre | ||
linkStyle default stroke:#aaa,stroke-width:4px,color:red,font-size:1.5em; | ||
subgraph sg_1v1 ["sg_1v1 stratum 0"] | ||
1v1[\"(1v1) <code>source_iter(0..10)</code>"/]:::pullClass | ||
2v1[\"(2v1) <code>map(|x| (x,))</code>"/]:::pullClass | ||
3v1[\"<div style=text-align:center>(3v1)</div> <code>py_udf(<br> r#"<br>def fib(n):<br>if n < 2:<br> return n<br>else:<br> return fib(n - 2) + fib(n - 1)<br> "#,<br> "fib",<br>)</code>"/]:::pullClass | ||
4v1[\"<div style=text-align:center>(4v1)</div> <code>map(|x: PyResult<Py<PyAny>>| Python::with_gil(|py| {<br> usize::extract(x.unwrap().as_ref(py)).unwrap()<br>}))</code>"/]:::pullClass | ||
5v1[/"(5v1) <code>assert([0, 1, 1, 2, 3, 5, 8, 13, 21, 34])</code>"\]:::pushClass | ||
1v1--->2v1 | ||
2v1--->3v1 | ||
3v1--->4v1 | ||
4v1--->5v1 | ||
end | ||
|
21 changes: 21 additions & 0 deletions
21
hydroflow/tests/snapshots/surface_python__python_too_many_args@graphvis_dot.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
--- | ||
source: hydroflow/tests/surface_python.rs | ||
expression: hf.meta_graph().unwrap().to_dot() | ||
--- | ||
digraph { | ||
subgraph "cluster n1v1" { | ||
fillcolor="#dddddd" | ||
style=filled | ||
label = "sg_1v1\nstratum 0" | ||
n1v1 [label="(n1v1) source_iter([(5,)])", fontname=Monaco, shape=invhouse, style = filled, color = "#0022ff", fontcolor = "#ffffff"] | ||
n2v1 [label="(n2v1) py_udf(r#\"\ldef add(a, b):\lreturn a + b\l \"#, \"add\")\l", fontname=Monaco, shape=invhouse, style = filled, color = "#0022ff", fontcolor = "#ffffff"] | ||
n3v1 [label="(n3v1) map(PyResult::<Py<PyAny>>::unwrap_err)", fontname=Monaco, shape=invhouse, style = filled, color = "#0022ff", fontcolor = "#ffffff"] | ||
n4v1 [label="(n4v1) map(|py_err| py_err.to_string())", fontname=Monaco, shape=invhouse, style = filled, color = "#0022ff", fontcolor = "#ffffff"] | ||
n5v1 [label="(n5v1) assert([\"TypeError: add() missing 1 required positional argument: 'b'\"])", fontname=Monaco, shape=house, style = filled, color = "#ffff00"] | ||
n1v1 -> n2v1 | ||
n2v1 -> n3v1 | ||
n3v1 -> n4v1 | ||
n4v1 -> n5v1 | ||
} | ||
} | ||
|
21 changes: 21 additions & 0 deletions
21
hydroflow/tests/snapshots/surface_python__python_too_many_args@graphvis_mermaid.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
--- | ||
source: hydroflow/tests/surface_python.rs | ||
expression: hf.meta_graph().unwrap().to_mermaid() | ||
--- | ||
%%{init:{'theme':'base','themeVariables':{'clusterBkg':'#ddd','clusterBorder':'#888'}}}%% | ||
flowchart TD | ||
classDef pullClass fill:#8af,stroke:#000,text-align:left,white-space:pre | ||
classDef pushClass fill:#ff8,stroke:#000,text-align:left,white-space:pre | ||
linkStyle default stroke:#aaa,stroke-width:4px,color:red,font-size:1.5em; | ||
subgraph sg_1v1 ["sg_1v1 stratum 0"] | ||
1v1[\"(1v1) <code>source_iter([(5,)])</code>"/]:::pullClass | ||
2v1[\"<div style=text-align:center>(2v1)</div> <code>py_udf(r#"<br>def add(a, b):<br>return a + b<br> "#, "add")</code>"/]:::pullClass | ||
3v1[\"(3v1) <code>map(PyResult::<Py<PyAny>>::unwrap_err)</code>"/]:::pullClass | ||
4v1[\"(4v1) <code>map(|py_err| py_err.to_string())</code>"/]:::pullClass | ||
5v1[/"(5v1) <code>assert(["TypeError: add() missing 1 required positional argument: 'b'"])</code>"\]:::pushClass | ||
1v1--->2v1 | ||
2v1--->3v1 | ||
3v1--->4v1 | ||
4v1--->5v1 | ||
end | ||
|
19 changes: 19 additions & 0 deletions
19
hydroflow/tests/snapshots/surface_python__python_two_args@graphvis_dot.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
--- | ||
source: hydroflow/tests/surface_python.rs | ||
expression: hf.meta_graph().unwrap().to_dot() | ||
--- | ||
digraph { | ||
subgraph "cluster n1v1" { | ||
fillcolor="#dddddd" | ||
style=filled | ||
label = "sg_1v1\nstratum 0" | ||
n1v1 [label="(n1v1) source_iter([(5, 1)])", fontname=Monaco, shape=invhouse, style = filled, color = "#0022ff", fontcolor = "#ffffff"] | ||
n2v1 [label="(n2v1) py_udf(r#\"\ldef add(a, b):\lreturn a + b\l \"#, \"add\")\l", fontname=Monaco, shape=invhouse, style = filled, color = "#0022ff", fontcolor = "#ffffff"] | ||
n3v1 [label="(n3v1) map(|x: PyResult<Py<PyAny>>| Python::with_gil(|py| {\l usize::extract(x.unwrap().as_ref(py)).unwrap()\l}))\l", fontname=Monaco, shape=invhouse, style = filled, color = "#0022ff", fontcolor = "#ffffff"] | ||
n4v1 [label="(n4v1) assert([6])", fontname=Monaco, shape=house, style = filled, color = "#ffff00"] | ||
n1v1 -> n2v1 | ||
n2v1 -> n3v1 | ||
n3v1 -> n4v1 | ||
} | ||
} | ||
|
19 changes: 19 additions & 0 deletions
19
hydroflow/tests/snapshots/surface_python__python_two_args@graphvis_mermaid.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
--- | ||
source: hydroflow/tests/surface_python.rs | ||
expression: hf.meta_graph().unwrap().to_mermaid() | ||
--- | ||
%%{init:{'theme':'base','themeVariables':{'clusterBkg':'#ddd','clusterBorder':'#888'}}}%% | ||
flowchart TD | ||
classDef pullClass fill:#8af,stroke:#000,text-align:left,white-space:pre | ||
classDef pushClass fill:#ff8,stroke:#000,text-align:left,white-space:pre | ||
linkStyle default stroke:#aaa,stroke-width:4px,color:red,font-size:1.5em; | ||
subgraph sg_1v1 ["sg_1v1 stratum 0"] | ||
1v1[\"(1v1) <code>source_iter([(5, 1)])</code>"/]:::pullClass | ||
2v1[\"<div style=text-align:center>(2v1)</div> <code>py_udf(r#"<br>def add(a, b):<br>return a + b<br> "#, "add")</code>"/]:::pullClass | ||
3v1[\"<div style=text-align:center>(3v1)</div> <code>map(|x: PyResult<Py<PyAny>>| Python::with_gil(|py| {<br> usize::extract(x.unwrap().as_ref(py)).unwrap()<br>}))</code>"/]:::pullClass | ||
4v1[/"(4v1) <code>assert([6])</code>"\]:::pushClass | ||
1v1--->2v1 | ||
2v1--->3v1 | ||
3v1--->4v1 | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#![cfg(feature = "python")] | ||
|
||
use hydroflow::{assert_graphvis_snapshots, hydroflow_syntax}; | ||
use multiplatform_test::multiplatform_test; | ||
use pyo3::prelude::*; | ||
|
||
#[multiplatform_test(test)] | ||
pub fn test_python_basic() { | ||
let mut hf = hydroflow_syntax! { | ||
source_iter(0..10) | ||
-> map(|x| (x,)) | ||
-> py_udf(r#" | ||
def fib(n): | ||
if n < 2: | ||
return n | ||
else: | ||
return fib(n - 2) + fib(n - 1) | ||
"#, "fib") | ||
-> map(|x: PyResult<Py<PyAny>>| Python::with_gil(|py| { | ||
usize::extract(x.unwrap().as_ref(py)).unwrap() | ||
})) | ||
-> assert([0, 1, 1, 2, 3, 5, 8, 13, 21, 34]); | ||
}; | ||
assert_graphvis_snapshots!(hf); | ||
|
||
hf.run_available(); | ||
} | ||
|
||
#[multiplatform_test(test)] | ||
pub fn test_python_too_many_args() { | ||
let mut hf = hydroflow_syntax! { | ||
source_iter([(5,)]) | ||
-> py_udf(r#" | ||
def add(a, b): | ||
return a + b | ||
"#, "add") | ||
-> map(PyResult::<Py<PyAny>>::unwrap_err) | ||
-> map(|py_err| py_err.to_string()) | ||
-> assert(["TypeError: add() missing 1 required positional argument: 'b'"]); | ||
}; | ||
assert_graphvis_snapshots!(hf); | ||
|
||
hf.run_available(); | ||
} | ||
|
||
#[multiplatform_test(test)] | ||
pub fn test_python_two_args() { | ||
let mut hf = hydroflow_syntax! { | ||
source_iter([(5,1)]) | ||
-> py_udf(r#" | ||
def add(a, b): | ||
return a + b | ||
"#, "add") | ||
-> map(|x: PyResult<Py<PyAny>>| Python::with_gil(|py| { | ||
usize::extract(x.unwrap().as_ref(py)).unwrap() | ||
})) | ||
-> assert([6]); | ||
}; | ||
assert_graphvis_snapshots!(hf); | ||
|
||
hf.run_available(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.