Skip to content

Commit

Permalink
Filter/square brackets (#849)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielhuppmann authored Apr 19, 2024
1 parent 1a9c05d commit 2e5c85f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
20 changes: 9 additions & 11 deletions pyam/str.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import pandas as pd
from pandas.api.types import is_list_like

REGEXP_CHARACTERS = r".^$+?()[]{}|"


def concat_with_pipe(x, *args, cols=None):
"""Concatenate a list or pandas.Series using ``|``, drop None or numpy.nan"""
Expand Down Expand Up @@ -123,21 +125,17 @@ def reduce_hierarchy(x, depth):
"""
_x = x.split("|")
depth = len(_x) + depth - 1 if depth < 0 else depth
return "|".join(_x[0 : (depth + 1)])
return "|".join(_x[0: (depth + 1)])


def escape_regexp(s):
"""Escape characters with specific regexp use"""
return (
str(s)
.replace("|", "\\|")
.replace(".", r"\.") # `.` has to be replaced before `*`
.replace("*", ".*")
.replace("+", r"\+")
.replace("(", r"\(")
.replace(")", r"\)")
.replace("$", "\\$")
)
s = str(s)
for c in REGEXP_CHARACTERS:
s = s.replace(c, "\\" + c)
# pyam uses `*` as wildcard, replace with `.*` for regex
s = s.replace("*", ".*")
return s


def is_str(x):
Expand Down
10 changes: 7 additions & 3 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,13 @@ def test_pattern_match_dot():
assert (obs == [False, True]).all()


def test_pattern_match_brackets():
data = pd.Series(["foo (bar)", "foo bar"])
values = ["foo (bar)"]
@pytest.mark.parametrize(
"bracket", ("(bar)", "[bar]", "{2}")
)
def test_pattern_match_brackets(bracket):
s = f"foo {bracket}"
data = pd.Series([s, "foo bar"])
values = [s]

obs = pattern_match(data, values)
assert (obs == [True, False]).all()
Expand Down

0 comments on commit 2e5c85f

Please sign in to comment.