Skip to content

Commit

Permalink
Merge pull request #13 from ZeroGachis/task/implement-left-strip
Browse files Browse the repository at this point in the history
✨ Add left-strip preprocessor
  • Loading branch information
sGeeK44 authored Nov 3, 2023
2 parents 6634fd8 + 539edbc commit f544ea1
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ assert rows == [{"name": "Joe"}, {"name": "William"}, {"name": "Jack"}, {"name":
- regex-extract
- replace
- strip-whitespaces
- left-strip

#### Validators

Expand Down
14 changes: 13 additions & 1 deletion magicparse/pre_processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -97,4 +109,4 @@ def key() -> str:
return "regex-extract"


builtins = [LeftPadZeroes, Map, RegexExtract, Replace, StripWhitespaces]
builtins = [LeftPadZeroes, Map, RegexExtract, Replace, StripWhitespaces, LeftStrip]
21 changes: 21 additions & 0 deletions tests/test_pre_processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
RegexExtract,
Replace,
StripWhitespaces,
LeftStrip,
)
import pytest
from unittest import TestCase
Expand Down Expand Up @@ -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(
{
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit f544ea1

Please sign in to comment.