From 539edbcbc9923d24f7903294063da9609875e0d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Perin?= Date: Fri, 3 Nov 2023 15:03:22 +0100 Subject: [PATCH] :sparkles: Add left-strip preprocessor --- README.md | 1 + magicparse/pre_processors.py | 14 +++++++++++++- tests/test_pre_processors.py | 21 +++++++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 4f1f6d4..6c5b9e7 100644 --- a/README.md +++ b/README.md @@ -111,6 +111,7 @@ assert rows == [{"name": "Joe"}, {"name": "William"}, {"name": "Jack"}, {"name": - regex-extract - replace - strip-whitespaces +- left-strip #### Validators diff --git a/magicparse/pre_processors.py b/magicparse/pre_processors.py index d3f65b9..cc1f25f 100644 --- a/magicparse/pre_processors.py +++ b/magicparse/pre_processors.py @@ -73,6 +73,18 @@ def key() -> str: return "strip-whitespaces" +class LeftStrip(PreProcessor): + def __init__(self, characters: str) -> None: + self.characters = characters + + def apply(self, value: str) -> str: + return value.lstrip(self.characters) + + @staticmethod + def key() -> str: + return "left-strip" + + class RegexExtract(PreProcessor): def __init__(self, pattern: str) -> None: pattern = re.compile(pattern) @@ -97,4 +109,4 @@ def key() -> str: return "regex-extract" -builtins = [LeftPadZeroes, Map, RegexExtract, Replace, StripWhitespaces] +builtins = [LeftPadZeroes, Map, RegexExtract, Replace, StripWhitespaces, LeftStrip] diff --git a/tests/test_pre_processors.py b/tests/test_pre_processors.py index cdec6b2..2f5fb6e 100644 --- a/tests/test_pre_processors.py +++ b/tests/test_pre_processors.py @@ -6,6 +6,7 @@ RegexExtract, Replace, StripWhitespaces, + LeftStrip, ) import pytest from unittest import TestCase @@ -38,6 +39,12 @@ def test_strip_whitespaces(self): pre_processor = PreProcessor.build({"name": "strip-whitespaces"}) assert isinstance(pre_processor, StripWhitespaces) + def test_left_strip(self): + pre_processor = PreProcessor.build( + {"name": "left-strip", "parameters": {"characters": "0"}} + ) + assert isinstance(pre_processor, LeftStrip) + def test_regex_extract(self): pre_processor = PreProcessor.build( { @@ -114,6 +121,20 @@ def test_success(self): assert pre_processor.apply(" an input ") == "an input" +class TestLeftStrip(TestCase): + def test_do_nothing(self): + pre_processor = PreProcessor.build( + {"name": "left-strip", "parameters": {"characters": "0"}} + ) + assert pre_processor.apply("12345") == "12345" + + def test_success(self): + pre_processor = PreProcessor.build( + {"name": "left-strip", "parameters": {"characters": "0"}} + ) + assert pre_processor.apply("0000012345") == "12345" + + class TestRegexExtract(TestCase): def test_build_without_value_group(self): with pytest.raises(