I am loading a data file with dated data into a csv.DictReader
The data looks like this:
date, weight, blood_pressure, sugar_level
1/1/01, 120.1, 100.1, 25.2
2/1/01, 130.1, 102.1, 26.2
3/1/01, 110.1, 120.1, 24.2
4/1/01, 130.1, 130.1, 28.2
5/1/01, 160.1, 104.1, 27.0
6/1/01, 150.5, 100.1, 22.5
7/1/01, 120.2, 129.1, 25.2
... etc...
I want to write a function that allows me to pass in the dictionary (read from the csv file), and return the previous value of a column an epoch ago. Since there are no enumerations in Python, I will use enums to 'constrain' the permissible epochs and column names:
The code snippet I have so far looks something like this:
import csv;
headers = ['date', 'weight', 'blood_pressure', 'sugar_level']
data = csv.DictReader(csvfile, headers);
epoch_type = (week_ago, fortnight_ago, month_ago);
column_names = headers[1:];
def function get_previous_value(data, column_name, epoch_name):
""" Note: data is assumed to be sorted in date ascending order """
""" Returns the previous value in the data, using the specified """
""" column name and epoch type """
I would be grateful if someone could show how to implement this function
[Edit] Changed argument name from 'lookback' to 'epoch_name' to aid clarity