Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
modernize expression_lib
Browse files Browse the repository at this point in the history
MarcoGorelli committed Oct 2, 2024
1 parent 7a353c3 commit f87f5b8
Showing 7 changed files with 57 additions and 88 deletions.
19 changes: 10 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -56,20 +56,21 @@ fn pig_latinnify(inputs: &[Series], kwargs: PigLatinKwargs) -> PolarsResult<Seri
This can then be exposed on the Python side:

```python
from __future__ import annotations
from typing import TYPE_CHECKING
import polars as pl
from polars.type_aliases import IntoExpr
from polars.utils.udfs import _get_shared_lib_location
from polars.plugins import register_plugin_function
from expression_lib._utils import LIB

from expression_lib.utils import parse_into_expr

lib = _get_shared_lib_location(__file__)
if TYPE_CHECKING:
from expression_lib._typing import IntoExpr


def pig_latinnify(expr: IntoExpr, capitalize: bool = False) -> pl.Expr:
expr = parse_into_expr(expr)
return expr.register_plugin(
lib=lib,
symbol="pig_latinnify",
return register_plugin_function(
plugin_path=LIB,
args=[expr],
function_name="pig_latinnify",
is_elementwise=True,
kwargs={"capitalize": capitalize},
)
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
from __future__ import annotations

from typing import TYPE_CHECKING

import polars as pl
from polars.type_aliases import IntoExpr
from polars.plugins import register_plugin_function
from pathlib import Path

from expression_lib.utils import parse_into_expr
from expression_lib._utils import LIB

if TYPE_CHECKING:
from expression_lib._typing import IntoExpr


def is_leap_year(expr: IntoExpr) -> pl.Expr:
expr = parse_into_expr(expr)
return register_plugin_function(
plugin_path=Path(__file__).parent,
plugin_path=LIB,
args=[expr],
function_name="is_leap_year",
is_elementwise=True,
@@ -19,9 +23,10 @@ def is_leap_year(expr: IntoExpr) -> pl.Expr:
# Note that this already exists in Polars. It is just for explanatory
# purposes.
def change_time_zone(expr: IntoExpr, tz: str = "Europe/Amsterdam") -> pl.Expr:
expr = parse_into_expr(expr)
return register_plugin_function(
plugin_path=Path(__file__).parent,
plugin_path=LIB,
args=[expr],
function_name="change_time_zone", is_elementwise=True, kwargs={"tz": tz}
function_name="change_time_zone",
is_elementwise=True,
kwargs={"tz": tz},
)
20 changes: 11 additions & 9 deletions example/derive_expression/expression_lib/expression_lib/dist.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
from __future__ import annotations

from typing import TYPE_CHECKING

import polars as pl
from polars.type_aliases import IntoExpr
from polars.plugins import register_plugin_function
from pathlib import Path

from expression_lib._utils import LIB

if TYPE_CHECKING:
from expression_lib._typing import IntoExpr

from expression_lib.utils import parse_into_expr

def hamming_distance(expr: IntoExpr, other: IntoExpr) -> pl.Expr:
expr = parse_into_expr(expr)
return register_plugin_function(
plugin_path=Path(__file__).parent,
plugin_path=LIB,
args=[expr, other],
function_name="hamming_distance",
is_elementwise=True,
)


def jaccard_similarity(expr: IntoExpr, other: IntoExpr) -> pl.Expr:
expr = parse_into_expr(expr)
return register_plugin_function(
plugin_path=Path(__file__).parent,
plugin_path=LIB,
args=[expr, other],
function_name="jaccard_similarity",
is_elementwise=True,
@@ -32,9 +35,8 @@ def haversine(
end_lat: IntoExpr,
end_long: IntoExpr,
) -> pl.Expr:
start_lat = parse_into_expr(start_lat)
return register_plugin_function(
plugin_path=Path(__file__).parent,
plugin_path=LIB,
args=[start_lat, start_long, end_lat, end_long],
function_name="haversine",
is_elementwise=True,
Original file line number Diff line number Diff line change
@@ -16,11 +16,14 @@
- static typing will not recognise your custom namespace. Errors such
as `"Expr" has no attribute "dist" [attr-defined]`.
"""

from __future__ import annotations

import polars as pl
from typing import Any, Callable
from expression_lib import date_util, dist, language, utils, panic

import polars as pl

from expression_lib import date_util, dist, language, panic


@pl.api.register_expr_namespace("language")
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
from __future__ import annotations

from typing import TYPE_CHECKING

import polars as pl
from polars.type_aliases import IntoExpr
from polars.plugins import register_plugin_function
from pathlib import Path

from expression_lib.utils import parse_into_expr
from expression_lib._utils import LIB

if TYPE_CHECKING:
from expression_lib._typing import IntoExpr


def pig_latinnify(expr: IntoExpr, capitalize: bool = False) -> pl.Expr:
expr = parse_into_expr(expr)
return register_plugin_function(
plugin_path=Path(__file__).parent,
plugin_path=LIB,
args=[expr],
function_name="pig_latinnify",
is_elementwise=True,
@@ -28,9 +31,8 @@ def append_args(
"""
This example shows how arguments other than `Series` can be used.
"""
expr = parse_into_expr(expr)
return register_plugin_function(
plugin_path=Path(__file__).parent,
plugin_path=LIB,
args=[expr],
kwargs={
"float_arg": float_arg,
14 changes: 9 additions & 5 deletions example/derive_expression/expression_lib/expression_lib/panic.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
from __future__ import annotations

from typing import TYPE_CHECKING

import polars as pl
from polars.type_aliases import IntoExpr
from polars.plugins import register_plugin_function

from expression_lib.utils import parse_into_expr
from pathlib import Path
from expression_lib._utils import LIB

if TYPE_CHECKING:
from expression_lib._typing import IntoExpr


def panic(expr: IntoExpr) -> pl.Expr:
expr = parse_into_expr(expr)
return register_plugin_function(
plugin_path=Path(__file__).parent,
plugin_path=LIB,
args=[expr],
function_name="panic",
)
48 changes: 0 additions & 48 deletions example/derive_expression/expression_lib/expression_lib/utils.py

This file was deleted.

0 comments on commit f87f5b8

Please sign in to comment.