From cef5e2eb864139c63a860b44c94541dee216c015 Mon Sep 17 00:00:00 2001 From: Rafael Araujo Date: Fri, 28 Nov 2025 17:32:42 +0000 Subject: [PATCH 1/5] Refacturing functions remove_symbols and format_cep --- brutils/__init__.py | 2 -- brutils/cep.py | 62 +++++++++++++++++++++------------------------ tests/test_cep.py | 23 +++++++---------- 3 files changed, 38 insertions(+), 49 deletions(-) diff --git a/brutils/__init__.py b/brutils/__init__.py index 7ba6e25c..80deeb49 100644 --- a/brutils/__init__.py +++ b/brutils/__init__.py @@ -6,7 +6,6 @@ ) from brutils.cep import generate as generate_cep from brutils.cep import is_valid as is_valid_cep -from brutils.cep import remove_symbols as remove_symbols_cep # CNH Imports from brutils.cnh import is_valid_cnh as is_valid_cnh @@ -95,7 +94,6 @@ "get_cep_information_from_address", "generate_cep", "is_valid_cep", - "remove_symbols_cep", # CNPJ "format_cnpj", "generate_cnpj", diff --git a/brutils/cep.py b/brutils/cep.py index 46ef975f..3160f2a3 100644 --- a/brutils/cep.py +++ b/brutils/cep.py @@ -2,6 +2,7 @@ from random import randint from unicodedata import normalize from urllib.request import urlopen +from re import sub from brutils.data.enums import UF from brutils.exceptions import CEPNotFound, InvalidCEP @@ -10,52 +11,47 @@ # FORMATTING ############ - -def remove_symbols(dirty: str) -> str: - """ - Removes specific symbols from a given CEP (Postal Code). - - This function takes a CEP (Postal Code) as input and removes all occurrences - of the '.' and '-' characters from it. - - Args: - cep (str): The input CEP (Postal Code) containing symbols to be removed. - - Returns: - str: A new string with the specified symbols removed. - - Example: - >>> remove_symbols("123-45.678.9") - "123456789" - >>> remove_symbols("abc.xyz") - "abcxyz" - """ - - return "".join(filter(lambda char: char not in ".-", dirty)) - - -def format_cep(cep: str) -> str | None: +def format_cep(cep: str, only_nums=False) -> str | ValueError: """ Formats a Brazilian CEP (Postal Code) into a standard format. - This function takes a CEP (Postal Code) as input and, if it is a valid - 8-digit CEP, formats it into the standard "12345-678" format. - + This function takes a CEP (Postal Code) as input and, + - Removes special characteres; + - Check if the string follows the CEP length pattern; + - Returns ValueError if the string is out of the pattern; + - Return a string with the formatted CEP. + Args: cep (str): The input CEP (Postal Code) to be formatted. Returns: str: The formatted CEP in the "12345-678" format if it's valid, - None if it's not valid. + ValueError if it's not valid. Example: >>> format_cep("12345678") "12345-678" + >>> format_cep(" 12.345/678 ", only_nums=True) + "12345678" >>> format_cep("12345") - None + ValueError("The value inputed doesn't fit with a CEP value") """ - - return f"{cep[:5]}-{cep[5:8]}" if is_valid(cep) else None + ### Checking data type + if not isinstance(cep, str): + return ValueError("The CEP value should be a string type") + + ### Removing special characteres + cep = sub('[^A-Za-z0-9]+', '', cep) + + ### Checking CEP patterns + if len(cep) != 8: + return ValueError("The value inputed doesn't fit with a CEP value") + + ### Returning CEP value + if only_nums: + return cep + else: + return f"{cep[:5]}-{cep[5:]}" # OPERATIONS @@ -156,7 +152,7 @@ def get_address_from_cep( """ base_api_url = "https://viacep.com.br/ws/{}/json/" - clean_cep = remove_symbols(cep) + clean_cep = format_cep(cep, only_nums=True) cep_is_valid = is_valid(clean_cep) if not cep_is_valid: diff --git a/tests/test_cep.py b/tests/test_cep.py index e47b12d3..6d550a24 100644 --- a/tests/test_cep.py +++ b/tests/test_cep.py @@ -9,20 +9,18 @@ get_address_from_cep, get_cep_information_from_address, is_valid, - remove_symbols, ) class TestCEP(TestCase): def test_remove_symbols(self): - self.assertEqual(remove_symbols("00000000"), "00000000") - self.assertEqual(remove_symbols("01310-200"), "01310200") - self.assertEqual(remove_symbols("01..310.-200.-"), "01310200") - self.assertEqual(remove_symbols("abc01310200*!*&#"), "abc01310200*!*&#") - self.assertEqual( - remove_symbols("ab.c1.--.3-102.-0-.0-.*.-!*&#"), "abc1310200*!*&#" + self.assertEqual(format_cep("00000000"), "00000-000") + self.assertEqual(format_cep("01310-200", only_nums=True), "01310200") + self.assertEqual(format_cep("01..310.-200.-"), "01310-200") + self.assertIsInstance(format_cep("abc01310200*!*&#"), ValueError) + self.assertIsInstance( + format_cep("ab.c1.--.3-102.-0-.0-.*.-!*&#"), ValueError ) - self.assertEqual(remove_symbols("...---..."), "") def test_is_valid(self): # When CEP is not string, returns False @@ -50,14 +48,11 @@ def test_when_cep_is_valid_returns_True_to_format(self, mock_is_valid): self.assertEqual(format_cep("01310200"), "01310-200") - # Checks if function is_valid_cnpj is called - mock_is_valid.assert_called_once_with("01310200") - - def test_when_cep_is_not_valid_returns_none(self, mock_is_valid): + def test_when_cep_is_not_valid_returns_error(self, mock_is_valid): mock_is_valid.return_value = False - # When cep isn't valid, returns None - self.assertIsNone(format_cep("013102009")) + # When cep isn't valid, returns ValueError + self.assertIsInstance(format_cep("013102009"), ValueError) @patch("brutils.cep.urlopen") From 7e81487e188bd58fc95c64a35f56542a5d0c2521 Mon Sep 17 00:00:00 2001 From: Rafael Araujo Date: Tue, 2 Dec 2025 15:38:45 +0000 Subject: [PATCH 2/5] updating documentation --- README.md | 43 ++++++++++++------------------------------- README_EN.md | 43 ++++++++++++++----------------------------- 2 files changed, 26 insertions(+), 60 deletions(-) diff --git a/README.md b/README.md index a24fffa3..4ecfc820 100644 --- a/README.md +++ b/README.md @@ -333,14 +333,18 @@ False ### format_cep Formata um CEP (Código de Endereçamento Postal) brasileiro em um formato padrão. -Esta função recebe um CEP como entrada e, se for um CEP válido com 8 dígitos, -o formata no padrão "12345-678". +Esta função recebe um CEP como entrada, remove caracteres especiais e, se for um +CEP válido com 8 dígitos, e retorna o formata no padrão "12345-678" ou apenas os +números "12345678" se o parâmetro only_nums for True. Argumentos: - cep (str): O CEP (Código de Endereçamento Postal) de entrada a ser formatado. +- only_nums (bool): Valor default é False, caso seja passado True como valor + será retornada uma string contendo apenas números + Retorna: - str: O CEP formatado no formato "12345-678" se for válido, None se não for @@ -352,37 +356,14 @@ Example: >>> from brutils import format_cep >>> format_cep('01310200') '01310-200' ->>> format_cep("12345678") +>>> format_cep(" 12.345_678 ") +"12345-678" +>>> format_cep("12345678", only_nums=True) "12345-678" >>> format_cep("12345") -None -``` - -### remove_symbols_cep - -Remove símbolos específicos de um CEP (Código de Endereçamento Postal) -fornecido. Esta função recebe um CEP como entrada e remove todas as ocorrências -dos caracteres '.' e '-' dele. - -Argumentos: - -- cep (str): O CEP (Código de Endereçamento Postal) de entrada que contém os - símbolos a serem removidos. - -Retorna: - -- str: Uma nova string com os símbolos especificados removidos. - -Exemplo: - -```python ->>> from brutils import remove_symbols_cep ->>> remove_symbols_cep('01310-200') -'01310200' ->>> remove_symbols_cep("123-45.678.9") -"123456789" ->>> remove_symbols_cep("abc.xyz") -"abcxyz" +ValueError +>>> format_cep("ac345-564") +"ac345-564" ``` ### generate_cep diff --git a/README_EN.md b/README_EN.md index aa784da5..3c6de2d1 100644 --- a/README_EN.md +++ b/README_EN.md @@ -334,13 +334,19 @@ False ### format_cep -This function takes a CEP (Postal Code) as input and, if it is a valid -8-digit CEP, formats it into the standard "12345-678" format. +This function formats a brazillian postal code (CEP) in the standard format. +It receives a CEP as input, removes all special characters, validates if the +CEP is 8 digits long, and returns the standard format "12345-678" or only the +numbers "12345678" if the parameters only_nums is setted as True. Args: - cep (str): The input CEP (Postal Code) to be formatted. +- only_nums (bool): The default value is False, in case of the True value be + inputed, the function will return a string with only + numbers + Returns: - str: The formatted CEP in the "12345-678" format if it's valid, @@ -352,35 +358,14 @@ Example: >>> from brutils import format_cep >>> format_cep('01310200') '01310-200' ->>> format_cep("12345678") +>>> format_cep(" 12.345_678 ") +"12345-678" +>>> format_cep("12345678", only_nums=True) "12345-678" >>> format_cep("12345") -None -``` - -### remove_symbols_cep - -This function takes a CEP (Postal Code) as input and removes all occurrences of -the '.' and '-' characters from it. - -Args: - -- cep (str): The input CEP (Postal Code) containing symbols to be removed. - -Returns: - -- str: A new string with the specified symbols removed. - -Example: - -```python ->>> from brutils import remove_symbols_cep ->>> remove_symbols_cep('01310-200') -'01310200' ->>> remove_symbols_cep("123-45.678.9") -"123456789" ->>> remove_symbols_cep("abc.xyz") -"abcxyz" +ValueError +>>> format_cep("ac345-564") +"ac345-564" ``` ### generate_cep From d0977cd48962bf7ba7a7d40d2408329302147921 Mon Sep 17 00:00:00 2001 From: Rafael Araujo Date: Tue, 2 Dec 2025 16:46:30 +0000 Subject: [PATCH 3/5] resolving format issue --- brutils/cep.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/brutils/cep.py b/brutils/cep.py index 3160f2a3..12225a31 100644 --- a/brutils/cep.py +++ b/brutils/cep.py @@ -1,8 +1,8 @@ from json import loads from random import randint +from re import sub from unicodedata import normalize from urllib.request import urlopen -from re import sub from brutils.data.enums import UF from brutils.exceptions import CEPNotFound, InvalidCEP @@ -11,16 +11,17 @@ # FORMATTING ############ + def format_cep(cep: str, only_nums=False) -> str | ValueError: """ Formats a Brazilian CEP (Postal Code) into a standard format. - This function takes a CEP (Postal Code) as input and, + This function takes a CEP (Postal Code) as input and, - Removes special characteres; - Check if the string follows the CEP length pattern; - Returns ValueError if the string is out of the pattern; - Return a string with the formatted CEP. - + Args: cep (str): The input CEP (Postal Code) to be formatted. @@ -39,14 +40,14 @@ def format_cep(cep: str, only_nums=False) -> str | ValueError: ### Checking data type if not isinstance(cep, str): return ValueError("The CEP value should be a string type") - + ### Removing special characteres - cep = sub('[^A-Za-z0-9]+', '', cep) + cep = sub("[^A-Za-z0-9]+", "", cep) ### Checking CEP patterns if len(cep) != 8: return ValueError("The value inputed doesn't fit with a CEP value") - + ### Returning CEP value if only_nums: return cep From d92930edd5a51e0eaa60ef3b82e72b45f22f7210 Mon Sep 17 00:00:00 2001 From: Rafael Araujo Date: Wed, 10 Dec 2025 16:28:04 +0000 Subject: [PATCH 4/5] updating according code review --- README.md | 2 +- README_EN.md | 2 +- brutils/cep.py | 15 +++++++-------- tests/test_cep.py | 10 +++++----- 4 files changed, 14 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 4ecfc820..694367ad 100644 --- a/README.md +++ b/README.md @@ -361,7 +361,7 @@ Example: >>> format_cep("12345678", only_nums=True) "12345-678" >>> format_cep("12345") -ValueError +None >>> format_cep("ac345-564") "ac345-564" ``` diff --git a/README_EN.md b/README_EN.md index 3c6de2d1..d1fb3cda 100644 --- a/README_EN.md +++ b/README_EN.md @@ -363,7 +363,7 @@ Example: >>> format_cep("12345678", only_nums=True) "12345-678" >>> format_cep("12345") -ValueError +None >>> format_cep("ac345-564") "ac345-564" ``` diff --git a/brutils/cep.py b/brutils/cep.py index 12225a31..9877ceb6 100644 --- a/brutils/cep.py +++ b/brutils/cep.py @@ -1,6 +1,5 @@ from json import loads from random import randint -from re import sub from unicodedata import normalize from urllib.request import urlopen @@ -12,14 +11,14 @@ ############ -def format_cep(cep: str, only_nums=False) -> str | ValueError: +def format_cep(cep: str, only_nums=False) -> str | None: """ Formats a Brazilian CEP (Postal Code) into a standard format. This function takes a CEP (Postal Code) as input and, - Removes special characteres; - Check if the string follows the CEP length pattern; - - Returns ValueError if the string is out of the pattern; + - Returns None if the string is out of the pattern; - Return a string with the formatted CEP. Args: @@ -27,7 +26,7 @@ def format_cep(cep: str, only_nums=False) -> str | ValueError: Returns: str: The formatted CEP in the "12345-678" format if it's valid, - ValueError if it's not valid. + None if it's not valid. Example: >>> format_cep("12345678") @@ -35,18 +34,18 @@ def format_cep(cep: str, only_nums=False) -> str | ValueError: >>> format_cep(" 12.345/678 ", only_nums=True) "12345678" >>> format_cep("12345") - ValueError("The value inputed doesn't fit with a CEP value") + None """ ### Checking data type if not isinstance(cep, str): - return ValueError("The CEP value should be a string type") + return None ### Removing special characteres - cep = sub("[^A-Za-z0-9]+", "", cep) + cep = "".join(filter(str.isalnum, cep)) ### Checking CEP patterns if len(cep) != 8: - return ValueError("The value inputed doesn't fit with a CEP value") + return None ### Returning CEP value if only_nums: diff --git a/tests/test_cep.py b/tests/test_cep.py index 6d550a24..06b9f683 100644 --- a/tests/test_cep.py +++ b/tests/test_cep.py @@ -17,9 +17,9 @@ def test_remove_symbols(self): self.assertEqual(format_cep("00000000"), "00000-000") self.assertEqual(format_cep("01310-200", only_nums=True), "01310200") self.assertEqual(format_cep("01..310.-200.-"), "01310-200") - self.assertIsInstance(format_cep("abc01310200*!*&#"), ValueError) - self.assertIsInstance( - format_cep("ab.c1.--.3-102.-0-.0-.*.-!*&#"), ValueError + self.assertEqual(format_cep("abc01310200*!*&#"), None) + self.assertEqual( + format_cep("ab.c1.--.3-102.-0-.0-.*.-!*&#"), None ) def test_is_valid(self): @@ -51,8 +51,8 @@ def test_when_cep_is_valid_returns_True_to_format(self, mock_is_valid): def test_when_cep_is_not_valid_returns_error(self, mock_is_valid): mock_is_valid.return_value = False - # When cep isn't valid, returns ValueError - self.assertIsInstance(format_cep("013102009"), ValueError) + # When cep isn't valid, returns None + self.assertEqual(format_cep("013102009"), None) @patch("brutils.cep.urlopen") From 4c6031d9c8afdf844f4204b72cd28e8b3478fcf3 Mon Sep 17 00:00:00 2001 From: Rafael Araujo Date: Wed, 10 Dec 2025 16:32:11 +0000 Subject: [PATCH 5/5] including lint --- tests/test_cep.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/test_cep.py b/tests/test_cep.py index 06b9f683..4981a6b2 100644 --- a/tests/test_cep.py +++ b/tests/test_cep.py @@ -18,9 +18,7 @@ def test_remove_symbols(self): self.assertEqual(format_cep("01310-200", only_nums=True), "01310200") self.assertEqual(format_cep("01..310.-200.-"), "01310-200") self.assertEqual(format_cep("abc01310200*!*&#"), None) - self.assertEqual( - format_cep("ab.c1.--.3-102.-0-.0-.*.-!*&#"), None - ) + self.assertEqual(format_cep("ab.c1.--.3-102.-0-.0-.*.-!*&#"), None) def test_is_valid(self): # When CEP is not string, returns False