Last active
August 29, 2015 14:05
-
-
Save gdude2002/26e60e8870b95a61aa4d 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
# This is proof-of-concept code, so it's rather messy and shit. | |
# It should however get the point across. | |
# If anyone has a better way to do this, let me know. | |
import time | |
# Time period per record in seconds | |
TIME_PERIOD = 1 | |
SAVED_PERIODS = 5 | |
_TIME = 0 | |
_COUNT = 1 | |
records = [None] * SAVED_PERIODS | |
for _x in xrange(SAVED_PERIODS): | |
records[_x] = [0, 0] | |
index = 0 | |
def on_message(): | |
global index | |
# Determine current time slot | |
t = int(time.time() / TIME_PERIOD) | |
if records[index][_TIME] != t: | |
# Create a new record and reset the data | |
index = (index + 1) % SAVED_PERIODS | |
records[index][_TIME] = t | |
records[index][_COUNT] = 0 | |
# Update the record for this time slot | |
records[index][_COUNT] += 1 | |
def print_records(): | |
print "\r\n=== Records ===" | |
# Just an example - obviously you'd ram the data into a graph or something | |
from datetime import datetime | |
# Loop through earliest to latest | |
for x in xrange(index + 1, index + 1 + SAVED_PERIODS): | |
x %= SAVED_PERIODS | |
# Convert our time slot into a real time again | |
dt = datetime.fromtimestamp(records[x][_TIME] * TIME_PERIOD) | |
print "{:%Y-%m-%d %H:%M:%S} | {}".format(dt, records[x][_COUNT]) | |
if __name__ == "__main__": | |
for x in xrange(3): | |
on_message() | |
time.sleep(TIME_PERIOD) | |
for x in xrange(7): | |
on_message() | |
time.sleep(TIME_PERIOD) | |
for x in xrange(4): | |
on_message() | |
time.sleep(TIME_PERIOD) | |
for x in xrange(2): | |
on_message() | |
time.sleep(TIME_PERIOD) | |
for x in xrange(9): | |
on_message() | |
time.sleep(TIME_PERIOD) | |
print_records() | |
for x in xrange(1): | |
on_message() | |
time.sleep(TIME_PERIOD) | |
print_records() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment