def closest(self, date=datetime.date.today(), country=None,
limit=datetime.timedelta(days=366)):
"""
Get the closest CPI value for a specified date. The date defaults to
today. A limit can be provided to exclude all values for dates further
away than defined by the limit. This defaults to 366 days.
"""
# Try to get the country
try:
possible_countries = [self.data[country]]
except:
possible_countries = [elem for elem in self.data.keys() if editdistance.eval(country,elem) < 3]
if len(possible_countries) == 0:
return "No country found, typo unlikely for ",country
# Find the closest date
country_cpi = {}
for country in possible_countries:
min_year_diff = 1000
min_year = 0
for year in self.data[country]:
if min_year_diff > abs(date.year - int(year)):
min_year_diff = abs(date.year - int(year))
min_year = year
country_cpi[country] = self.data[country][min_year]
if len(country_cpi) == 1:
return country_cpi[country_cpi.keys()[0]]
else:
return country_cpi
评论列表
文章目录