def _currency_from_dict_elem( self, d: Dict[str, Union[int, float, str]]) -> Currency: iso = d['iso'] sell = d['sell'] buy = d['buy'] name = d['title'] return Currency(name, iso, sell=sell, buy=buy)
def __currency_object_from_row(cls, row_object: BeautifulSoup) -> Currency: table_cols = row_object.find_all('td') buy = table_cols[3].find_all("span")[0].text.strip() sell = table_cols[4].find_all("span")[0].text.strip() buy = buy.replace(" ", "") sell = sell.replace(" ", "") return Currency(name=table_cols[0].text.strip(), iso=table_cols[2].text, sell=float(sell), buy=float(buy))
def _currency_from_row(self, row: BeautifulSoup) -> Currency: row_cells = row.find_all("td") name = row_cells[0].find(text=True).lower() text = row_cells[1].find(text=True, recursive=False) multiplier, iso = self._subtract_cur_iso_and_multiplier(text) buy = row_cells[2].find(text=True, recursive=False).replace(" ", "") sell = row_cells[3].find(text=True, recursive=False).replace(" ", "") currency = Currency(name, iso, float(sell) / multiplier, float(buy) / multiplier) return currency
def _currency_from_xml_obj(self, xml_tree: etree._Element, currency_name: str) -> Currency: xpath = 'Currency/CharCode[text()="{}"]/..' res = xml_tree.xpath(xpath.format(currency_name.upper())) c = res[0] name = iso = c.find('CharCode').text try: sell_value = float(c.find('Rate').text) except ValueError: sell_value = 0.0 c = Currency(name, iso, sell_value, None) return c
def get_currency(self, currency_name="USD", date=None): """Get currency data for the given currency name""" today = datetime.date.today() if date is None: date = today if currency_name.upper() not in self.allowed_currencies: allowed = ", ".join(self.allowed_currencies) msg = "Incorrect currency '{}', allowed values: {}" raise BotLoggedError(msg.format(currency_name, allowed)) currencies = self.get_all_currencies(date=date) for currency in currencies: if currency.iso.upper() == currency_name: return currency return Currency.empty_currency()
def _currency_from_row(self, row: BeautifulSoup) -> Currency: cells = row.find_all('td') currency_name = cells[1].text if '/' in currency_name: return None try: multiplier = self._multiplier_from_name(currency_name) currency_code = cells[2].text buy = float(cells[3].find(text=True).replace(" ", "")) / multiplier sell = float(cells[4].find(text=True).replace(" ", "")) / multiplier return Currency(name=currency_code, iso=currency_code, sell=sell, buy=buy) except AttributeError: return None
def get_currency(self, currency_name: str = "USD", date: datetime.date = None) -> Currency: logger.info("Belgazprom: getting {}" "for the {}".format(currency_name, date)) today = datetime.date.today() if date is None: date = today assert isinstance(date, datetime.date), "Incorrect date supplied" currencies = self.get_all_currencies(date) for cur in currencies: if currency_name.upper() == cur.iso: return cur else: return Currency.empty_currency()
def get_currency(self, currency_name: str="USD", date: datetime.date=None) -> Currency: logger.info("Belgazprom: getting {}" "for the {}".format(currency_name, date)) today = datetime.date.today() if date is None: date = today assert isinstance(date, datetime.date), "Incorrect date supplied" currencies = self.get_all_currencies(date) for cur in currencies: if currency_name.upper() == cur.iso: return cur else: return Currency.empty_currency()
def get_currency(self, currency_name="USD", date=None): """Get currency data for the given currency name""" today = datetime.date.today() if date is None: date = today if currency_name.upper() not in PriorbankParser.allowed_currencies: allowed = ", ".join(PriorbankParser.allowed_currencies) msg = "Incorrect currency '{}', allowed values: {}" raise BotLoggedError(msg.format(currency_name, allowed)) currencies = self.get_all_currencies(date=date) for currency in currencies: if currency.iso.upper() == currency_name: if date < DENOMINATION_DATE: currency.multiplier = DENOMINATION_MULTIPLIER else: currency.multiplier = 1 return currency return Currency.empty_currency()
def __get_currency_objects(self, cur_table: BeautifulSoup, days_since_now=None) -> Sequence[Currency]: """ Parses BeautifulSoup table with exchanges rates and extracts currency data """ if not days_since_now: currencies = [] exchange_table = cur_table.find('table').find('tbody') exchange_rows = exchange_table.find_all('tr') for row in exchange_rows: try: c = BelgazpromParser.__currency_object_from_row(row) currencies.append(c) except ValueError: logger.error("Error obtaining currency object from {}".format(row)) currencies.append(Currency.empty_currency()) return currencies
def __get_currency_objects(self, cur_table: BeautifulSoup, days_since_now=None) -> Sequence[Currency]: """ Parses BeautifulSoup table with exchanges rates and extracts currency data """ if not days_since_now: currencies = [] exchange_table = cur_table.find('table').find('tbody') exchange_rows = exchange_table.find_all('tr') for row in exchange_rows: try: c = BelgazpromParser.__currency_object_from_row(row) currencies.append(c) except ValueError: logger.error( "Error obtaining currency object from {}".format(row)) currencies.append(Currency.empty_currency()) return currencies
def test_specific_currencies_sorted_correctly(self): c1 = Currency(iso="RUB", buy=20, sell=30) c2 = Currency(iso="USD", buy=20, sell=30) c3 = Currency(iso="BLZ", buy=20, sell=30) c4 = Currency(iso="ZLT", buy=20, sell=30) self.assertEqual(sort_currencies([c1, c2, c3, c4]), [c2, c1, c3, c4])