Skip to content

Commit f544ea1

Browse files
authored
Merge pull request #13 from ZeroGachis/task/implement-left-strip
✨ Add left-strip preprocessor
2 parents 6634fd8 + 539edbc commit f544ea1

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ assert rows == [{"name": "Joe"}, {"name": "William"}, {"name": "Jack"}, {"name":
111111
- regex-extract
112112
- replace
113113
- strip-whitespaces
114+
- left-strip
114115

115116
#### Validators
116117

magicparse/pre_processors.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,18 @@ def key() -> str:
7373
return "strip-whitespaces"
7474

7575

76+
class LeftStrip(PreProcessor):
77+
def __init__(self, characters: str) -> None:
78+
self.characters = characters
79+
80+
def apply(self, value: str) -> str:
81+
return value.lstrip(self.characters)
82+
83+
@staticmethod
84+
def key() -> str:
85+
return "left-strip"
86+
87+
7688
class RegexExtract(PreProcessor):
7789
def __init__(self, pattern: str) -> None:
7890
pattern = re.compile(pattern)
@@ -97,4 +109,4 @@ def key() -> str:
97109
return "regex-extract"
98110

99111

100-
builtins = [LeftPadZeroes, Map, RegexExtract, Replace, StripWhitespaces]
112+
builtins = [LeftPadZeroes, Map, RegexExtract, Replace, StripWhitespaces, LeftStrip]

tests/test_pre_processors.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
RegexExtract,
77
Replace,
88
StripWhitespaces,
9+
LeftStrip,
910
)
1011
import pytest
1112
from unittest import TestCase
@@ -38,6 +39,12 @@ def test_strip_whitespaces(self):
3839
pre_processor = PreProcessor.build({"name": "strip-whitespaces"})
3940
assert isinstance(pre_processor, StripWhitespaces)
4041

42+
def test_left_strip(self):
43+
pre_processor = PreProcessor.build(
44+
{"name": "left-strip", "parameters": {"characters": "0"}}
45+
)
46+
assert isinstance(pre_processor, LeftStrip)
47+
4148
def test_regex_extract(self):
4249
pre_processor = PreProcessor.build(
4350
{
@@ -114,6 +121,20 @@ def test_success(self):
114121
assert pre_processor.apply(" an input ") == "an input"
115122

116123

124+
class TestLeftStrip(TestCase):
125+
def test_do_nothing(self):
126+
pre_processor = PreProcessor.build(
127+
{"name": "left-strip", "parameters": {"characters": "0"}}
128+
)
129+
assert pre_processor.apply("12345") == "12345"
130+
131+
def test_success(self):
132+
pre_processor = PreProcessor.build(
133+
{"name": "left-strip", "parameters": {"characters": "0"}}
134+
)
135+
assert pre_processor.apply("0000012345") == "12345"
136+
137+
117138
class TestRegexExtract(TestCase):
118139
def test_build_without_value_group(self):
119140
with pytest.raises(

0 commit comments

Comments
 (0)