def __init__(self, utlanstype: list = None, sektor: list = None, rentebinding: list = None, tid: list = None): """ Constructor / Instantiate the class Parameters ---------- utlanstype : list type of loan, default ["70"] sektor : list sektor, default is ["04b"] rentebinding : list type of interest rate, default is ["08", "12", "10", "11", "06"] tid : list time frame """ try: LOGGER.info("trying to create '{}'".format( self.__class__.__name__)) Assertor.assert_data_types([utlanstype, sektor, rentebinding, tid], [(type(None), list) for _ in range(3)]) self.utlanstype = ["70"] if not utlanstype else utlanstype self.sektor = ["04b"] if not sektor else sektor self.rentebinding = ["08", "12", "10", "11", "06" ] if not rentebinding else rentebinding self.tid = self._updated_table_date() if not tid else tid LOGGER.success("created {}".format(self.__class__.__name__)) except Exception as ssb_payload_exception: LOGGER.exception(ssb_payload_exception) raise ssb_payload_exception
def mortgage_offers(self): """ Retrieve finansportalen.no's boliglån grunndata xml and returns dict for content Returns ------- out : dict content from boliglån grunndata Xxml feed """ try: LOGGER.info("trying to retrieve '{}'".format( self.mortgage_offers.__name__)) offers = {} soup = BeautifulSoup( self.response().content.decode("windows-1252"), "xml") root = Et.fromstring(soup.prettify()) remove_url_re = '{[^>]+}' for i, children in enumerate(root.findall(PORTALEN_ENTRY)): offers.update({ i + 1: { re.sub(remove_url_re, '', child.tag): child.text.strip() for child in children if child.text } }) LOGGER.success("'{}' successfully retrieved".format( self.mortgage_offers.__name__)) return offers except Exception as mortgage_offers_exception: LOGGER.exception(mortgage_offers_exception) raise mortgage_offers_exception
def update(self, db_name: str, col_name: str, query: dict, new_value: dict): """ method for updating document(s) in a collection Parameters ---------- db_name : str db name to look for collection col_name : str collection name to apply update query : dict document to query new_value : dict new values to apply in document """ try: LOGGER.info("trying to '{}' document '{}' with value '{}'".format( self.update.__name__, query, new_value)) Assertor.assert_data_types([db_name, col_name], [str, str]) collection = getattr(self._client, db_name.lower())[col_name.lower()] collection.update_many(query, new_value) LOGGER.success("'{}' successfully completed".format( self.update.__name__)) except Exception as exp: LOGGER.exception(exp) raise exp
def read(self, db_name: str, col_name: str): """ method for reading all documents in a collection Parameters ---------- db_name : str db name to lookup col_name : str collection name to lookup Returns ------- out : list all documents in collection """ try: LOGGER.info( "trying to '{}' all documents in collection: '{}' from db: '{}'" .format(self.read.__name__, col_name, db_name)) Assertor.assert_data_types([db_name, col_name], [str, str]) documents = [] for document in getattr(self._client[db_name.lower()], col_name.lower()).find(): documents.append(document) LOGGER.success( "'{}' successfully completed - '{}' document(s) found".format( self.read.__name__, len(documents))) return documents except Exception as exp: LOGGER.exception(exp) raise exp
def delete(self, db_name: str, col_name: str): """ Delete all documents in collection. Will also delete the db that the collection is in. Parameters ---------- db_name : str name of db col_name : str name of collection to be deleted """ try: LOGGER.info( "trying to '{}' all documents from collection: '{}' in db: '{}'" .format(self.delete.__name__, col_name, db_name)) Assertor.assert_data_types([db_name, col_name], [str, str]) collection = getattr(self._client, db_name)[col_name] count = collection.count() collection.drop() LOGGER.success( "'{}' successfully completed - '{}' document(s) deleted". format(self.delete.__name__, count)) except Exception as exp: LOGGER.exception(exp) raise exp
def sifo_expenses(self): """ get SIFO expenses given the family information Returns ------- out : dict dictionary with SIFO expenses """ try: LOGGER.info("trying to retrieve '{}'".format( self.sifo_expenses.__name__)) soup = BeautifulSoup(self.response(), "xml") root = Et.fromstring(soup.prettify()) expenses = {'_id': self.family.id_str} for child in root: expenses.update( {child.tag: child.text.strip().replace(".", "")}) LOGGER.success("'{}' successfully retrieved".format( self.sifo_expenses.__name__)) return expenses except Exception as sifo_expenses_exception: LOGGER.exception(sifo_expenses_exception) raise sifo_expenses_exception
def __init__(self, family_members: list = None, income: Union[int, float, str] = 0, cars: Union[int, str] = 0): """ Constructor / Instantiate the class Parameters ---------- family_members : list list of Person (Male or Female) instances income : int, float, str gross yearly income cars : int, str number of cars in the family """ super().__init__() try: self._assert_family_members(family_members) Assertor.assert_data_types([income, cars], [(int, float, str), (int, str)]) Assertor.assert_non_negative([income, cars]) self._family_members = family_members self._inntekt = str(income) self._antall_biler = str(cars) LOGGER.success( "created '{}', with id: [{}]".format(self.__class__.__name__, self.id_str)) except Exception as family_exception: LOGGER.exception(family_exception) raise family_exception
def __init__(self, age: Union[int, float, str] = 0, kinder_garden: str = '0', sfo: str = '0', pregnant: str = '0'): """ Constructor / Instantiate the class Parameters ---------- age : int, float, str age of person kinder_garden : str kids in kinder garden, '1' true or '0' false sfo : str After school programme, '1' true or '0' false pregnant : str Pregnant female, '1' true or '0' false """ try: super().__init__('k', age, kinder_garden, sfo) Assertor.assert_data_types([age, kinder_garden, sfo], [(float, int, str), str, str]) Assertor.assert_arguments({pregnant: ['pregnant', ('0', '1')]}) if Person.sifo_age(age) not in ('19', '50') and pregnant == '1': raise ValueError("pregnancy at this age is not possible") self._gravid = pregnant LOGGER.success("created '{}', with id: [{}]".format( self.__class__.__name__, self.id_str)) except Exception as female_exception: LOGGER.exception(female_exception) raise female_exception
def format_number(self): """ formatting of phone number according to norwegian standard Returns ------- out : str formatted phone number References ------- https://begrep.difi.no/Felles/mobiltelefonnummer """ try: prefix, number = "+47", self.remove_prefix(self.number) valid_number = re.compile("^\\+?[- 0-9]{8,20}$").search(number) if valid_number: phone_number = prefix + " " + " ".join( [number[i:i + 2] for i in range(0, len(number), 2)]) LOGGER.info("format number '{}' to -> '{}'".format( number, phone_number)) return phone_number raise NotPossibleError( "'{}' is an invalid phone number".format(number)) except Exception as format_number_error: LOGGER.exception(format_number_error) raise format_number_error
def ssb_interest_rates(self): """ gets the interest information from SSB table nr. 10748 Returns ------- out : dict interest rate information from SSB """ try: LOGGER.info("trying to retrieve '{}'".format( self.ssb_interest_rates.__name__)) response = self.response().json() keys = response["dimension"]["Rentebinding"]["category"][ "label"].values() values = response["value"] LOGGER.success("'{}' successfully retrieved".format( self.ssb_interest_rates.__name__)) return { key.lower(): str(val) for key, val in dict(zip(keys, values)).items() } except Exception as ssb_interest_rates_exception: LOGGER.exception(ssb_interest_rates_exception) raise ssb_interest_rates_exception
def __init__(self, age: Union[str, int], income: Union[str, int, float]): """ Constructor / Instantiate the class Parameters ---------- age : str, int age of individual income : str, int, float income of individual """ try: super().__init__() Assertor.assert_data_types([age, income], [(str, int), (str, int, float)]) self.age = str(age + 1) self.income = str(income) self.year = str(2022) self.url = SKATTEETATEN_URL + self.year LOGGER.success("created '{}', with id: [{}]".format( self.__class__.__name__, self.id_)) except Exception as skatteetaten_exception: LOGGER.exception(skatteetaten_exception) raise skatteetaten_exception
def __init__(self): """ Constructor / Instantiate the class """ try: super().__init__() self._browser = requests.post(PORTALEN_URL, auth=PORTALEN_CRED) LOGGER.success("created '{}', with id: [{}]".format( self.__class__.__name__, self.id_str)) except Exception as portalen_exception: LOGGER.exception(portalen_exception) raise portalen_exception
def except_hook(exc_type, exc_value, exc_tb): """ exception hook to handle all exception that are handled by logic in the application """ error_view = ErrorView(None) trace_back_list = [trace.strip() + "\n" for trace in traceback.format_exception(exc_type, exc_value, exc_tb)] trace_back = "".join(trace_back_list) try: raise Exception( "Error! Please contact system administrator, exited with\n\n'{}: {}'".format( exc_type.__name__, exc_value)) except Exception as except_hook_exception: error_view.show_error(except_hook_exception, {}, trace_back) LOGGER.exception("{}\n\n {}".format(except_hook_exception, trace_back.strip()))
def __init__(self): """ Abstract class, so class cannot be instantiated """ try: LOGGER.info("trying to create '{}'".format( self.__class__.__name__)) super().__init__() self._browser = Browser() self._browser.set_handle_robots(False) self._browser.set_handle_refresh(False) self._id_str = str(uuid4()) except Exception as scraper_exception: LOGGER.exception(scraper_exception) raise scraper_exception
def __init__(self, number: str): """ Constructor / Instantiate the class Parameters ---------- number : str phone number to be validated """ try: super().__init__() Assertor.assert_data_types([number], [str]) self.number = number LOGGER.success("created '{}', with id: [{}]".format( self.__class__.__name__, self.id_str)) except Exception as phone_exception: LOGGER.exception(phone_exception) raise phone_exception
def __init__(self): """ Constructor / Instantiate the class. Should only create one connection to the MongoDB cloud database cluster. """ try: LOGGER.info("trying to create '{}'".format( self.__class__.__name__)) self._id_str = str(uuid4()) self._client = MongoClient(DB_STRING) self._db = None self._collection = None LOGGER.success("created '{}', with id: [{}]".format( self.__class__.__name__, self.id_str)) except Exception as dao_exception: LOGGER.exception(dao_exception) raise dao_exception
def __init__(self, family: Family): """ Constructor / Instantiate the class Parameters ---------- family : Family object with family information """ try: super().__init__() Assertor.assert_data_types([family], [Family]) self._family = family LOGGER.success("created '{}', with id: [{}]".format( self.__class__.__name__, self.id_)) except Exception as sifo_exception: LOGGER.exception(sifo_exception) raise sifo_exception
def create(self, db_name: str, col_name: str, document: (dict, list)): """ method for creating posts / documents (dict or list) into collection in database Parameters ---------- db_name : str db to insert record col_name : str collection to insert record document : dict, list record(s) to insert """ try: LOGGER.info( "trying to '{}' document(s) in collection: '{}' in db: '{}'". format(self.create.__name__, col_name, db_name)) Assertor.assert_data_types([db_name, col_name, document], [str, str, (dict, list)]) if not isinstance(document, (dict, list)): raise TypeError("expected type '{}', got '{}' " "instead".format( (dict.__name__, list.__name__), type(document).__name__)) count = len(document) if isinstance(document, list) else len( [document]) if not self._db: self._db = self._client[db_name.lower()] self._collection = self._db[col_name.lower()] else: self._collection = self._db[col_name.lower()] self._collection.insert(document) LOGGER.success( "'{}' successfully completed - '{}' document(s)".format( self.create.__name__, count)) except Exception as exp: LOGGER.exception(exp) raise exp
def __init__(self, payload: SsbPayload = None): """ Constructor / Instantiate the class Parameters ---------- payload : SsbPayload SSB compatible JSON dictionary """ try: super().__init__() Assertor.assert_data_types([payload], [(type(None), SsbPayload)]) self._payload = SsbPayload() if not payload else payload LOGGER.success("created '{}', with id: [{}]".format( self.__class__.__name__, self.id_str)) except Exception as ssb_exception: LOGGER.exception(ssb_exception) raise ssb_exception
def __init__(self, postal_code: str): """ Constructor / Instantiate the class Parameters ---------- postal_code : str postal code to be searched """ try: super().__init__() Assertor.assert_data_types([postal_code], [str]) self._postal_code = postal_code self.validate_postal_code() LOGGER.success("created '{}', with id: [{}]".format( self.__class__.__name__, self.id_)) except Exception as posten_exception: LOGGER.exception(posten_exception) raise posten_exception
def __init__(self, finn_code: str): """ Constructor / Instantiate the class Parameters ---------- finn_code : str Finn-code to be searched for """ try: super().__init__() Assertor.assert_data_types([finn_code], [str]) self._finn_code = finn_code self.validate_finn_code() self._browser = None LOGGER.success("created '{}', with id: [{}]".format( self.__class__.__name__, self.id_)) except Exception as finn_exception: LOGGER.exception(finn_exception) raise finn_exception
def save_json(file_dict: dict, file_dir: str = "report/json", file_prefix: str = "Info"): """ save information in object to JSON file Parameters ---------- file_dict : dict retrieve information from this object and save to file file_dir : str file directory to save JSON files file_prefix : str title of file """ try: Assertor.assert_data_types([file_dir, file_prefix], [str, str]) try: if not os.path.exists(file_dir): os.makedirs(file_dir) except Exception as exception: raise OSError("creation of dir " + file_dir + " failed with: " + str(exception)) _json = json.dumps(file_dict, indent=2, separators=(',', ': '), ensure_ascii=False) local_time = datetime.datetime.now().isoformat().replace( ":", "-").replace(".", "-") file = open( os.path.join(file_dir, file_prefix + local_time + ".json"), "w") file.write(_json) file.close() except Exception as json_exception: LOGGER.exception(json) raise json_exception
def housing_information(self): """ Retrieve and parse housing information from Finn.no search to dict Returns ------- out : dict """ try: LOGGER.info("trying to retrieve '{}' for -> '{}'".format( self.housing_information.__name__, self.finn_code)) soup = BeautifulSoup(self.response().content, "lxml") address = soup.find("p", attrs={"class": "u-caption"}).text price = "".join( price.text for price in soup.find_all("span", attrs={"class": "u-t3"}) if " kr" in price.text).strip().replace(u"\xa0", " ") info = {"finn_adresse": address, "prisantydning": price} keys, values = list(soup.find_all(["th", "dt"])), list( soup.find_all(["td", "dd"])) info.update({ re.sub("[^a-z]+", "", key.text.lower()): val.text.strip().replace(u"\xa0", " ") for key, val in zip(keys, values) }) LOGGER.success("'{}' successfully retrieved".format( self.housing_information.__name__)) return info except Exception as housing_information_exception: LOGGER.exception(housing_information_exception) raise housing_information_exception
def __init__(self, age: Union[int, float, str] = 0, kinder_garden: str = '0', sfo: str = '0'): """ Constructor / Instantiate the class Parameters ---------- age : int, float, str age of person kinder_garden : str kids in kinder garden, '1' true or '0' false sfo : str After school programme, '1' true or '0' false """ try: super().__init__('m', age, kinder_garden, sfo) LOGGER.success("created '{}', with id: [{}]".format( self.__class__.__name__, self.id_str)) except Exception as male_exception: LOGGER.exception(male_exception) raise male_exception