From 7c417a43f983c877b5427d3bccffee4a47a0bdf4 Mon Sep 17 00:00:00 2001 From: Jamie Forrest Date: Mon, 29 Jan 2024 10:21:26 -0500 Subject: [PATCH 1/3] Refactor duplicated code into two helper methods. --- piiregex.py | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/piiregex.py b/piiregex.py index 9bb758c..e7b8191 100644 --- a/piiregex.py +++ b/piiregex.py @@ -76,13 +76,10 @@ def __init__(self, text=""): self.text = text # Build class attributes of callables. - for k, v in regexes.items(): - setattr(self, k, regex(self, v)(self)) + self.__build_regex_class_attributes() if text: - for key in regexes.keys(): - method = getattr(self, key) - setattr(self, key, method()) + self.__build_class_attributes_callables() def any_match(self, text=""): """Scan through all available matches and try to match. @@ -91,11 +88,8 @@ def any_match(self, text=""): self.text = text # Regenerate class attribute callables. - for k, v in regexes.items(): - setattr(self, k, regex(self, v)(self)) - for key in regexes.keys(): - method = getattr(self, key) - setattr(self, key, method()) + self.__build_regex_class_attributes() + self.__build_class_attributes_callables() matches = [] for match in regexes.keys(): @@ -105,3 +99,13 @@ def any_match(self, text=""): return True if matches else False + def __build_regex_class_attributes(self): + """Build regex class attributes.""" + for k, v in regexes.items(): + setattr(self, k, regex(self, v)(self)) + + def __build_class_attributes_callables(self): + """Build callable class attributes.""" + for key in regexes.keys(): + method = getattr(self, key) + setattr(self, key, method()) From 21a3a9155c1cda3578d1b965c4a91cebe84dd41d Mon Sep 17 00:00:00 2001 From: Jamie Forrest Date: Mon, 29 Jan 2024 10:23:04 -0500 Subject: [PATCH 2/3] Match a method to return all of the matches. --- piiregex.py | 15 +++++++++------ test_piiregex.py | 21 +++++++++++++++++++++ 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/piiregex.py b/piiregex.py index e7b8191..2cfc141 100644 --- a/piiregex.py +++ b/piiregex.py @@ -82,8 +82,12 @@ def __init__(self, text=""): self.__build_class_attributes_callables() def any_match(self, text=""): - """Scan through all available matches and try to match. + """Scan through all available regexes and return whether we found any. """ + return True if self.matches(text=text) else False + + def matches(self, text=""): + """Scan through all available regexes and return a list of matches.""" if text: self.text = text @@ -91,13 +95,12 @@ def any_match(self, text=""): self.__build_regex_class_attributes() self.__build_class_attributes_callables() - matches = [] + results = [] for match in regexes.keys(): - # If we've got a result, add it to matches. + # If we've got a match, add it to results. if getattr(self, match): - matches.append(match) - - return True if matches else False + results.append(match) + return results def __build_regex_class_attributes(self): """Build regex class attributes.""" diff --git a/test_piiregex.py b/test_piiregex.py index 28ad276..a35e4f7 100644 --- a/test_piiregex.py +++ b/test_piiregex.py @@ -182,3 +182,24 @@ def test_any_match_using_init_method(): parsed_text = PiiRegex('asdasdasdasdasd') assert parsed_text.any_match() is False + +def test_matches(pii_regex): + # This matches a phone number and a zip code. + matches = pii_regex.matches('07123 123123') + assert len(matches) == 2 + assert 'ukphones' in matches + assert 'zip_codes' in matches + # This should match nothing. + assert pii_regex.matches('asdasdasdasdasd') == [] + + +def test_matches_using_init_method(): + # This matches a phone number and a zip code. + parsed_text = PiiRegex('07123 123123') + matches = parsed_text.matches() + assert len(matches) == 2 + assert 'ukphones' in matches + assert 'zip_codes' in matches + # This should match nothing. + parsed_text = PiiRegex('asdasdasdasdasd') + assert parsed_text.matches() == [] From 75d8671168802642b2455a6bccd326faed18b3ae Mon Sep 17 00:00:00 2001 From: Jamie Forrest Date: Mon, 29 Jan 2024 10:23:15 -0500 Subject: [PATCH 3/3] Bump library version. --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 0f255ea..dcd339a 100644 --- a/setup.py +++ b/setup.py @@ -1,4 +1,4 @@ from distutils.core import setup -setup(name="piiregex", version="0.0.2", py_modules=["piiregex"]) +setup(name="piiregex", version="0.0.3", py_modules=["piiregex"])