Skip to content

Commit 0a1687d

Browse files
authored
Merge pull request #51 from edwinvehmaanpera/update-how-to-do-nothing
Update tutorial to reflect new cookiecutter template and Polars version
2 parents 93eeb7a + bbdc552 commit 0a1687d

14 files changed

+58
-72
lines changed

docs/abs.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ we did for `noop`, we'll just change the names. Please add this to
1515
`minimal_plugin/__init__.py`, right below the definition of `noop`:
1616
```python
1717
def abs_i64(expr: IntoExpr) -> pl.Expr:
18-
return register_plugin(
18+
return register_plugin_function(
1919
args=[expr],
20-
lib=lib,
21-
symbol="abs_i64",
20+
plugin_path=LIB,
21+
function_name="abs_i64",
2222
is_elementwise=True,
2323
)
2424
```
@@ -90,10 +90,10 @@ First, add the following definition to `minimal_plugin/__init__.py`:
9090

9191
```python
9292
def abs_numeric(expr: IntoExpr) -> pl.Expr:
93-
return register_plugin(
93+
return register_plugin_function(
9494
args=[expr],
95-
lib=lib,
96-
symbol="abs_numeric",
95+
plugin_path=LIB,
96+
function_name="abs_numeric",
9797
is_elementwise=True,
9898
)
9999
```

docs/aggregate.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ Nothing fancy here:
1919

