From f21e55a126143e737377e4d2c2f4204a3bc1d836 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= Date: Sat, 1 Oct 2022 18:58:00 +0200 Subject: [PATCH] Regex: support more grouping functions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In case of multiple matches we only had an option to return a sum of numeric values. Add more functions. Signed-off-by: Rafał Miłecki --- TUTORIAL.md | 5 +++-- src/invoice2data/extract/parsers/regex.py | 8 ++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/TUTORIAL.md b/TUTORIAL.md index f1852898..19b2fc71 100644 --- a/TUTORIAL.md +++ b/TUTORIAL.md @@ -96,8 +96,9 @@ Optional properties: - `type` (if present must be one of: `int`, `float`, `date`) -results in parsing every matched value to a specified type -- `group` (if present must be `sum`) - results in grouping all matched - values using specified method +- `group` (if present must be one of: `sum`, `min`, `max`, `first`, + `last`) - specifies grouping function (defines what value to return in + case of multiple matches) Example for `regex`: diff --git a/src/invoice2data/extract/parsers/regex.py b/src/invoice2data/extract/parsers/regex.py index 2909d3ed..85ba3ccd 100644 --- a/src/invoice2data/extract/parsers/regex.py +++ b/src/invoice2data/extract/parsers/regex.py @@ -49,6 +49,14 @@ def parse(template, field, settings, content, legacy=False): if "group" in settings: if settings["group"] == "sum": result = sum(result) + elif settings["group"] == "min": + result = min(result) + elif settings["group"] == "max": + result = max(result) + elif settings["group"] == "first": + result = result[0] + elif settings["group"] == "last": + result = result[-1] else: logger.warning("Unsupported grouping method: " + settings["group"]) return None