Skip to content

Commit

Permalink
added extension_match, _fullmatch
Browse files Browse the repository at this point in the history
  • Loading branch information
konstantinstadler committed Dec 15, 2023
1 parent 0711f8b commit 4dd72ba
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 4 deletions.
99 changes: 95 additions & 4 deletions pymrio/core/mriosystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -2164,8 +2164,95 @@ def get_extensions(self, data=False):
else:
yield key

def extension_fullmatch(self, extensions=None, find_all=None, **kwargs):
"""Get a dict of extension index dicts with full match of a search pattern.
This calls the extension.fullmatch for all extensions.
Similar to pandas str.fullmatch, thus the start of the index string must match.
Note
-----
Arguments are set to case=True, flags=0, na=False, regex=True.
For case insensitive matching, use (?i) at the beginning of the pattern.
See the pandas/python.re documentation for more details.
Parameters
----------
extensions: str, list of str, list of extensions, None
Which extensions to consider, default (None): all extensions
find_all : None or str
If str (regex pattern) search in all index levels.
All matching rows are returned. The remaining kwargs are ignored.
kwargs : dict
The regex which should be contained. The keys are the index names,
the values are the regex.
If the entry is not in index name, it is ignored silently.
Returns
-------
dict
A dict with the extension names as keys and an Index/MultiIndex of
the matched rows as values
"""
return self._apply_extension_method(
extensions, method="match", find_all=find_all, **kwargs
)

def extension_match(self, extensions=None, find_all=None, **kwargs):
"""Get a dict of extension index dicts which match a search pattern
This calls the extension.match for all extensions.
Similar to pandas str.match, thus the start of the index string must match.
Note
-----
Arguments are set to case=True, flags=0, na=False, regex=True.
For case insensitive matching, use (?i) at the beginning of the pattern.
See the pandas/python.re documentation for more details.
Parameters
----------
extensions: str, list of str, list of extensions, None
Which extensions to consider, default (None): all extensions
find_all : None or str
If str (regex pattern) search in all index levels.
All matching rows are returned. The remaining kwargs are ignored.
kwargs : dict
The regex which should be contained. The keys are the index names,
the values are the regex.
If the entry is not in index name, it is ignored silently.
Returns
-------
dict
A dict with the extension names as keys and an Index/MultiIndex of
the matched rows as values
"""
return self._apply_extension_method(
extensions, method="match", find_all=find_all, **kwargs
)

def extension_contains(self, extensions=None, find_all=None, **kwargs):
"""Get a dict of extension index dicts
"""Get a dict of extension index dicts which contains a search pattern
This calls the extension.contains for all extensions.
Similar to pandas str.contains, thus the index
string must contain the regex pattern.
Note
-----
Arguments are set to case=True, flags=0, na=False, regex=True.
For case insensitive matching, use (?i) at the beginning of the pattern.
See the pandas/python.re documentation for more details.
Parameters
----------
Expand All @@ -2178,13 +2265,17 @@ def extension_contains(self, extensions=None, find_all=None, **kwargs):
The regex which should be contained. The keys are the index names,
the values are the regex.
If the entry is not in index name, it is ignored silently.
Returns
-------
dict
A dict with the extension names as keys and an Index/MultiIndex of
the matched rows as values
"""
method = "contains"
return self._apply_extension_method(
extensions, method, find_all=find_all, **kwargs
extensions, method="contains", find_all=find_all, **kwargs
)

# TODO: CONT: added methods for match, fullmatch
# TODO: CONT: add method for extract extension

def _apply_extension_method(self, extensions, method, *args, **kwargs):
Expand Down
27 changes: 27 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,33 @@ def test_contain_match_matchall(fix_testmrio):
assert all(ext_all_comp == tt.emissions.F.index)


def test_extension_match_contain(fix_testmrio):
tt = fix_testmrio.testmrio
match_air = tt.extension_match(find_all="air")
assert len(match_air["factor_inputs"]) == 0
assert len(match_air["emissions"]) == 1

contain_value_added = tt.extension_contains(inputtype="dded")
assert len(contain_value_added["factor_inputs"]) == 1
assert len(contain_value_added["emissions"]) == 0

fullmatch_0 = tt.extension_fullmatch(emissions="dded")
assert len(fullmatch_0["factor_inputs"]) == 0
assert len(fullmatch_0["emissions"]) == 0
fullmatch_1 = tt.extension_fullmatch(stressor="emission_type.*")
assert len(fullmatch_1["factor_inputs"]) == 0
assert len(fullmatch_1["emissions"]) == 2

# dual match
dual_match1 = tt.extension_match(stressor="emission_type.*", compartment="air")
assert len(dual_match1["factor_inputs"]) == 0
assert len(dual_match1["emissions"]) == 1

dual_match2 = tt.extension_contains(stressor="1", inputtype="alue")
assert len(dual_match2["factor_inputs"]) == 1
assert len(dual_match2["emissions"]) == 1


def test_direct_account_calc(fix_testmrio):
orig = fix_testmrio.testmrio
orig.calc_all()
Expand Down

0 comments on commit 4dd72ba

Please sign in to comment.