A simple class for keeping a rolling tally of the number of events inside a specific time window
| 13 | |
| 14 | |
| 15 | class SpeedCounter: |
| 16 | """ |
| 17 | A simple class for keeping a rolling tally of the number of events inside a specific time window |
| 18 | """ |
| 19 | |
| 20 | def __init__(self, window=60): |
| 21 | self.timestamps = deque() |
| 22 | self.window = window |
| 23 | |
| 24 | def tick(self): |
| 25 | current_time = time.time() |
| 26 | self.timestamps.append(current_time) |
| 27 | self.remove_old_timestamps(current_time) |
| 28 | |
| 29 | def remove_old_timestamps(self, current_time): |
| 30 | while self.timestamps and current_time - self.timestamps[0] > self.window: |
| 31 | self.timestamps.popleft() |
| 32 | |
| 33 | @property |
| 34 | def speed(self): |
| 35 | self.remove_old_timestamps(time.time()) |
| 36 | return len(self.timestamps) |
| 37 | |
| 38 | |
| 39 | class ScanStats: |
no outgoing calls
searching dependent graphs…