Skip to content

Commit

Permalink
Conversão de código do estado do IBGE para a sigla do Estado (#410)
Browse files Browse the repository at this point in the history
Co-authored-by: Fernando Bezerra fsbezerra@outlook.com
  • Loading branch information
carlos-moreno committed Sep 23, 2024
1 parent 9972d18 commit 6346f4c
Show file tree
Hide file tree
Showing 10 changed files with 141 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Added

- Utilitário `convert_code_to_uf` [#397](https://github.com/brazilian-utils/brutils-python/pull/410)

## [2.2.0] - 2024-09-12

Expand Down
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ False
- [is_valid_voter_id](#is_valid_voter_id)
- [format_voter_id](#format_voter_id)
- [generate_voter_id](#generate_voter_id)
- [IBGE](#ibge)
- [convert_code_to_uf](#convert_code_to_uf)

## CPF

Expand Down Expand Up @@ -1084,6 +1086,29 @@ Exemplo:
'950125640248'
```

## IBGE
### convert_code_to_uf
Converte um determinado código do IBGE (string de 2 dígitos) para sua UF (abreviatura estadual) correspondente.

Args:
* code (str): O código IBGE de 2 dígitos a ser convertido.

Retorna:
* str or None: O código UF correspondente ao código IBGE, ou None se o
código IBGE for inválido.

Exemplo:

```python
>>> from brutils.ibge.uf import convert_code_to_uf
>>> convert_code_to_uf("12")
'AC'
>>> convert_code_to_uf("33")
'RJ'
>>> convert_code_to_uf("99")
>>>
```


# Novos Utilitários e Reportar Bugs

Expand Down
25 changes: 25 additions & 0 deletions README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ False
- [is_valid_voter_id](#is_valid_voter_id)
- [format_voter_id](#format_voter_id)
- [generate_voter_id](#generate_voter_id)
- [IBGE](#ibge)
- [convert_code_to_uf](#convert_code_to_uf)

## CPF

Expand Down Expand Up @@ -1087,6 +1089,29 @@ Example:
'950125640248'
```

## IBGE
### convert_code_to_uf
Converts a given IBGE code (2-digit string) to its corresponding UF (state abbreviation).

Args:
* code (str): The 2-digit IBGE code to be converted.

Retorna:
* str or None: The UF code corresponding to the IBGE code, or None if the
IBGE code is invalid.

Exemplo:

```python
>>> from brutils.ibge.uf import convert_code_to_uf
>>> convert_code_to_uf("12")
'AC'
>>> convert_code_to_uf("33")
'RJ'
>>> convert_code_to_uf("99")
>>>
```

# Feature Request and Bug Report

If you want to suggest new features or report bugs, simply create
Expand Down
7 changes: 7 additions & 0 deletions brutils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@
# Email Import
from brutils.email import is_valid as is_valid_email

# IBGE Imports
from brutils.ibge.uf import (
convert_code_to_uf,
)

# Legal Process Imports
from brutils.legal_process import (
format_legal_process,
Expand Down Expand Up @@ -165,4 +170,6 @@
"format_voter_id",
"generate_voter_id",
"is_valid_voter_id",
# IBGE
"convert_code_to_uf",
]
2 changes: 1 addition & 1 deletion brutils/data/enums/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .uf import UF
from .uf import CODE_TO_UF, UF
30 changes: 30 additions & 0 deletions brutils/data/enums/uf.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,33 @@ class UF(BetterEnum):
SP = "São Paulo"
SE = "Sergipe"
TO = "Tocantins"


class CODE_TO_UF(BetterEnum):
AC = "12"
AL = "27"
AP = "16"
AM = "13"
BA = "29"
CE = "23"
DF = "53"
ES = "32"
GO = "52"
MA = "21"
MT = "51"
MS = "52"
MG = "31"
PA = "15"
PB = "25"
PR = "41"
PE = "26"
PI = "22"
RJ = "33"
RN = "24"
RS = "43"
RO = "11"
RR = "14"
SC = "42"
SP = "35"
SE = "28"
TO = "17"
Empty file added brutils/ibge/__init__.py
Empty file.
32 changes: 32 additions & 0 deletions brutils/ibge/uf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from brutils.data.enums.uf import CODE_TO_UF


def convert_code_to_uf(code): # type: (str) -> str | None
"""
Converts a given IBGE code (2-digit string) to its corresponding UF (state abbreviation).
This function takes a 2-digit IBGE code and returns the corresponding UF code.
It handles all Brazilian states and the Federal District.
Args:
code (str): The 2-digit IBGE code to be converted.
Returns:
str or None: The UF code corresponding to the IBGE code,
or None if the IBGE code is invalid.
Example:
>>> convert_code_to_uf('12')
'AC'
>>> convert_code_to_uf('33')
'RJ'
>>> convert_code_to_uf('99')
>>>
"""

result = None

if code in CODE_TO_UF.values:
result = CODE_TO_UF(code).name

return result
Empty file added tests/ibge/__init__.py
Empty file.
18 changes: 18 additions & 0 deletions tests/ibge/test_uf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from unittest import TestCase

from brutils.ibge.uf import convert_code_to_uf


class TestUF(TestCase):
def test_convert_code_to_uf(self):
# Testes para códigos válidos
self.assertEqual(convert_code_to_uf("12"), "AC")
self.assertEqual(convert_code_to_uf("33"), "RJ")
self.assertEqual(convert_code_to_uf("31"), "MG")
self.assertEqual(convert_code_to_uf("52"), "GO")

# Testes para códigos inválidos
self.assertIsNone(convert_code_to_uf("99"))
self.assertIsNone(convert_code_to_uf("00"))
self.assertIsNone(convert_code_to_uf(""))
self.assertIsNone(convert_code_to_uf("AB"))

0 comments on commit 6346f4c

Please sign in to comment.