From f9748d9662af1c70a1ded91fabedb7699e798dd7 Mon Sep 17 00:00:00 2001 From: Griffiths Lott Date: Wed, 14 Dec 2022 08:18:36 -0500 Subject: [PATCH] Added EINData dataclass with compare function & scoring --- EINService.py | 63 ++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 50 insertions(+), 13 deletions(-) diff --git a/EINService.py b/EINService.py index 65c2817..2168775 100644 --- a/EINService.py +++ b/EINService.py @@ -10,26 +10,55 @@ from pprint import pprint as prt SAMPLE_EIN = "59-1571026" @dataclass -class EINResult: - found: bool - match: bool - businessName: str - address: str - city: str - state: zip +class EINData: + + def __init__(self, ein: str, buinessName: str, address1: str, city:str, state:str, zip: str, phone: str) -> None: + if re.search("\d{2}(-|)\d{7}", str(ein)) == None: + try: raise Exception(f"Invalid EIN: {ein}") + except Exception as e: + print(e) + return None + self.ein = ein.strip().replace('-','') + self.buinessName = buinessName.lower().strip() + self.address1 = address1.lower().strip() + self.city = city.lower().strip() + self.state = state.lower().strip() + self.phone = phone.strip().replace('-','').replace('(',"").replace(')','').replace('+','') + try: + self.zip = int(zip.replace('-','').strip()) + except: + print(f"Invalid ZIP code: {zip}") + + def get_ein(self) -> str: + return f"{self.ein[0:2]}-{self.ein[2:]}" + + def compare(self, otherEIN: 'EINData') -> dict: + compareDict = { + "buinessName" : True if self.buinessName == otherEIN.businessName else False, + "address" : True if self.address1 == otherEIN.address1 else False, + "city": True if self.city == otherEIN.city else False, + "state": True if self.state == otherEIN.state else False, + "zip" : True if self.zip == otherEIN.zip else False + } + score = 0 + for v in compareDict.values(): + score += 1 if v else 0 + compareDict["score"] = score + return compareDict + class EINService(ABC): @classmethod @abstractmethod - def search_ein(self, ein: str) -> EINResult: + def search_ein(self, ein: str) -> EINData: """ Takes a an ein and returns information from a search using the service """ @classmethod @abstractmethod - def search_eins(self, eins: list[EINResult]): + def search_eins(self, eins: list[EINData]): """ """ @@ -58,12 +87,20 @@ class EINTaxIDService(EINService): text = soup.text.splitlines()[4] data = self._parse_return(text) - return data - - def search_eins(eins: list[EINResult]): + return EINData( + ein, + data["company"], + data["address1"], + data["city"], + data["state"], + data["zip"], + data["phone"] + ) + + def search_eins(eins: list[EINData]): pass - def _parse_return(self, content: str): + def _parse_return(self, content: str) -> EINData: m = re.search("EIN Number:", content) company = content[0:m.start()].strip()