Skip to content

Commit

Permalink
update for new versions
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoGorelli committed Jan 26, 2024
1 parent 79499f5 commit ed74282
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 51 deletions.
64 changes: 27 additions & 37 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ crate-type= ["cdylib"]

[dependencies]
pyo3 = { version = "0.20.0", features = ["extension-module"] }
pyo3-polars = { version = "0.10.0", features = ["derive"] }
pyo3-polars = { version = "0.11.1", features = ["derive"] }
serde = { version = "1", features = ["derive"] }
polars = { version = "0.36.2", default-features = false }
rust-stemmers = "1.2.0"
polars = { version = "0.37.0", default-features = false }
# rust-stemmers = "1.2.0"

[target.'cfg(target_os = "linux")'.dependencies]
jemallocator = { version = "0.5", features = ["disable_initial_exec_tls"] }
2 changes: 2 additions & 0 deletions minimal_plugin/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import polars as pl
from polars.utils.udfs import _get_shared_lib_location
from polars.type_aliases import IntoExpr
from minimal_plugin.utils import parse_into_expr

lib = _get_shared_lib_location(__file__)



def noop(expr: IntoExpr) -> pl.Expr:
expr = parse_into_expr(expr)
return expr.register_plugin(
Expand Down
49 changes: 49 additions & 0 deletions minimal_plugin/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
from __future__ import annotations

from typing import TYPE_CHECKING

import polars as pl

if TYPE_CHECKING:
from polars.type_aliases import IntoExpr, PolarsDataType


def parse_into_expr(
expr: IntoExpr,
*,
str_as_lit: bool = False,
list_as_lit: bool = True,
dtype: PolarsDataType | None = None,
) -> pl.Expr:
"""
Parse a single input into an expression.
Parameters
----------
expr
The input to be parsed as an expression.
str_as_lit
Interpret string input as a string literal. If set to `False` (default),
strings are parsed as column names.
list_as_lit
Interpret list input as a lit literal, If set to `False`,
lists are parsed as `Series` literals.
dtype
If the input is expected to resolve to a literal with a known dtype, pass
this to the `lit` constructor.
Returns
-------
polars.Expr
"""
if isinstance(expr, pl.Expr):
pass
elif isinstance(expr, str) and not str_as_lit:
expr = pl.col(expr)
elif isinstance(expr, list) and not list_as_lit:
expr = pl.lit(pl.Series(expr), dtype=dtype)
else:
expr = pl.lit(expr, dtype=dtype)

return expr

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[build-system]
requires = ["maturin>=1.0,<2.0"]
requires = ["maturin>=1.0,<2.0", "polars>=0.26.0"]
build-backend = "maturin"

[project]
Expand Down
20 changes: 10 additions & 10 deletions src/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,17 +180,17 @@ fn add_suffix(inputs: &[Series], kwargs: AddSuffixKwargs) -> PolarsResult<Series
Ok(out.into_series())
}

use rust_stemmers::{Algorithm, Stemmer};
// use rust_stemmers::{Algorithm, Stemmer};

#[polars_expr(output_type=String)]
fn snowball_stem(inputs: &[Series]) -> PolarsResult<Series> {
let ca: &StringChunked = inputs[0].str()?;
let en_stemmer = Stemmer::create(Algorithm::English);
let out: StringChunked = ca.apply_to_buffer(|value: &str, output: &mut String| {
write!(output, "{}", en_stemmer.stem(value)).unwrap()
});
Ok(out.into_series())
}
// #[polars_expr(output_type=String)]
// fn snowball_stem(inputs: &[Series]) -> PolarsResult<Series> {
// let ca: &StringChunked = inputs[0].str()?;
// let en_stemmer = Stemmer::create(Algorithm::English);
// let out: StringChunked = ca.apply_to_buffer(|value: &str, output: &mut String| {
// write!(output, "{}", en_stemmer.stem(value)).unwrap()
// });
// Ok(out.into_series())
// }

#[polars_expr(output_type=Float64)]
fn weighted_mean(inputs: &[Series]) -> PolarsResult<Series> {
Expand Down

0 comments on commit ed74282

Please sign in to comment.