Skip to content

Commit 2403f3e

Browse files
authored
Merge pull request #17 from ctdunc/chore/1.1_release
Upgrade compatibility with polars 1.*
2 parents b20be36 + 69cd063 commit 2403f3e

File tree

7 files changed

+139
-436
lines changed

7 files changed

+139
-436
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ build-backend = "maturin"
55
[project]
66
name = "polars_istr"
77
requires-python = ">=3.8"
8-
version = "0.1.0"
8+
version = "0.1.1"
99

1010
license = {file = "LICENSE.txt"}
1111
classifiers = [

python/polars_istr/_utils.py

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,31 @@
1+
import os
2+
import re
3+
from pathlib import Path
4+
from typing import Any, Dict, List, Optional, Union
5+
16
import polars as pl
2-
from typing import Any, Optional, List, Dict
7+
38
from .type_alias import StrOrExpr
49

10+
_POLARS_LEGACY_SUPPORT = tuple(int(re.sub("[^0-9]", "", x)) for x in pl.__version__.split(".")) < (
11+
0,
12+
20,
13+
16,
14+
)
15+
_IS_POLARS_V1 = pl.__version__.startswith("1")
16+
17+
_PLUGIN_PATH = Path(__file__).parent
18+
19+
_PLUGIN_LIB_LEGACY = os.path.join(
20+
os.path.dirname(__file__),
21+
next(
22+
filter(
23+
lambda file: file.endswith((".so", ".dll", ".pyd")),
24+
os.listdir(os.path.dirname(__file__)),
25+
)
26+
),
27+
)
28+
529

630
def str_to_expr(x: StrOrExpr) -> pl.Expr:
731
if isinstance(x, str):
@@ -14,23 +38,20 @@ def str_to_expr(x: StrOrExpr) -> pl.Expr:
1438

1539
def pl_plugin(
1640
*,
17-
lib: str,
1841
symbol: str,
19-
args: List[StrOrExpr],
42+
args: List[Union[pl.Series, pl.Expr]],
2043
kwargs: Optional[Dict[str, Any]] = None,
2144
is_elementwise: bool = False,
2245
returns_scalar: bool = False,
2346
changes_length: bool = False,
2447
cast_to_supertype: bool = False,
2548
) -> pl.Expr:
26-
# pl.__version__ should always be a valid version number, so split returns always 3 strs
27-
if tuple(int(x) for x in pl.__version__.split(".")) < (0, 20, 16):
28-
# This will eventually be deprecated?
29-
first = str_to_expr(args[0])
30-
return first.register_plugin(
31-
lib=lib,
49+
if _POLARS_LEGACY_SUPPORT:
50+
# This will eventually be deprecated, yes
51+
return args[0].register_plugin(
52+
lib=_PLUGIN_LIB_LEGACY,
3253
symbol=symbol,
33-
args=[str_to_expr(x) for x in args[1:]],
54+
args=args[1:],
3455
kwargs=kwargs,
3556
is_elementwise=is_elementwise,
3657
returns_scalar=returns_scalar,
@@ -41,8 +62,8 @@ def pl_plugin(
4162
from polars.plugins import register_plugin_function
4263

4364
return register_plugin_function(
44-
plugin_path=lib,
45-
args=[str_to_expr(x) for x in args],
65+
plugin_path=_PLUGIN_PATH,
66+
args=args,
4667
function_name=symbol,
4768
kwargs=kwargs,
4869
is_elementwise=is_elementwise,

python/polars_istr/cusip.py

Lines changed: 17 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
from __future__ import annotations
22
import polars as pl
3-
from polars.utils.udfs import _get_shared_lib_location
4-
from ._utils import pl_plugin, StrOrExpr
3+
from ._utils import pl_plugin
54

6-
_lib = _get_shared_lib_location(__file__)
75

8-
9-
def cusip_extract_all(x: StrOrExpr) -> pl.Expr:
6+
def cusip_extract_all(x: pl.Series | pl.Expr) -> pl.Expr:
107
"""
118
Returns a struct containing country_code, issue_num, issuer_num, check_digit,
129
or null, if it cannot be parsed.
@@ -15,106 +12,98 @@ def cusip_extract_all(x: StrOrExpr) -> pl.Expr:
1512
"""
1613
return pl_plugin(
1714
args=[x],
18-
lib=_lib,
1915
symbol="pl_cusip_full",
2016
is_elementwise=True,
2117
)
2218

2319

24-
def cusip_issue_num(x: StrOrExpr) -> pl.Expr:
20+
def cusip_issue_num(x: pl.Series | pl.Expr) -> pl.Expr:
2521
"""
2622
Returns the issue number from the CUSIP, or null if it cannot be parsed.
2723
"""
2824
return pl_plugin(
2925
args=[x],
30-
lib=_lib,
3126
symbol="pl_cusip_issue_num",
3227
is_elementwise=True,
3328
)
3429

3530

36-
def cusip_issuer_num(x: StrOrExpr) -> pl.Expr:
31+
def cusip_issuer_num(x: pl.Series | pl.Expr) -> pl.Expr:
3732
"""
3833
Returns the issuer number from the CUSIP, or null if it cannot be parsed.
3934
"""
4035
return pl_plugin(
4136
args=[x],
42-
lib=_lib,
4337
symbol="pl_cusip_issuer_num",
4438
is_elementwise=True,
4539
)
4640

4741

48-
def cusip_check_digit(x: StrOrExpr) -> pl.Expr:
42+
def cusip_check_digit(x: pl.Series | pl.Expr) -> pl.Expr:
4943
"""
5044
Returns check digit from the CUSIP, or null if it cannot be parsed.
5145
"""
5246
return pl_plugin(
5347
args=[x],
54-
lib=_lib,
5548
symbol="pl_cusip_check_digit",
5649
is_elementwise=True,
5750
)
5851

5952

60-
def cusip_country_code(x: StrOrExpr) -> pl.Expr:
53+
def cusip_country_code(x: pl.Series | pl.Expr) -> pl.Expr:
6154
"""
6255
Returns the country code from the CUSIP, or null if it cannot be parsed.
6356
"""
6457
return pl_plugin(
6558
args=[x],
66-
lib=_lib,
6759
symbol="pl_cusip_country_code",
6860
is_elementwise=True,
6961
)
7062

7163

72-
def cusip_payload(x: StrOrExpr) -> pl.Expr:
64+
def cusip_payload(x: pl.Series | pl.Expr) -> pl.Expr:
7365
"""
7466
Returns the payload (CUSIP ex. check digit) from the CUSIP, or null if it
7567
cannot be parsed.
7668
"""
7769
return pl_plugin(
7870
args=[x],
79-
lib=_lib,
8071
symbol="pl_cusip_payload",
8172
is_elementwise=True,
8273
)
8374

8475

85-
def cusip_is_private_issue(x: StrOrExpr) -> pl.Expr:
76+
def cusip_is_private_issue(x: pl.Series | pl.Expr) -> pl.Expr:
8677
"""
8778
Returns true if the issue number is reserved for private use.
8879
"""
8980
return pl_plugin(
9081
args=[x],
91-
lib=_lib,
9282
symbol="pl_cusip_is_private_issue",
9383
is_elementwise=True,
9484
)
9585

9686

97-
def cusip_has_private_issuer(x: StrOrExpr) -> pl.Expr:
87+
def cusip_has_private_issuer(x: pl.Series | pl.Expr) -> pl.Expr:
9888
"""
9989
Returns true if the issuer is reserved for private use.
10090
"""
10191
return pl_plugin(
10292
args=[x],
103-
lib=_lib,
10493
symbol="pl_cusip_has_private_issuer",
10594
is_elementwise=True,
10695
)
10796

10897

109-
def cusip_is_private_use(x: StrOrExpr) -> pl.Expr:
98+
def cusip_is_private_use(x: pl.Series | pl.Expr) -> pl.Expr:
11099
"""
111100
Returns True if either the issuer or issue number is reserved for
112101
private use.
113102
"""
114-
return pl_plugin(args=[x], lib=_lib, symbol="pl_cusip_is_private_use", is_elementwise=True)
103+
return pl_plugin(args=[x], symbol="pl_cusip_is_private_use", is_elementwise=True)
115104

116105

117-
def cusip_is_cins(x: StrOrExpr) -> pl.Expr:
106+
def cusip_is_cins(x: pl.Series | pl.Expr) -> pl.Expr:
118107
"""
119108
Returns true if this CUSIP number is actually a
120109
CUSIP International Numbering System (CINS) number,
@@ -123,10 +112,10 @@ def cusip_is_cins(x: StrOrExpr) -> pl.Expr:
123112
124113
Null if unable to parse.
125114
"""
126-
return pl_plugin(args=[x], lib=_lib, symbol="pl_cusip_is_cins", is_elementwise=True)
115+
return pl_plugin(args=[x], symbol="pl_cusip_is_cins", is_elementwise=True)
127116

128117

129-
def cusip_is_cins_base(x: StrOrExpr) -> pl.Expr:
118+
def cusip_is_cins_base(x: pl.Series | pl.Expr) -> pl.Expr:
130119
"""
131120
Returns true if this CUSIP identifier is actually a CUSIP International
132121
Numbering System (CINS) identifier (with the further restriction that
@@ -135,10 +124,10 @@ def cusip_is_cins_base(x: StrOrExpr) -> pl.Expr:
135124
136125
Null if unable to parse.
137126
"""
138-
return pl_plugin(args=[x], lib=_lib, symbol="pl_cusip_is_cins_base", is_elementwise=True)
127+
return pl_plugin(args=[x], symbol="pl_cusip_is_cins_base", is_elementwise=True)
139128

140129

141-
def cusip_is_cins_extended(x: StrOrExpr) -> pl.Expr:
130+
def cusip_is_cins_extended(x: pl.Series | pl.Expr) -> pl.Expr:
142131
"""
143132
Returns true if this CUSIP identifier is actually a CUSIP International
144133
Numbering System (CINS) identifier (with the further restriction that
@@ -147,92 +136,4 @@ def cusip_is_cins_extended(x: StrOrExpr) -> pl.Expr:
147136
148137
Null if unable to parse.
149138
"""
150-
return pl_plugin(args=[x], lib=_lib, symbol="pl_cusip_is_cins_extended", is_elementwise=True)
151-
152-
153-
@pl.api.register_expr_namespace("cusip")
154-
class CusipExt:
155-
"""
156-
This class contains tools for parsing CUSIP/CINS and Extended CINS format data.
157-
158-
Polars Namespace: cusip
159-
160-
Example: pl.col("cusip_cins_string").cusip.country_code()
161-
"""
162-
163-
def __init__(self, expr: pl.Expr):
164-
self._expr: pl.Expr = expr
165-
166-
def extract_all(self) -> pl.Expr:
167-
return self._expr.register_plugin(
168-
lib=_lib,
169-
symbol="pl_cusip_full",
170-
is_elementwise=True,
171-
)
172-
173-
def issue_num(self) -> pl.Expr:
174-
return self._expr.register_plugin(
175-
lib=_lib,
176-
symbol="pl_cusip_issue_num",
177-
is_elementwise=True,
178-
)
179-
180-
def issuer_num(self) -> pl.Expr:
181-
return self._expr.register_plugin(
182-
lib=_lib,
183-
symbol="pl_cusip_issuer_num",
184-
is_elementwise=True,
185-
)
186-
187-
def check_digit(self) -> pl.Expr:
188-
return self._expr.register_plugin(
189-
lib=_lib,
190-
symbol="pl_cusip_check_digit",
191-
is_elementwise=True,
192-
)
193-
194-
def country_code(self) -> pl.Expr:
195-
return self._expr.register_plugin(
196-
lib=_lib,
197-
symbol="pl_cusip_country_code",
198-
is_elementwise=True,
199-
)
200-
201-
def payload(self) -> pl.Expr:
202-
return self._expr.register_plugin(
203-
lib=_lib,
204-
symbol="pl_cusip_payload",
205-
is_elementwise=True,
206-
)
207-
208-
def is_private_issue(self) -> pl.Expr:
209-
return self._expr.register_plugin(
210-
lib=_lib,
211-
symbol="pl_cusip_is_private_issue",
212-
is_elementwise=True,
213-
)
214-
215-
def has_private_issuer(self) -> pl.Expr:
216-
return self._expr.register_plugin(
217-
lib=_lib,
218-
symbol="pl_cusip_has_private_issuer",
219-
is_elementwise=True,
220-
)
221-
222-
def is_private_use(self) -> pl.Expr:
223-
return self._expr.register_plugin(
224-
lib=_lib, symbol="pl_cusip_is_private_use", is_elementwise=True
225-
)
226-
227-
def is_cins(self) -> pl.Expr:
228-
return self._expr.register_plugin(lib=_lib, symbol="pl_cusip_is_cins", is_elementwise=True)
229-
230-
def is_cins_base(self) -> pl.Expr:
231-
return self._expr.register_plugin(
232-
lib=_lib, symbol="pl_cusip_is_cins_base", is_elementwise=True
233-
)
234-
235-
def is_cins_extended(self) -> pl.Expr:
236-
return self._expr.register_plugin(
237-
lib=_lib, symbol="pl_cusip_is_cins_extended", is_elementwise=True
238-
)
139+
return pl_plugin(args=[x], symbol="pl_cusip_is_cins_extended", is_elementwise=True)

0 commit comments

Comments
 (0)