Skip to content

Commit 86cc8bc

Browse files
authored
Merge pull request mouredev#2894 from KevinED11/main
reto #16 - python
2 parents 51b9651 + 890443c commit 86cc8bc

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import re
2+
from typing import Protocol
3+
from functools import partial, lru_cache
4+
from enum import StrEnum
5+
6+
7+
type IntTuple = tuple[int, ...]
8+
9+
10+
class SearcherFn(Protocol):
11+
def __call__(self, text: str) -> IntTuple: ...
12+
13+
14+
class SearchPattern(StrEnum):
15+
ALL_NUMBERS = r"\d+"
16+
17+
18+
@lru_cache
19+
def generic_searcher(pattern: str, text: str) -> IntTuple:
20+
return tuple(re.findall(pattern, text))
21+
22+
23+
search_all_numbers = partial(generic_searcher, pattern=SearchPattern.ALL_NUMBERS)
24+
25+
26+
class ValidatorFn(Protocol):
27+
def __call__(self, value: str) -> bool: ...
28+
29+
30+
class ValidationPattern(StrEnum):
31+
EMAIL = r"^\w+@\w+\.\w+$"
32+
PHONE = r"^\+?\d{10, 15}$"
33+
URL = r"^https?://.+$"
34+
35+
36+
@lru_cache
37+
def generic_validator(pattern: str, value: str) -> bool:
38+
return re.search(pattern, value) is not None
39+
40+
41+
validate_email = partial(generic_validator, pattern=ValidationPattern.EMAIL)
42+
validate_phone = partial(generic_validator, pattern=ValidationPattern.PHONE)
43+
validate_url = partial(generic_validator, pattern=ValidationPattern.URL)
44+
45+
46+
def execute_validator(validator: ValidatorFn, value: str) -> bool:
47+
return validator(value=value)
48+
49+
50+
def execute_searcher(searcher: SearcherFn, text: str) -> IntTuple:
51+
return searcher(text=text)
52+
53+
54+
class Separator(StrEnum):
55+
LINE_BREAK = "\n"
56+
57+
58+
def main() -> None:
59+
email_result = execute_validator(validator=validate_email, value="[email protected]")
60+
phone_result = execute_validator(validator=validate_phone, value="000000000000")
61+
url_result = execute_validator(
62+
validator=validate_url, value="https://www.google.com"
63+
)
64+
print(email_result, phone_result, url_result, sep=Separator.LINE_BREAK)
65+
66+
search_result = execute_searcher(searcher=search_all_numbers, text="ke8i9")
67+
print(search_result)
68+
69+
70+
if __name__ == "__main__":
71+
main()

0 commit comments

Comments
 (0)