Skip to content

Commit 1d2922f

Browse files
committed
Improve handling around helpers.
1 parent 4a586f9 commit 1d2922f

File tree

7 files changed

+48
-28
lines changed

7 files changed

+48
-28
lines changed

PyFunceble/helpers/command.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ def run(self, rstrip: bool = True) -> Generator[str, None, None]:
212212
is that :func:`~PyFunceble.helpers.Command.execute` wait for the
213213
process to end in order to return its output while this method
214214
return each line one by one
215-
- as they are outputed.
215+
- as they are outputted.
216216
217217
:param bool rstrip:
218218
Deactivates the rstrip of the output.

PyFunceble/helpers/dict.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class DictHelper:
6666
Simplify some :code:`dict` manipulation.
6767
6868
:param dict main: The main :code:`dict` to work with.
69-
:raise TypeError: When :code:`main` is not a dict nor a list (tolarated).
69+
:raise TypeError: When :code:`main` is not a dict nor a list (tolerated).
7070
"""
7171

7272
_subject: Optional[Union[Any, dict]] = None
@@ -349,7 +349,7 @@ def to_yaml(
349349
:param bool default_flow_style: Uses the default flow style.
350350
:param int indent: The indentation to apply.
351351
:param bool allow_unicode: Allows the decoding of unicode chars.
352-
:param bool sort_keys: Sors the keys.
352+
:param bool sort_keys: Sorts the keys.
353353
354354
:rtype: dict|list
355355
"""
@@ -371,7 +371,7 @@ def flatten(
371371
data: Optional[Any] = None,
372372
) -> dict:
373373
"""
374-
Flatten the current dictionnary.
374+
Flatten the current dictionary.
375375
376376
:param separator:
377377
The separator to apply.
@@ -405,7 +405,7 @@ def flatten(
405405

406406
def unflatten(self, *, separator: str = ".", data: Optional[Any] = None):
407407
"""
408-
Unflatten a previously flatten dictionnary.
408+
Unflatten a previously flatten dictionary.
409409
410410
:param separator:
411411
The separator to split.

PyFunceble/helpers/file.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ def read(self, *, encoding: str = "utf-8") -> Optional[str]:
183183

184184
def read_bytes(self) -> Optional[bytes]:
185185
"""
186-
Read the given file ath and returns it's bytes contetn.
186+
Read the given file ath and returns it's bytes content.
187187
"""
188188

189189
data = None
@@ -203,7 +203,7 @@ def open(self, *args, **kwargs) -> "open":
203203

204204
def copy(self, destination: str, *, create_parent: bool = False) -> "FileHelper":
205205
"""
206-
Copy the globaly given file path to the given destination.
206+
Copy the globally given file path to the given destination.
207207
208208
:param str destination: The destination of the copy.
209209
:param bool create_parent: Tell us if we have to create the parent directory.

PyFunceble/helpers/list.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -115,18 +115,21 @@ def remove_empty(self) -> "ListHelper":
115115

116116
return self
117117

118-
def remove_duplicates(self) -> "ListHelper":
118+
def remove_duplicates(self, *, efficient: bool = True) -> "ListHelper":
119119
"""
120120
Removes the duplicates of the current list.
121121
"""
122122

123-
result = []
123+
if efficient:
124+
self.subject = list(set(self.subject))
125+
else:
126+
result = []
124127

125-
for element in self.subject:
126-
if element not in result:
127-
result.append(element)
128+
for element in self.subject:
129+
if element not in result:
130+
result.append(element)
128131

129-
self.subject = result
132+
self.subject = result
130133

131134
return self
132135

@@ -147,7 +150,7 @@ def custom_sort(self, key_method: Any, *, reverse: bool = False) -> "ListHelper"
147150
148151
:param key_method:
149152
A function or method to use to format the
150-
readed element before sorting.
153+
read element before sorting.
151154
:type key_method: function|method
152155
153156
:param bool reverse: Tell us if we have to reverse the list.

PyFunceble/helpers/merge.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
The tool to check the availability or syntax of dosubject, IP or URL.
2+
The tool to check the availability or syntax of domain, IP or URL.
33
44
::
55
@@ -164,7 +164,7 @@ def __dict(self, origin: Any, strict: bool = True) -> dict:
164164

165165
def into(self, origin: Any, strict: bool = True) -> Any:
166166
"""
167-
Process the mergin.
167+
Process the merging.
168168
169169
:param origin: The original data.
170170
:param strict:

