Skip to content

Commit

Permalink
Add support for regex match in expander
Browse files Browse the repository at this point in the history
Can be useful for things like filtering experiments:

```
// run only h3 experiments that use less than 10 nodes
ramble workspace setup --where 're_search("h3.*_node_\d_", {experiment_name})'
```
  • Loading branch information
linsword13 committed Sep 5, 2024
1 parent bca398e commit 66e6596
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/ramble/docs/workspace_config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ Supported functions are:
* ``simplify_str()`` (convert input string to only alphanumerical characters and dashes)
* ``randrange`` (from `random.randrange`)
* ``randint`` (from `random.randint`)
* ``re_search(regex, str)`` (determine if ``str`` contains pattern ``regex``, based on ``re.search``)

.. _ramble-escaped-variables:

Expand Down
7 changes: 7 additions & 0 deletions lib/ramble/ramble/expander.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ def _or(a, b):
return a or b


def _re_search(regex, s):
import re

return re.search(regex, s) is not None


supported_math_operators = {
ast.Add: operator.add,
ast.Sub: operator.sub,
Expand Down Expand Up @@ -60,6 +66,7 @@ def _or(a, b):
"randrange": random.randrange,
"randint": random.randint,
"simplify_str": spack.util.naming.simplify_name,
"re_search": _re_search,
}


Expand Down
2 changes: 2 additions & 0 deletions lib/ramble/ramble/test/expander.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ def exp_dict():
('"2.1.1" in ["2.1.1", "3.1.1", "4.2.1"]', "True", set(), 1),
('"2.1.2" in ["2.1.1", "3.1.1", "4.2.1"]', "False", set(), 1),
("{test_mask}", "0x0", {"test_mask"}, 1),
('re_search(r"bz$", {experiment_name})', "False", set(), 1),
('re_search(r"_fo+", {env_name})', "True", set(), 1),

Check warning on line 77 in lib/ramble/ramble/test/expander.py

View workflow job for this annotation

GitHub Actions / formatting

"fo" should be "of" or "for" or "do" or "go" or "to".
],
)
def test_expansions(input, output, no_expand_vars, passes):
Expand Down

0 comments on commit 66e6596

Please sign in to comment.