Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,14 @@ False
- [format_cnpj](#format_cnpj)
- [remove_symbols_cnpj](#remove_symbols_cnpj)
- [generate_cnpj](#generate_cnpj)
- [CEP](#cep)
- [is_valid_cep](#is_valid_cep)

## CPF

### is_valid_cpf

Verifica se o CPF é valido.
Verifica se o CPF é valido. Apenas números, formatados como string. Não verifica se o CPF existe.

```python
>>> from brutils import is_valid_cpf
Expand Down Expand Up @@ -90,7 +92,7 @@ Gera um CPF válido aleatório.

### is_valid_cnpj

Verifica se o CNPJ é valido. Apenas números, formatados como string.
Verifica se o CNPJ é valido. Apenas números, formatados como string. Não verifica se o CNPJ existe.

```python
>>> from brutils import is_valid_cnpj
Expand Down Expand Up @@ -128,6 +130,18 @@ Gera um CNPJ válido aleatório.
'34665388000161'
```

## CEP

### is_valid_cep

Verifica se o CEP é valido. Apenas números, formatados como string. Não verifica se o CEP existe.

```python
>>> from brutils import is_valid_cep
>>> is_valid_cep('88390000')
True
```

## Contributing

Sua colaboração é sempre bem-vinda! Preparamos o [arquivo contributing][contributing] pra te ajudar nos primeiros passos. Toda ajuda conta! Sinta-se livre para criar novas [GitHub issues][github-issues] e interagir aqui.
Expand Down
18 changes: 16 additions & 2 deletions README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,14 @@ False
- [format_cnpj](#format_cnpj)
- [remove_symbols_cnpj](#remove_symbols_cnpj)
- [generate_cnpj](#generate_cnpj)
- [CEP](#cep)
- [is_valid_cep](#is_valid_cep)

## CPF

### is_valid_cpf

Check if CPF is valid.
Check if CPF is valid. Numbers only, formatted as strings. Does not check if CPF exists.

```python
>>> from brutils import is_valid_cpf
Expand Down Expand Up @@ -93,7 +95,7 @@ Generate a valid random CPF.

### is_valid_cnpj

Check if CNPJ is valid. Numbers only, formatted as strings.
Check if CNPJ is valid. Numbers only, formatted as strings. Does not check if CNPJ exists.

```python
>>> from brutils import is_valid_cnpj
Expand Down Expand Up @@ -133,6 +135,18 @@ Generate a valid random CNPJ.
'34665388000161'
```

## CEP

### is_valid_cep

Check if CEP is valid. Numbers only, formatted as strings. Does not check if CEP exists.

```python
>>> from brutils import is_valid_cep
>>> is_valid_cep('88390000')
True
```

## Contributing

Collaboration is super welcome! We prepared the [contributing file][contributing] to help you in the first steps. Every little bit of help counts! Feel free to create new [GitHub issues][github-issues] and interact here.
Expand Down
3 changes: 3 additions & 0 deletions brutils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@
remove_symbols as remove_symbols_cnpj,
generate as generate_cnpj,
)
from brutils.cep import (
is_valid as is_valid_cep,
)
22 changes: 22 additions & 0 deletions brutils/cep.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from random import randint


# FORMATTING
############


# OPERATIONS
############


def is_valid(cep): # type: (str) -> bool
"""
Returns when CEP is valid, input should be a digit
string of proper length. Doesn't validate if it's real,
'cause only the "Correios" Base does.
Comment on lines +14 to +16
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Esse disclaimer podia estar na documentação (READMEs) também. O quê achas? Assim a pessoa que for utilizar já fica ciente de como a função funciona.


Source: https://en.wikipedia.org/wiki/C%C3%B3digo_de_Endere%C3%A7amento_Postal

"""

return isinstance(cep, str) and len(cep) == 8 and cep.isdigit()
31 changes: 31 additions & 0 deletions tests/test_cep.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from os import pardir
from os.path import abspath, join, dirname
from sys import path, version_info, dont_write_bytecode
from inspect import getsourcefile
from unittest.mock import patch

dont_write_bytecode = True
range = range if version_info.major >= 3 else xrange
path.insert(
1, abspath(join(dirname(abspath(getsourcefile(lambda: 0))), pardir))
)
from brutils.cep import (
is_valid,
)
from unittest import TestCase, main


class CEP(TestCase):
def test_is_valid(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👏👏👏

# When CEP is not string, returns False
assert not is_valid(1)

# When CEP's len is different of 8, returns False
assert not is_valid("1")

# When CEP does not contain only digits, returns False
assert not is_valid("1234567-")

# When CEP is valid
assert is_valid("99999999")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Um CEP pode ter tudo o mesmo número?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caraca, que loucura!! Deve ser bem bom para decorar hahah

assert is_valid("88390000")