def __init__(self, start=None, end=None): if start is None: self.start = self.observation_start_date else: self.start = DateHelper.make_date(start) if end is None: self.end = DateHelper.today() else: self.end = DateHelper.make_date(end)
class CBR_USD(ParserBase): """Bank of Russia official USD to RUB exchange rate""" freq = 'd' observation_start_date = DateHelper.make_date('1992-01-01') # '1991-07-01' reference = dict( source_url="http://www.cbr.ru/scripts/Root.asp?PrtId=SXML", varnames=['USDRUR_CB']) def _yield_dicts(self): return cbr_fx.get_cbr_er(self.start, self.end) def sample(self): """Yields dictionaries with sample datapoints.""" return iter([{ 'date': '2017-09-15', 'freq': 'd', 'name': 'USDRUR_CB', 'value': 57.7706 }, { 'date': '2017-09-16', 'freq': 'd', 'name': 'USDRUR_CB', 'value': 57.5336 }, { 'date': '2017-09-19', 'freq': 'd', 'name': 'USDRUR_CB', 'value': 57.6242 }, { 'date': '2017-09-20', 'freq': 'd', 'name': 'USDRUR_CB', 'value': 58.0993 }, { 'date': '2017-09-21', 'freq': 'd', 'name': 'USDRUR_CB', 'value': 58.129 }, { 'date': '2017-09-22', 'freq': 'd', 'name': 'USDRUR_CB', 'value': 58.2242 }, { 'date': '2017-09-23', 'freq': 'd', 'name': 'USDRUR_CB', 'value': 57.6527 }, { 'date': '2017-09-26', 'freq': 'd', 'name': 'USDRUR_CB', 'value': 57.566 }])
class BrentEIA(ParserBase): """Brent oil price from US EIA""" freq = 'd' observation_start_date = DateHelper.make_date('1987-05-15') reference = dict( source_url='https://www.eia.gov/opendata/qb.php?category=241335', varnames=['BRENT']) def _yield_dicts(self): # brent always returns full dataset, need truncate for start_date for p in brent.yield_brent_dicts(): if DateHelper.make_date(p['date']) >= self.start: yield p def sample(self): """Yield a few dictionaries with datapoints.""" return iter([{ 'date': '2017-09-18', 'freq': 'd', 'name': 'BRENT', 'value': 55.5 }, { 'date': '2017-09-15', 'freq': 'd', 'name': 'BRENT', 'value': 56.18 }, { 'date': '2017-09-14', 'freq': 'd', 'name': 'BRENT', 'value': 56.76 }, { 'date': '2017-09-13', 'freq': 'd', 'name': 'BRENT', 'value': 55.52 }, { 'date': '2017-09-12', 'freq': 'd', 'name': 'BRENT', 'value': 55.06 }, { 'date': '2017-09-11', 'freq': 'd', 'name': 'BRENT', 'value': 54.2 }])
class RosstatKEP_Base(ParserBase): observation_start_date = DateHelper.make_date('1999-01-31') reference = dict(source_url=("http://www.gks.ru/wps/wcm/connect/" "rosstat_main/rosstat/ru/statistics/" "publications/catalog/doc_1140080765391"), varnames=['GDP', 'CPI', 'etc']) freq = None #overload this in child classes def _yield_dicts(self): return kep.yield_kep_dicts(self.freq) def sample(self): yield { "date": "2015-11-30", "freq": self.freq, "name": "CPI_rog", "value": 100.8 } yield { "date": "2015-11-30", "freq": self.freq, "name": "RUR_EUR_eop", "value": 70.39 } yield { "date": "2015-12-31", "freq": self.freq, "name": "CPI_rog", "value": 100.8 } yield { "date": "2015-12-31", "freq": self.freq, "name": "RUR_EUR_eop", "value": 79.7 }
def test_on_invalid_format(self): with pytest.raises(Exception): assert DateHelper.make_date('25-03-2000', '%dk%m-%Y')
def test_with_fmt_on_valid_format_day_month_year(self): date = DateHelper.make_date('25-03-2000', fmt='%d-%m-%Y') assert date == datetime.date(2000, 3, 25)
def test_on_invalid_month_raises_ValueError(self): with pytest.raises(ValueError): DateHelper.make_date('2007-25-25')
def test_make_date_on_no_argument_raises_TypeError(self): with pytest.raises(TypeError): DateHelper.make_date()
def test_make_date_on_empty_str_raises_Exception(self): with pytest.raises(Exception): DateHelper.make_date('')
def test_on_none_returns_today(self): assert DateHelper.make_date(None) == datetime.date.today()
def test_accepts_slashed_format(self): date = DateHelper.make_date('2007/01/25') assert date.day == 25 assert date.month == 1 assert date.year == 2007
def test_accepts_YYYY_MM_DD(self): date = DateHelper.make_date('2007-01-25') assert date.day == 25 assert date.month == 1 assert date.year == 2007
def test_returns_datetime_date_type(self): date = DateHelper.make_date('2007-01-25') assert isinstance(date, datetime.date)
def is_date_in_range(d): dt = DateHelper.make_date(d['date']) return dt >= self.start and dt <= self.end
def _yield_dicts(self): # brent always returns full dataset, need truncate for start_date for p in brent.yield_brent_dicts(): if DateHelper.make_date(p['date']) >= self.start: yield p