2020
```python
2121
def vertical_weighted_mean(values: IntoExpr, weights: IntoExpr) -> pl.Expr:
22-
return register_plugin(
22+
return register_plugin_function(
2323
args=[values, weights],
24-
lib=lib,
25-
symbol="vertical_weighted_mean",
24+
plugin_path=LIB,
25+
function_name="vertical_weighted_mean",
2626
is_elementwise=False,
2727
returns_scalar=True,
2828
)

docs/arguments.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ following:
1717

1818
```python
1919
def add_suffix(expr: IntoExpr, *, suffix: str) -> pl.Expr:
20-
return register_plugin(
20+
return register_plugin_function(
2121
args=[expr],
22-
lib=lib,
23-
symbol="add_suffix",
22+
plugin_path=LIB,
23+
function_name="add_suffix",
2424
is_elementwise=True,
2525
kwargs={"suffix": suffix},
2626
)

docs/cum_sum.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ We're going to implement `cum_sum`.
1616
Add this to `minimal_plugin/__init__.py`:
1717
```python
1818
def cum_sum(expr: IntoExpr) -> pl.Expr:
19-
return register_plugin(
19+
return register_plugin_function(
2020
args=[expr],
21-
lib=lib,
22-
symbol="cum_sum",
21+
plugin_path=LIB,
22+
function_name="cum_sum",
2323
is_elementwise=False,
2424
)
2525
```

docs/life_pt1.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ from typing import Iterable, Any
4242

4343
import polars as pl
4444
from polars._typing import IntoExpr
45-
46-
from game_of_life.utils import register_plugin
45+
from polars.plugins import register_plugin_function
4746

4847

4948
# Parse a board from a file or stdin
@@ -161,10 +160,10 @@ def life_step(left: IntoExpr, mid: IntoExpr, right: IntoExpr) -> pl.Expr:
161160
the same column names as the original data frame, so the resulting df will
162161
have the same shape. See how this is done in the `step(df, n)` function.
163162
"""
164-
return register_plugin(
163+
return register_plugin_function(
165164
args=[left, mid, right],
166-
lib=lib,
167-
symbol="life_step",
165+
plugin_path=LIB,
166+
function_name="life_step",
168167
is_elementwise=False,
169168
)
170169
```

docs/lists.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ On the Python side, this'll be similar to `sum_i64`:
4242

4343
```python
4444
def weighted_mean(expr: IntoExpr, weights: IntoExpr) -> pl.Expr:
45-
return register_plugin(
45+
return register_plugin_function(
4646
args=[expr, weights],
47-
lib=lib,
48-
symbol="weighted_mean",
47+
plugin_path=LIB,
48+
function_name="weighted_mean",
4949
is_elementwise=True,
5050
)
5151
```

docs/lists_in_lists_out.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ To finish this off, the Python side will be a bog-standard:
8484

8585
```python
8686
def non_zero_indices(expr: IntoExpr) -> pl.Expr:
87-
return register_plugin(
88-
args=[expr], lib=lib, symbol="non_zero_indices", is_elementwise=True
87+
return register_plugin_function(
88+
args=[expr], plugin_path=LIB, function_name="non_zero_indices", is_elementwise=True
8989
)
9090
```
9191

docs/lost_in_space.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ We'll start easy by dealing with the Python side. Add the following to `minimal_
4848

4949
```python
5050
def reverse_geocode(lat: IntoExpr, long: IntoExpr) -> pl.Expr:
51-
return register_plugin(
52-
args=[lat, long], lib=lib, symbol="reverse_geocode", is_elementwise=True
51+
return register_plugin_function(
52+
args=[lat, long], plugin_path=LIB, function_name="reverse_geocode", is_elementwise=True
5353
)
5454
```
5555

docs/noop.md

Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,18 @@ should look a bit like the following:
1313
├── Cargo.toml
1414
├── minimal_plugin
1515
│   ├── __init__.py
16-
│   └── utils.py
16+
│   └── typing.py
1717
├── pyproject.toml
18-
└── src
19-
├── expressions.rs
20-
├── lib.rs
21-
└── utils.rs
18+
├── run.py
19+
├── src
20+
│ ├── expressions.rs
21+
│ └── lib.rs
22+
└── tests
2223
```
24+
The cookiecutter command you ran earlier set up a Polars plugin project with a
25+
sample function called `pig_latinnify` already implemented. The [Polars Plugins Cookiecutter](https://github.com/MarcoGorelli/cookiecutter-polars-plugins)
26+
helps you quickly start a Polars plugin project, skipping the boilerplate setup.
27+
Check it out for more details!
2328

2429
[Prerequisites]: ../prerequisites/
2530

@@ -31,22 +36,20 @@ Start by adding the following to `minimal_plugin/__init__.py`:
3136

3237
```python
3338
def noop(expr: IntoExpr) -> pl.Expr:
34-
return register_plugin(
39+
return register_plugin_function(
3540
args=[expr],
36-
lib=lib,
37-
symbol="noop",
41+
plugin_path=LIB,
42+
function_name="noop",
3843
is_elementwise=True,
3944
)
4045
```
4146
Let's go through this line-by-line:
4247

4348
- when we compile Rust, it generates a Shared Object file.
44-
The `lib` variable holds its filepath;
49+
The `LIB` variable holds its filepath;
4550
- We'll cover `is_elementwise` in [Yes we SCAN], for now don't pay attention to it;
46-
- We use the utility function `register_plugin`, provided to us by the cookiecutter.
47-
Polars actually has a public [register_plugin_function](https://docs.pola.rs/py-polars/html/reference/plugins.html#polars.plugins.register_plugin_function) utility for this, but it was only introduced in
48-
Polars 0.20.16. The `register_plugin` function we introduce here handles backwards-compatibility
49-
until Polars 0.20.6, so we use that in this tutorial.
51+
- We use the Polars utility function [register_plugin_function](https://docs.pola.rs/py-polars/html/reference/plugins.html#polars.plugins.register_plugin_function) to extend its functionality with our own.
52+
5053

5154
Note that string literals are parsed as expressions, so that if somebody
5255
calls `noop('a')`, it gets interpreted as `noop(pl.col('a'))`.
@@ -103,9 +106,9 @@ and return a (cheap!) clone of it.
103106

104107
## Putting it all together
105108

106-
Right, does this all work? Let's make a Python file `run.py` with which to
107-
test it out. We'll just make a toy dataframe and apply `noop`
108-
to each column!
109+
Right, does this all work? Let's edit the Python file `run.py`,
110+
which we will use for testing. We'll just make a toy dataframe
111+
and apply `noop` to each column!
109112
```python
110113
import polars as pl
111114
import minimal_plugin as mp
@@ -118,22 +121,6 @@ df = pl.DataFrame({
118121
print(df.with_columns(mp.noop(pl.all()).name.suffix('_noop')))
119122
```
120123

121-
Your working directory should now look a bit like this:
122-
123-
```
124-
.
125-
├── Cargo.toml
126-
├── minimal_plugin
127-
│   ├── __init__.py
128-
│   └── utils.py
129-
├── pyproject.toml
130-
├── run.py
131-
└── src
132-
├── expressions.rs
133-
├── lib.rs
134-
└── utils.rs
135-
```
136-
137124
Let's compile! Please run `maturin develop` (or `maturin develop --release` if benchmarking).
138125
You'll need to do this every time you change any of your Rust code.
139126
It may take a while the first time, but subsequent executions will

docs/prerequisites.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ Please navigate to it with `cd minimal_plugin`.
4545

4646
Next, [create a Python3.8+ virtual environment](https://docs.python.org/3/library/venv.html), and install:
4747

48-
- `polars>=0.20.0`
48+
- `polars>=1.3.0`
4949
- `maturin>=1.4.0`
5050

5151
Finally, you'll also need to [install Rust](https://rustup.rs/).

0 commit comments

Comments
 (0)