def get_approx(self, currency, date): """ Returns currency rates for the specified date or for the nearest date if there is no data for the specified date. """ if currency == constants.LOCAL_CURRENCY: return ( Decimal(1), Decimal(1) ) day = util.get_day(date) today = util.get_day(datetime.date.today()) rates = [ rate for rate in self._db.execute(""" SELECT day, sell_rate, buy_rate FROM rates WHERE currency = ? AND ? <= day AND day <= ? """, (currency, day - MIN_RATE_ACCURACY, day + MIN_RATE_ACCURACY)) ] if ( self._todays_rates is not None and currency in self._todays_rates and day - MIN_RATE_ACCURACY <= today <= day + MIN_RATE_ACCURACY ): rates.append((today,) + self._todays_rates[currency]) nearest = None for rate in rates: if nearest is None or abs(day - rate[0]) < abs(day - nearest[0]): nearest = rate if nearest is None: return None else: return (Decimal(nearest[1]), Decimal(nearest[2]))
def __add(self, rates): """Saves new rate info.""" data = [] for date, currencies in rates.items(): for currency, rates in currencies.items(): data.append(( util.get_day(date), currency, str(rates[0]), str(rates[1]) )) self._db.executemany("INSERT INTO rates VALUES (?, ?, ?, ?)", data) self._db.commit()