Skip to content

Commit d426148

Browse files
authored
fix: Modernize expression_lib (#111)
1 parent 7a353c3 commit d426148

File tree

9 files changed

+87
-99
lines changed

9 files changed

+87
-99
lines changed

README.md

+13-9
Original file line numberDiff line numberDiff line change
@@ -56,20 +56,24 @@ fn pig_latinnify(inputs: &[Series], kwargs: PigLatinKwargs) -> PolarsResult<Seri
5656
This can then be exposed on the Python side:
5757

5858
```python
59+
from __future__ import annotations
60+
61+
from typing import TYPE_CHECKING
62+
5963
import polars as pl
60-
from polars.type_aliases import IntoExpr
61-
from polars.utils.udfs import _get_shared_lib_location
64+
from polars.plugins import register_plugin_function
6265

63-
from expression_lib.utils import parse_into_expr
66+
from expression_lib._utils import LIB
6467

65-
lib = _get_shared_lib_location(__file__)
68+
if TYPE_CHECKING:
69+
from expression_lib._typing import IntoExprColumn
6670

6771

68-
def pig_latinnify(expr: IntoExpr, capitalize: bool = False) -> pl.Expr:
69-
expr = parse_into_expr(expr)
70-
return expr.register_plugin(
71-
lib=lib,
72-
symbol="pig_latinnify",
72+
def pig_latinnify(expr: IntoExprColumn, capitalize: bool = False) -> pl.Expr:
73+
return register_plugin_function(
74+
plugin_path=LIB,
75+
args=[expr],
76+
function_name="pig_latinnify",
7377
is_elementwise=True,
7478
kwargs={"capitalize": capitalize},
7579
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from typing import TYPE_CHECKING, Union
2+
3+
if TYPE_CHECKING:
4+
import sys
5+
6+
import polars as pl
7+
8+
if sys.version_info >= (3, 10):
9+
from typing import TypeAlias
10+
else:
11+
from typing_extensions import TypeAlias
12+
13+
IntoExprColumn: TypeAlias = Union[pl.Expr, str, pl.Series]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from pathlib import Path
2+
3+
LIB = Path(__file__).parent
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
1+
from __future__ import annotations
2+
3+
from typing import TYPE_CHECKING
4+
15
import polars as pl
2-
from polars.type_aliases import IntoExpr
36
from polars.plugins import register_plugin_function
4-
from pathlib import Path
57

6-
from expression_lib.utils import parse_into_expr
8+
from expression_lib._utils import LIB
79

10+
if TYPE_CHECKING:
11+
from expression_lib._typing import IntoExprColumn
812

9-
def is_leap_year(expr: IntoExpr) -> pl.Expr:
10-
expr = parse_into_expr(expr)
13+
14+
def is_leap_year(expr: IntoExprColumn) -> pl.Expr:
1115
return register_plugin_function(
12-
plugin_path=Path(__file__).parent,
16+
plugin_path=LIB,
1317
args=[expr],
1418
function_name="is_leap_year",
1519
is_elementwise=True,
@@ -18,10 +22,11 @@ def is_leap_year(expr: IntoExpr) -> pl.Expr:
1822

1923
# Note that this already exists in Polars. It is just for explanatory
2024
# purposes.
21-
def change_time_zone(expr: IntoExpr, tz: str = "Europe/Amsterdam") -> pl.Expr:
22-
expr = parse_into_expr(expr)
25+
def change_time_zone(expr: IntoExprColumn, tz: str = "Europe/Amsterdam") -> pl.Expr:
2326
return register_plugin_function(
24-
plugin_path=Path(__file__).parent,
27+
plugin_path=LIB,
2528
args=[expr],
26-
function_name="change_time_zone", is_elementwise=True, kwargs={"tz": tz}
29+
function_name="change_time_zone",
30+
is_elementwise=True,
31+
kwargs={"tz": tz},
2732
)

example/derive_expression/expression_lib/expression_lib/dist.py

+17-15
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,42 @@
1+
from __future__ import annotations
2+
3+
from typing import TYPE_CHECKING
4+
15
import polars as pl
2-
from polars.type_aliases import IntoExpr
36
from polars.plugins import register_plugin_function
4-
from pathlib import Path
57

8+
from expression_lib._utils import LIB
9+
10+
if TYPE_CHECKING:
11+
from expression_lib._typing import IntoExprColumn
612

7-
from expression_lib.utils import parse_into_expr
813

9-
def hamming_distance(expr: IntoExpr, other: IntoExpr) -> pl.Expr:
10-
expr = parse_into_expr(expr)
14+
def hamming_distance(expr: IntoExprColumn, other: IntoExprColumn) -> pl.Expr:
1115
return register_plugin_function(
12-
plugin_path=Path(__file__).parent,
16+
plugin_path=LIB,
1317
args=[expr, other],
1418
function_name="hamming_distance",
1519
is_elementwise=True,
1620
)
1721

1822

19-
def jaccard_similarity(expr: IntoExpr, other: IntoExpr) -> pl.Expr:
20-
expr = parse_into_expr(expr)
23+
def jaccard_similarity(expr: IntoExprColumn, other: IntoExprColumn) -> pl.Expr:
2124
return register_plugin_function(
22-
plugin_path=Path(__file__).parent,
25+
plugin_path=LIB,
2326
args=[expr, other],
2427
function_name="jaccard_similarity",
2528
is_elementwise=True,
2629
)
2730

2831

2932
def haversine(
30-
start_lat: IntoExpr,
31-
start_long: IntoExpr,
32-
end_lat: IntoExpr,
33-
end_long: IntoExpr,
33+
start_lat: IntoExprColumn,
34+
start_long: IntoExprColumn,
35+
end_lat: IntoExprColumn,
36+
end_long: IntoExprColumn,
3437
) -> pl.Expr:
35-
start_lat = parse_into_expr(start_lat)
3638
return register_plugin_function(
37-
plugin_path=Path(__file__).parent,
39+
plugin_path=LIB,
3840
args=[start_lat, start_long, end_lat, end_long],
3941
function_name="haversine",
4042
is_elementwise=True,

example/derive_expression/expression_lib/expression_lib/extension.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,14 @@
1616
- static typing will not recognise your custom namespace. Errors such
1717
as `"Expr" has no attribute "dist" [attr-defined]`.
1818
"""
19+
1920
from __future__ import annotations
2021

21-
import polars as pl
2222
from typing import Any, Callable
23-
from expression_lib import date_util, dist, language, utils, panic
23+
24+
import polars as pl
25+
26+
from expression_lib import date_util, dist, language, panic
2427

2528

2629
@pl.api.register_expr_namespace("language")

example/derive_expression/expression_lib/expression_lib/language.py

+11-9
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
1+
from __future__ import annotations
2+
3+
from typing import TYPE_CHECKING
4+
15
import polars as pl
2-
from polars.type_aliases import IntoExpr
36
from polars.plugins import register_plugin_function
4-
from pathlib import Path
57

6-
from expression_lib.utils import parse_into_expr
8+
from expression_lib._utils import LIB
79

10+
if TYPE_CHECKING:
11+
from expression_lib._typing import IntoExprColumn
812

913

10-
def pig_latinnify(expr: IntoExpr, capitalize: bool = False) -> pl.Expr:
11-
expr = parse_into_expr(expr)
14+
def pig_latinnify(expr: IntoExprColumn, capitalize: bool = False) -> pl.Expr:
1215
return register_plugin_function(
13-
plugin_path=Path(__file__).parent,
16+
plugin_path=LIB,
1417
args=[expr],
1518
function_name="pig_latinnify",
1619
is_elementwise=True,
@@ -19,7 +22,7 @@ def pig_latinnify(expr: IntoExpr, capitalize: bool = False) -> pl.Expr:
1922

2023

2124
def append_args(
22-
expr: IntoExpr,
25+
expr: IntoExprColumn,
2326
float_arg: float,
2427
integer_arg: int,
2528
string_arg: str,
@@ -28,9 +31,8 @@ def append_args(
2831
"""
2932
This example shows how arguments other than `Series` can be used.
3033
"""
31-
expr = parse_into_expr(expr)
3234
return register_plugin_function(
33-
plugin_path=Path(__file__).parent,
35+
plugin_path=LIB,
3436
args=[expr],
3537
kwargs={
3638
"float_arg": float_arg,
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
1+
from __future__ import annotations
2+
3+
from typing import TYPE_CHECKING
4+
15
import polars as pl
2-
from polars.type_aliases import IntoExpr
36
from polars.plugins import register_plugin_function
47

5-
from expression_lib.utils import parse_into_expr
6-
from pathlib import Path
8+
from expression_lib._utils import LIB
9+
10+
if TYPE_CHECKING:
11+
from expression_lib._typing import IntoExprColumn
712

813

9-
def panic(expr: IntoExpr) -> pl.Expr:
10-
expr = parse_into_expr(expr)
14+
def panic(expr: IntoExprColumn) -> pl.Expr:
1115
return register_plugin_function(
12-
plugin_path=Path(__file__).parent,
16+
plugin_path=LIB,
1317
args=[expr],
1418
function_name="panic",
1519
)

example/derive_expression/expression_lib/expression_lib/utils.py

-48
This file was deleted.

0 commit comments

Comments
 (0)