Refining the heat map with the date_report() function

This report provides data to generate the activity heat map. For it to operate properly, it must have the same filename and path specified in the HTML template. The default template for the file is named heatmap.tsv and is located in the same directory as the output HTML report.

After opening this file with those defaults on line 267, we write the headers with a tab character delimiting the day, hour, and value columns and ending with a newline character. At this point, we can begin iterating through our list of dictionaries by using two for loops to access each list containing dictionaries.

In the first for loop, we use the enumerate() method to capture the loop iteration number. This number conveniently corresponds to the date we're processing, allowing us to use this value to write the day value:

260 def date_report():
261 """
262 The date_report function writes date information in a
263 TSV report. No input args as the filename
264 is static within the HTML dashboard
265 :return: None
266 """
267 csv_out = open(make_path("heatmap.tsv"), 'w')
268 csv_out.write("day\thour\tvalue\n")
269 for date, hours_list in enumerate(date_list):

In the second for loop, we iterate through each dictionary, gathering both the hour and count values separately by using the items() method to extract the key and value as a tuple. With these values, we can now assign the date, hour, and count to a tab-separated string and write it to the file.

On line 271, we add 1 to the date value as the heat map chart uses a 1 through 7 range, whereas our list uses an index of 0 through 6 to count days of the week.

After iterating through the hours, we flush the data to the disk before moving forward to the next dictionary of hours. Once we've iterated through all of the seven days, we can close this document as it's ready to be used with our heat map chart in the html_report() function:

270         for hour, count in hours_list.items():
271 to_write = "{}\t{}\t{}\n".format(date+1, hour, count)
272 csv_out.write(to_write)
273 csv_out.flush()
274 csv_out.close()