I have an array filled with 5 other arrays that consists of arrays of 2 values (first a date then a count):
[
[
[dt, cnt], [dt, cnt], ....
],
[
[dt, cnt], [dt, cnt], ....
],
[
[dt, cnt], [dt, cnt], ....
],
[
[dt, cnt], [dt, cnt], ....
],
[
[dt, cnt], [dt, cnt], ....
],
]
It concerns click statistics of different websites. This data needs to be converted to data to make a consistent chart with Google visualisations. So first a conversion is done in Python, then this result converted to JSON to pass to the Google libs.
My current code is this:
# determine min date
mindate = datetime.date.max
for dataSet in sets:
if (dataSet[0][0] < mindate):
mindate = dataSet[0][0];
# fill a dictionary with all dates involved
datedict = {}
for dat in daterange(mindate, today):
datedict[dat] = [dat];
# fill dictionary with rest of data
arrlen = 2
for dataSet in sets:
# first the values
for value in dataSet:
datedict[value[0]] = datedict[value[0]] + [value[1]];
# don't forget the missing values (use 0)
for dat in daterange(mindate, today):
if len(datedict[dat]) < arrlen:
datedict[dat] = datedict[dat] + [0]
arrlen = arrlen + 1
# convert to an array
datearr = []
for dat in daterange(mindate, today):
datearr = datearr + [datedict[dat]]
# convert to json
result = json.dumps(datearr, cls=DateTimeEncoder)
(The DateTimeEncoder
just gives a JavaScript-friendly datetime in the JSON)
The output looks like this:
[ ["new Date(2008, 7, 27)", 0, 5371, 1042, 69, 0], ["new Date(2008, 7, 28)", 0, 5665, 1100, 89, 0], ... ]
This is one of my first Python adventures, so I expect this piece of code can be improved upon easily. I want this to be shorter and more elegant. Show me the awesomeness of Python because I'm still a bit disappointed.
I'm using Django, by the way.