Number of events grouped by date in python?

This is undoubtedly another noobish question, but I will ask it anyway:

I have a set of event events with the exact time and time in UTC. I would like to create a line chart showing the total number of events by day (date) in a specified date range. Right now I can get a common dataset for the desired date range, but then I need to go through it and count for each date.

The application runs on the Google engine and uses python.

What is the best way to create a new dataset showing the date and corresponding counts (including if there were no events on that date) that I can use to transfer this information to the django template?

The data set for this example is as follows:

class Event(db.Model):
    event_name = db.StringProperty()
    doe = db.DateTimeProperty()
    dlu = db.DateTimeProperty()
    user = db.UserProperty()

Ideally, I want something with a date and quantity for that date.

Thank you and please let me know if something else is needed to answer this question!

+3
source share
3 answers

You will need to perform binning in memory (i.e. after fetching the data store).

The .date()instance method datetimewill facilitate your binning; he beats off the element of time. Then you can use the dictionary to store the bins:

bins = {}
for event in Event.all().fetch(1000):
    bins.setdefault(event.doe.date(), []).append( event )

Then do what you want (e.g. counting) the bins. For direct counting:

counts = collections.defaultdict(int)
for event in Event.all().fetch(1000):
    counts[event.doe.date()] += 1
+1
source

I do not see how this is possible with a single query, because GQL does not support GROUP BY or aggregation in general.

0

, , , , , . libraryupdate library , , , , . , (, , get) .

0

Source: https://habr.com/ru/post/1738883/


All Articles