def load_data(year):
'''
Load data into memory cache
'''
year = str(year)
if year in CACHE:
return True
data_file = os.path.join(
os.path.dirname(__file__), 'data', '{}.csv'.format(year)
)
if not os.path.isfile(data_file):
return False
CACHE[year] = {}
with io.open(data_file, encoding='utf-8') as rf:
# Detect CSV header line
has_header = csv.Sniffer().has_header(rf.read(1024))
rf.seek(0)
reader = csv.DictReader(rf, DATA_FIELDS)
if has_header:
next(reader)
for data_line in reader:
day = clean_up_dict(data_line)
# Convert into `int` type so we don't need to parse it afterwards
dt = datetime.strptime(day['date'], '%Y-%m-%d')
day['year'] = dt.year
day['month'] = dt.month
day['day'] = dt.day
day['isholiday'] = bool(int(day['isholiday']))
day['isworkday'] = bool(int(day['isworkday']))
CACHE[year][day.pop('date')] = day
return True
评论列表
文章目录