Skip to content

Commit 53bc8a7

Browse files
Revert "refactor: Optimizing cep function (#668)" (#679)
This reverts commit 2d53fd1.
1 parent 2d53fd1 commit 53bc8a7

File tree

5 files changed

+102
-55
lines changed

5 files changed

+102
-55
lines changed

README.md

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -333,18 +333,14 @@ False
333333
### format_cep
334334

335335
Formata um CEP (Código de Endereçamento Postal) brasileiro em um formato padrão.
336-
Esta função recebe um CEP como entrada, remove caracteres especiais e, se for um
337-
CEP válido com 8 dígitos, e retorna o formata no padrão "12345-678" ou apenas os
338-
números "12345678" se o parâmetro only_nums for True.
336+
Esta função recebe um CEP como entrada e, se for um CEP válido com 8 dígitos,
337+
o formata no padrão "12345-678".
339338

340339
Argumentos:
341340

342341
- cep (str): O CEP (Código de Endereçamento Postal) de entrada a ser
343342
formatado.
344343

345-
- only_nums (bool): Valor default é False, caso seja passado True como valor
346-
será retornada uma string contendo apenas números
347-
348344
Retorna:
349345

350346
- str: O CEP formatado no formato "12345-678" se for válido, None se não for
@@ -356,14 +352,37 @@ Example:
356352
>>> from brutils import format_cep
357353
>>> format_cep('01310200')
358354
'01310-200'
359-
>>> format_cep(" 12.345_678 ")
360-
"12345-678"
361-
>>> format_cep("12345678", only_nums=True)
355+
>>> format_cep("12345678")
362356
"12345-678"
363357
>>> format_cep("12345")
364358
None
365-
>>> format_cep("ac345-564")
366-
"ac345-564"
359+
```
360+
361+
### remove_symbols_cep
362+
363+
Remove símbolos específicos de um CEP (Código de Endereçamento Postal)
364+
fornecido. Esta função recebe um CEP como entrada e remove todas as ocorrências
365+
dos caracteres '.' e '-' dele.
366+
367+
Argumentos:
368+
369+
- cep (str): O CEP (Código de Endereçamento Postal) de entrada que contém os
370+
símbolos a serem removidos.
371+
372+
Retorna:
373+
374+
- str: Uma nova string com os símbolos especificados removidos.
375+
376+
Exemplo:
377+
378+
```python
379+
>>> from brutils import remove_symbols_cep
380+
>>> remove_symbols_cep('01310-200')
381+
'01310200'
382+
>>> remove_symbols_cep("123-45.678.9")
383+
"123456789"
384+
>>> remove_symbols_cep("abc.xyz")
385+
"abcxyz"
367386
```
368387

369388
### generate_cep

README_EN.md

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -334,19 +334,13 @@ False
334334

335335
### format_cep
336336

337-
This function formats a brazillian postal code (CEP) in the standard format.
338-
It receives a CEP as input, removes all special characters, validates if the
339-
CEP is 8 digits long, and returns the standard format "12345-678" or only the
340-
numbers "12345678" if the parameters only_nums is setted as True.
337+
This function takes a CEP (Postal Code) as input and, if it is a valid
338+
8-digit CEP, formats it into the standard "12345-678" format.
341339

342340
Args:
343341

344342
- cep (str): The input CEP (Postal Code) to be formatted.
345343

346-
- only_nums (bool): The default value is False, in case of the True value be
347-
inputed, the function will return a string with only
348-
numbers
349-
350344
Returns:
351345

352346
- str: The formatted CEP in the "12345-678" format if it's valid,
@@ -358,14 +352,35 @@ Example:
358352
>>> from brutils import format_cep
359353
>>> format_cep('01310200')
360354
'01310-200'
361-
>>> format_cep(" 12.345_678 ")
362-
"12345-678"
363-
>>> format_cep("12345678", only_nums=True)
355+
>>> format_cep("12345678")
364356
"12345-678"
365357
>>> format_cep("12345")
366358
None
367-
>>> format_cep("ac345-564")
368-
"ac345-564"
359+
```
360+
361+
### remove_symbols_cep
362+
363+
This function takes a CEP (Postal Code) as input and removes all occurrences of
364+
the '.' and '-' characters from it.
365+
366+
Args:
367+
368+
- cep (str): The input CEP (Postal Code) containing symbols to be removed.
369+
370+
Returns:
371+
372+
- str: A new string with the specified symbols removed.
373+
374+
Example:
375+
376+
```python
377+
>>> from brutils import remove_symbols_cep
378+
>>> remove_symbols_cep('01310-200')
379+
'01310200'
380+
>>> remove_symbols_cep("123-45.678.9")
381+
"123456789"
382+
>>> remove_symbols_cep("abc.xyz")
383+
"abcxyz"
369384
```
370385

371386
### generate_cep

brutils/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
)
77
from brutils.cep import generate as generate_cep
88
from brutils.cep import is_valid as is_valid_cep
9+
from brutils.cep import remove_symbols as remove_symbols_cep
910

