-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Added escape_regex
operation to the str
namespace and as a global function
#19257
Merged
Merged
Changes from 8 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
9b42c1f
working without test and without `escape_regex` as function
barak1412 6da46d8
working without test and without `escape_regex` as function
barak1412 27d460b
fix lint
barak1412 32e4daa
added test
barak1412 622d233
Merge branch 'main' of https://github.com/barak1412/polars into expos…
barak1412 2d29893
Merge branch 'main' of https://github.com/barak1412/polars into expos…
barak1412 2e48326
fixed example
barak1412 040d2c7
fix feature gate
barak1412 3d08c6e
Merge branch 'main' of https://github.com/barak1412/polars into expos…
barak1412 1e78fab
cr fix
barak1412 c74aab5
added to the api docs
barak1412 f2a95af
Merge branch 'main' of https://github.com/barak1412/polars into expos…
barak1412 2dde33f
Merge branch 'main' of https://github.com/barak1412/polars into expos…
barak1412 b66bb9e
Merge branch 'main' of https://github.com/barak1412/polars into expos…
barak1412 9349af1
Merge branch 'main' of https://github.com/barak1412/polars into expos…
barak1412 833700c
Merge branch 'main' of https://github.com/barak1412/polars into expos…
barak1412 3776dbf
merge conflict
barak1412 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
crates/polars-ops/src/chunked_array/strings/escape_regex.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
use polars_core::prelude::arity::unary_elementwise; | ||
use polars_core::prelude::StringChunked; | ||
use regex::escape; | ||
|
||
#[inline] | ||
pub fn escape_regex_str(s: &str) -> String { | ||
escape(s) | ||
} | ||
|
||
fn escape_regex_helper(s: Option<&str>) -> Option<String> { | ||
s.map(escape_regex_str) | ||
} | ||
|
||
pub fn escape_regex(ca: &StringChunked) -> StringChunked { | ||
unary_elementwise(ca, escape_regex_helper) | ||
orlp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
use pyo3::prelude::*; | ||
|
||
#[pyfunction] | ||
pub fn escape_regex(s: &str) -> PyResult<String> { | ||
let escaped_s = polars_ops::chunked_array::strings::escape_regex_str(s); | ||
Ok(escaped_s) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from __future__ import annotations | ||
|
||
import contextlib | ||
|
||
with contextlib.suppress(ImportError): # Module not available when building docs | ||
import polars.polars as plr | ||
import polars._reexport as pl | ||
|
||
|
||
def escape_regex(s: str) -> str: | ||
r""" | ||
Escapes string regex meta characters. | ||
|
||
Parameters | ||
---------- | ||
s | ||
The string that all of its meta characters will be escaped. | ||
|
||
""" | ||
if isinstance(s, pl.Expr): | ||
msg = "escape_regex function is unsupported for `Exp`, you may want use `Expr.str.escape_regex` instead" | ||
orlp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
raise TypeError(msg) | ||
elif not isinstance(s, str): | ||
msg = f"escape_regex function supports only `str` type, got `{type(s)}`" | ||
raise TypeError(msg) | ||
|
||
return plr.escape_regex(s) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added this function so
pl.escape_regex
andstr.escape_regex
will be coupled by same implementation.