Skip to content

Commit 7e5541b

Browse files
committed
add version getter function
1 parent d5ba9af commit 7e5541b

File tree

6 files changed

+83
-5
lines changed

6 files changed

+83
-5
lines changed

Cargo.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "polars-capitol"
3-
version = "0.1.0"
3+
version = "0.1.1"
44
edition = "2021"
55

66
[lib]
@@ -12,5 +12,5 @@ pyo3 = { version = "0.22", features = ["extension-module", "abi3-py38"] }
1212
pyo3-polars = { version = "0.19.0", features = ["derive"] }
1313
serde = { version = "1", features = ["derive"] }
1414
polars = { version = "0.45.1", default-features = false }
15-
capitol = "0.2.0"
15+
capitol = "0.2.1"
1616

polars_capitol/__init__.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,12 @@ def cdg_url(expr: IntoExprColumn) -> pl.Expr:
2121
function_name="cdg_url",
2222
is_elementwise=True,
2323
)
24+
25+
26+
def version(expr: IntoExprColumn) -> pl.Expr:
27+
return register_plugin_function(
28+
args=[expr],
29+
plugin_path=LIB,
30+
function_name="version",
31+
is_elementwise=True,
32+
)

run.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,7 @@
1515
s = pl.col("package_id").str.split(by="-").list.get(1, null_on_oob=True)
1616
expr = cap.cdg_url(s)
1717
result = df.with_columns(cdg_url=expr)
18+
expr = cap.version(s)
19+
result = df.with_columns(version=expr)
1820

1921
print(result)

src/expressions.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,21 @@ fn cdg_url(inputs: &[Series]) -> PolarsResult<Series> {
1414
});
1515
Ok(out.into_series())
1616
}
17+
18+
#[polars_expr(output_type=String)]
19+
fn version(inputs: &[Series]) -> PolarsResult<Series> {
20+
let s = &inputs[0];
21+
let ca = s.str()?;
22+
let out = ca.try_apply_into_string_amortized(|value: &str, output: &mut String| {
23+
let citation = Citation::parse(value).unwrap();
24+
if let Some(version) = citation.version() {
25+
write!(output, "{}", version).unwrap();
26+
Ok(())
27+
} else {
28+
Err(PolarsError::InvalidOperation(
29+
"`version` called on citation without version".into(),
30+
))
31+
}
32+
});
33+
Ok(out?.into_series())
34+
}

tests/test_get_version.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import polars as pl
2+
import polars_capitol as cap
3+
import pytest
4+
5+
6+
def test_version():
7+
df = pl.DataFrame(
8+
{
9+
"package_id": [
10+
"BILLS-118hr8070ih",
11+
]
12+
}
13+
)
14+
s = pl.col("package_id").str.split(by="-").list.get(1, null_on_oob=True)
15+
expr = cap.version(s)
16+
result = df.with_columns(version=expr)
17+
18+
expected_df = pl.DataFrame(
19+
{
20+
"package_id": [
21+
"BILLS-118hr8070ih",
22+
],
23+
"version": [
24+
"ih",
25+
],
26+
}
27+
)
28+
29+
assert result.equals(expected_df)
30+
31+
32+
def test_citation_without_version_raises():
33+
df = pl.DataFrame(
34+
{
35+
"package_id": [
36+
"BILLS-118hr8070",
37+
]
38+
}
39+
)
40+
s = pl.col("package_id").str.split(by="-").list.get(1, null_on_oob=True)
41+
expr = cap.version(s)
42+
43+
with pytest.raises(pl.exceptions.ComputeError) as exception:
44+
df.with_columns(version=expr)
45+
46+
assert (
47+
str(exception.value)
48+
== "the plugin failed with message: `version` called on citation without version"
49+
)

0 commit comments

Comments
 (0)