Created
August 18, 2018 19:03
-
-
Save guinslym/92b10395683535d7c59816c399f6f4d5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from datetime import datetime | |
import random | |
import time | |
def create_random_datetime(from_date, to_date, rand_type='uniform'): | |
""" | |
Create random date within timeframe. | |
Parameters | |
---------- | |
from_date : datetime object | |
to_date : datetime object | |
rand_type : {'uniform'} | |
Examples | |
-------- | |
>>> random.seed(28041990) | |
>>> create_random_datetime(datetime(1990, 4, 28), datetime(2000, 12, 31)) | |
datetime.datetime(1998, 12, 13, 23, 38, 0, 121628) | |
>>> create_random_datetime(datetime(1990, 4, 28), datetime(2000, 12, 31)) | |
datetime.datetime(2000, 3, 19, 19, 24, 31, 193940) | |
""" | |
delta = to_date - from_date | |
if rand_type == 'uniform': | |
rand = random.random() | |
else: | |
raise NotImplementedError('Unknown random mode \'{}\'' | |
.format(rand_type)) | |
return from_date + rand * delta | |
d = [create_random_datetime(datetime(2018, 7, 1), datetime(2018, 8, 31)) for i in range(200)] | |
e = [i.date() for i in d] | |
data = [int(time.mktime(i.timetuple()) )for i in e] | |
data = sorted(data) | |
import collections | |
counter=collections.Counter(data) | |
counter = dict(counter) | |
print(counter) | |
datas = [] | |
for key, value in enumerate(counter): | |
datas.append({"date":value , "value":key}) | |
import json | |
datas = json.dumps(counter) | |
print(datas) | |
# {date: 946702811, value: 15} | |
class HeatmapView(TemplateView): | |
def get_context_data(self, **kwargs): | |
context = super( | |
HeatmapView, self | |
).get_context_data(**kwargs) | |
context['data'] = datas | |
return context | |
template_name = 'portail/heatmap.html' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment