-
-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Conversão de código do estado do IBGE para a sigla do Estado (#410)
Co-authored-by: Fernando Bezerra fsbezerra@outlook.com
- Loading branch information
1 parent
9972d18
commit 6346f4c
Showing
10 changed files
with
141 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
from .uf import UF | ||
from .uf import CODE_TO_UF, UF |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) |