patternpythonMinor
Converting click count data to JSON
Viewed 0 times
clickconvertingjsoncountdata
Problem
I have an array filled with 5 other arrays that consists of arrays of 2 values (first a date then a count):
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:
(The
The output looks like this:
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.
[
[
[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.
Solution
Trailing semicolons
At many places there is a
It's completely unnecessary in Python and you should remove those.
Appending to arrays
At several places you are appending values to arrays in a strange way, for example:
The common and shorter way to do this is using
Augmented assignment operator
Instead of:
This is shorter and better:
Use list comprehensions
Instead of:
You can use a list comprehension, a powerful feature of Python:
At many places there is a
; at the end of the line.It's completely unnecessary in Python and you should remove those.
Appending to arrays
At several places you are appending values to arrays in a strange way, for example:
datedict[val[0]] = datedict[val[0]] + [val[1]];The common and shorter way to do this is using
.append():datedict[val[0]].append(val[1])Augmented assignment operator
+=Instead of:
l = l + 1This is shorter and better:
l += 1Use list comprehensions
Instead of:
datearr = []
for dat in daterange(mindate, today):
datearr = datearr + [datedict[dat]]You can use a list comprehension, a powerful feature of Python:
datearr = [datedict[dat] for dat in daterange(mindate, today)]Code Snippets
datedict[val[0]] = datedict[val[0]] + [val[1]];datedict[val[0]].append(val[1])datearr = []
for dat in daterange(mindate, today):
datearr = datearr + [datedict[dat]]datearr = [datedict[dat] for dat in daterange(mindate, today)]Context
StackExchange Code Review Q#78248, answer score: 4
Revisions (0)
No revisions yet.