PyFunceble/helpers/regex.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ class RegexHelper:
6363
"""
6464

6565
_regex: Optional[str] = None
66+
_compiled: Optional[re.Pattern] = None
6667
escape_regex: bool = False
6768

6869
def __init__(self, regex: Optional[str] = None, escape_regex: bool = False):
@@ -99,6 +100,8 @@ def regex(self, value: str) -> None:
99100
else:
100101
self._regex = re.escape(value)
101102

103+
self._compiled = re.compile(self._regex)
104+
102105
def set_regex(self, value: str) -> "RegexHelper":
103106
"""
104107
Sets the regex to work with.
@@ -120,19 +123,15 @@ def get_not_matching_list(self, data: List[str]) -> List[str]:
120123
in the given data.
121124
"""
122125

123-
pre_result = re.compile(self.regex)
124-
125-
return [x for x in data if not pre_result.search(str(x))]
126+
return [x for x in data if not self._compiled.search(str(x))]
126127

127128
def get_matching_list(self, data: List[str]) -> List[str]:
128129
"""
129130
Returns the strings which does the match the regex
130131
in the given data.
131132
"""
132133

133-
pre_result = re.compile(self.regex)
134-
135-
return [x for x in data if pre_result.search(str(x))]
134+
return [x for x in data if self._compiled.search(str(x))]
136135

137136
def match(
138137
self,
@@ -157,12 +156,11 @@ def match(
157156
"""
158157

159158
result = []
160-
to_match = re.compile(self.regex)
161159

162160
if rematch:
163-
pre_result = to_match.findall(data)
161+
pre_result = self._compiled.findall(data)
164162
else:
165-
pre_result = to_match.search(data)
163+
pre_result = self._compiled.search(data)
166164

167165
if return_match and pre_result:
168166
if rematch:
@@ -209,7 +207,7 @@ def replace_match(
209207
self.regex,
210208
replacement,
211209
data,
212-
occurences,
210+
count=occurences,
213211
flags=re.MULTILINE if multiline else 0,
214212
)
215213
return data
@@ -222,4 +220,4 @@ def split(self, data: str) -> List[str]:
222220
:rtype: list
223221
"""
224222

225-
return re.split(self.regex, data)
223+
return self._compiled.split(data)

tests/helpers/test_list.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,29 @@ def test_remove_duplicates(self) -> None:
195195
given = copy.deepcopy(self.str_test_subject)
196196

197197
expected = ["hello", "world", "", "!", " ", "world!", "Hello"]
198-
actual = self.helper.set_subject(given).remove_duplicates().subject
198+
actual = (
199+
self.helper.set_subject(given).remove_duplicates(efficient=False).subject
200+
)
199201

200202
self.assertEqual(expected, actual)
201203

204+
def test_remove_duplicates_efficient(self) -> None:
205+
"""
206+
Tests the method which let us remove the duplicates from a given list
207+
for the case that we want the best possible performance.
208+
"""
209+
210+
given = copy.deepcopy(self.str_test_subject)
211+
212+
expected = ["", "hello", "world!", "world", "!", " ", "Hello"]
213+
actual = (
214+
self.helper.set_subject(given).remove_duplicates(efficient=True).subject
215+
)
216+
217+
self.assertTrue(isinstance(actual, list))
218+
self.assertTrue(len(actual) == len(expected))
219+
self.assertTrue(all(x in actual for x in expected))
220+
202221
def test_sort(self) -> None:
203222
"""
204223
Tests the method which let us sort a given list.

0 commit comments

Comments
 (0)