1011
# CNH Imports
1112
from brutils.cnh import is_valid_cnh as is_valid_cnh
@@ -94,6 +95,7 @@
9495
"get_cep_information_from_address",
9596
"generate_cep",
9697
"is_valid_cep",
98+
"remove_symbols_cep",
9799
# CNPJ
98100
"format_cnpj",
99101
"generate_cnpj",

brutils/cep.py

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,35 @@
1111
############
1212

1313

14-
def format_cep(cep: str, only_nums=False) -> str | None:
14+
def remove_symbols(dirty: str) -> str:
15+
"""
16+
Removes specific symbols from a given CEP (Postal Code).
17+
18+
This function takes a CEP (Postal Code) as input and removes all occurrences
19+
of the '.' and '-' characters from it.
20+
21+
Args:
22+
cep (str): The input CEP (Postal Code) containing symbols to be removed.
23+
24+
Returns:
25+
str: A new string with the specified symbols removed.
26+
27+
Example:
28+
>>> remove_symbols("123-45.678.9")
29+
"123456789"
30+
>>> remove_symbols("abc.xyz")
31+
"abcxyz"
32+
"""
33+
34+
return "".join(filter(lambda char: char not in ".-", dirty))
35+
36+
37+
def format_cep(cep: str) -> str | None:
1538
"""
1639
Formats a Brazilian CEP (Postal Code) into a standard format.
1740
18-
This function takes a CEP (Postal Code) as input and,
19-
- Removes special characteres;
20-
- Check if the string follows the CEP length pattern;
21-
- Returns None if the string is out of the pattern;
22-
- Return a string with the formatted CEP.
41+
This function takes a CEP (Postal Code) as input and, if it is a valid
42+
8-digit CEP, formats it into the standard "12345-678" format.
2343
2444
Args:
2545
cep (str): The input CEP (Postal Code) to be formatted.
@@ -31,27 +51,11 @@ def format_cep(cep: str, only_nums=False) -> str | None:
3151
Example:
3252
>>> format_cep("12345678")
3353
"12345-678"
34-
>>> format_cep(" 12.345/678 ", only_nums=True)
35-
"12345678"
3654
>>> format_cep("12345")
3755
None
3856
"""
39-
### Checking data type
40-
if not isinstance(cep, str):
41-
return None
42-
43-
### Removing special characteres
44-
cep = "".join(filter(str.isalnum, cep))
45-
46-
### Checking CEP patterns
47-
if len(cep) != 8:
48-
return None
4957

50-
### Returning CEP value
51-
if only_nums:
52-
return cep
53-
else:
54-
return f"{cep[:5]}-{cep[5:]}"
58+
return f"{cep[:5]}-{cep[5:8]}" if is_valid(cep) else None
5559

5660

5761
# OPERATIONS
@@ -152,7 +156,7 @@ def get_address_from_cep(
152156
"""
153157
base_api_url = "https://viacep.com.br/ws/{}/json/"
154158

155-
clean_cep = format_cep(cep, only_nums=True)
159+
clean_cep = remove_symbols(cep)
156160
cep_is_valid = is_valid(clean_cep)
157161

158162
if not cep_is_valid:

tests/test_cep.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,20 @@
99
get_address_from_cep,
1010
get_cep_information_from_address,
1111
is_valid,
12+
remove_symbols,
1213
)
1314

1415

1516
class TestCEP(TestCase):
1617
def test_remove_symbols(self):
17-
self.assertEqual(format_cep("00000000"), "00000-000")
18-
self.assertEqual(format_cep("01310-200", only_nums=True), "01310200")
19-
self.assertEqual(format_cep("01..310.-200.-"), "01310-200")
20-
self.assertEqual(format_cep("abc01310200*!*&#"), None)
21-
self.assertEqual(format_cep("ab.c1.--.3-102.-0-.0-.*.-!*&#"), None)
18+
self.assertEqual(remove_symbols("00000000"), "00000000")
19+
self.assertEqual(remove_symbols("01310-200"), "01310200")
20+
self.assertEqual(remove_symbols("01..310.-200.-"), "01310200")
21+
self.assertEqual(remove_symbols("abc01310200*!*&#"), "abc01310200*!*&#")
22+
self.assertEqual(
23+
remove_symbols("ab.c1.--.3-102.-0-.0-.*.-!*&#"), "abc1310200*!*&#"
24+
)
25+
self.assertEqual(remove_symbols("...---..."), "")
2226

2327
def test_is_valid(self):
2428
# When CEP is not string, returns False
@@ -46,11 +50,14 @@ def test_when_cep_is_valid_returns_True_to_format(self, mock_is_valid):
4650

4751
self.assertEqual(format_cep("01310200"), "01310-200")
4852

49-
def test_when_cep_is_not_valid_returns_error(self, mock_is_valid):
53+
# Checks if function is_valid_cnpj is called
54+
mock_is_valid.assert_called_once_with("01310200")
55+
56+
def test_when_cep_is_not_valid_returns_none(self, mock_is_valid):
5057
mock_is_valid.return_value = False
5158

5259
# When cep isn't valid, returns None
53-
self.assertEqual(format_cep("013102009"), None)
60+
self.assertIsNone(format_cep("013102009"))
5461

5562

5663
@patch("brutils.cep.urlopen")

0 commit comments

Comments
 (0)