Skip to content
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

add regexMatch #83

Merged
merged 2 commits into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions adtl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ def parse_if(
return cast_value != value
elif cmp in ["=", "=="]:
return cast_value == value
elif cmp == "=~":
return bool(re.match(value, cast_value, re.IGNORECASE))
else:
raise ValueError(f"Unrecognized operand: {cmp}")
elif isinstance(rule[key], set): # common error, missed colon to make it a dict
Expand Down
11 changes: 10 additions & 1 deletion docs/specification.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ if = { foobar_type = 4 }
```

Operations other than equals can be specified as `{ field_name = {op = value} }`
where *op* is one of `< | > | <= | >= | !=`. Logical operations (and, or, not) are
where *op* is one of `< | > | <= | >= | != | =~`. Logical operations (and, or, not) are
supported with `any = [ condition-list ]` (or), `all = [ condition-list ]` (and),
`not = { condition }` (not).
In the above example, if we wanted to set from field *foobar* only if
Expand All @@ -180,6 +180,15 @@ if.all = [ # in TOML this is a nested key, like { "if": { "all": [ ... ] } }
]
```

The `=~` operator allows matching with regular expressions, similar to the Bash
and Perl operators. The following will match strings `SARS-CoV 2`, `SARS COV 2`
and `sars-cov-2`. Case is ignored when matching.

```toml
field = "foobar"
if.foobar."=~" = ".*SARS[- ]CoV[- ]2.*"
```

The **oneToMany** table has default conditional behaviour so that rows are only shown
if the row is not empty, and contains values which can be mapped correctly if maps are
provided. For example, an observation recording the presence/absence of vomiting should only
Expand Down
7 changes: 5 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ packages = ["adtl"]

[project]
name = "adtl"
version = "0.4.0"
version = "0.5.0"
description = "Another data transformation language"
authors = [{name = "Abhishek Dasgupta", email = "abhishek.dasgupta@dtc.ox.ac.uk"}]
authors = [
{name = "Abhishek Dasgupta", email = "abhishek.dasgupta@dtc.ox.ac.uk"},
{name = "Pip Liggins", email = "philippa.liggins@dtc.ox.ac.uk"}
]
license = {file = "LICENSE"}
requires-python = ">=3.8"
readme = "README.md"
Expand Down
14 changes: 14 additions & 0 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,20 @@ def test_get_value(row_rule, expected):
@pytest.mark.parametrize(
"row_rule,expected",
[
(({"pathogen": "covid 19"}, {"pathogen": {"=~": ".*covid.*"}}), True),
(({"pathogen": "covid 19"}, {"pathogen": {"=~": ".*SARS-?CoV-?2.*"}}), False),
(
({"pathogen": "sars cov 2"}, {"pathogen": {"=~": ".*SARS[- ]CoV[- ]2.*"}}),
True,
),
(
({"pathogen": "sars-cov 2"}, {"pathogen": {"=~": ".*SARS[- ]CoV[- ]2.*"}}),
True,
),
(
({"pathogen": "coronavírus"}, {"pathogen": {"=~": ".*coronav[ií]rus.*"}}),
True,
),
((ROW_CONDITIONAL, {"outcome_type": 4}), True),
((ROW_CONDITIONAL, {"not": {"outcome_type": 4}}), False),
((ROW_CONDITIONAL, {"outcome_type": {"==": 4}}), True),
Expand Down
Loading