Skip to content

Commit 1d6efda

Browse files
committed
refactor: módulo uf.py
1 parent 483a6d2 commit 1d6efda

4 files changed

Lines changed: 12 additions & 14 deletions

File tree

brutils/data/enums/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
from .uf import CODE_TO_UF, UF
1+
from brutils.data.enums.uf import UF, UF_CODE

brutils/data/enums/uf.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from .better_enum import BetterEnum
1+
from brutils.data.enums.better_enum import BetterEnum
22

33

44
class UF(BetterEnum):
@@ -31,7 +31,7 @@ class UF(BetterEnum):
3131
TO = "Tocantins"
3232

3333

34-
class CODE_TO_UF(BetterEnum):
34+
class UF_CODE(BetterEnum):
3535
AC = "12"
3636
AL = "27"
3737
AP = "16"
@@ -43,7 +43,7 @@ class CODE_TO_UF(BetterEnum):
4343
GO = "52"
4444
MA = "21"
4545
MT = "51"
46-
MS = "52"
46+
MS = "50"
4747
MG = "31"
4848
PA = "15"
4949
PB = "25"

brutils/ibge/uf.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
from brutils.data.enums.uf import CODE_TO_UF, UF
1+
from brutils.data.enums.uf import UF, UF_CODE
22

33

4-
def convert_code_to_uf(code): # type: (str) -> str | None
4+
def convert_code_to_uf(code: str) -> str | None:
55
"""
66
Converts a given IBGE code (2-digit string) to its corresponding UF (state abbreviation).
77
@@ -23,13 +23,10 @@ def convert_code_to_uf(code): # type: (str) -> str | None
2323
>>> convert_code_to_uf('99')
2424
>>>
2525
"""
26+
if code not in UF_CODE.values:
27+
return None
2628

27-
result = None
28-
29-
if code in CODE_TO_UF.values:
30-
result = CODE_TO_UF(code).name
31-
32-
return result
29+
return UF_CODE(code).name
3330

3431

3532
def convert_uf_to_name(uf: str) -> str | None:
@@ -51,12 +48,12 @@ def convert_uf_to_name(uf: str) -> str | None:
5148
>>> convert_uf_to_name('rj')
5249
'Rio de Janeiro'
5350
"""
54-
if not uf or not isinstance(uf, str):
51+
if not uf or not isinstance(uf, str) or len(uf.strip()) != 2:
5552
return None
5653

5754
federal_unit = uf.strip().upper()
5855

59-
if federal_unit not in UF.__members__:
56+
if federal_unit not in UF.names:
6057
return None
6158

6259
result = UF[federal_unit].value

tests/ibge/test_uf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,5 @@ def test_convert_uf_to_name(self):
2727

2828
# Testes para códigos inválidos
2929
self.assertIsNone(convert_code_to_uf("XX"))
30+
self.assertIsNone(convert_code_to_uf("XXX"))
3031
self.assertIsNone(convert_code_to_uf(""))

0 commit comments

Comments
 (0)