How to hang timestamp at 5 minute intervals in python

I am using python to increment a counter at 5 minute intervals in redis and the speed of code execution is quite important. I will have a sorted set that looks like this:

1:30 incr 1:35 incr ...

      

where incr is the number of hits between this 5 minute interval.

Let's say I'm at 13:32 and I want to place all the hits between 1:30 and 1:35 in this bucket.

Naively, I can just scroll through the list and check if the minute is between 30 and 35 and put 1:30 in the bucket. Is there a more pythonic method for doing this?

+3


source to share


1 answer


from redis import Redis

r = Redis(db=1)
r.flushdb()
def store(t):
    h, m = map(int, t.split(':'))
    m = m - m % 5
    r.zincrby('tc', '%d:%02d' % (h, m), 1)

store('1:02')
store('1:30')
store('1:32')
store('1:35')

print r.zrange('tc', 0, -1, withscores=True)

      

Output:



[('1:00', 1.0), ('1:35', 1.0), ('1:30', 2.0)]

      

+4


source







All Articles