Segue projeto para análise e contribuição:
import re
from re import Match
class CEP:
def __init__(self, number):
if self.valid(number) is not None:
self.__number = self.remove_mask(number)
else:
raise ValueError('CEP inválido')
def valid(self, number: str) -> Match[str]:
number = self.remove_mask(number)
comp = re.compile(r"^\d{8}$")
return comp.match(number)
def remove_mask(self, number:str) -> str:
return re.sub(r"[.-]", '', number)
@property
def number(self):
return self.__number
def __str__(self):
return "{}.{}-{}".format(self.__number[:2], self.__number[2:5], self.__number[5:])