%PDF- %PDF-
Direktori : /home/waritko/build/gw2pvo/gw2pvo/ |
Current File : //home/waritko/build/gw2pvo/gw2pvo/average.py |
import logging __author__ = "Mark Ruys" __copyright__ = "Copyright 2017, Mark Ruys" __license__ = "MIT" __email__ = "mark@paracas.nl" class MovingAverage: def __init__(self, n): self.n = round(n) if n > 0 else 1 self.denominator = self.n * (self.n + 1) / 2 self.queue = [] def add(self, x): if len(self.queue) == 0: self.queue = [x] * self.n self.total = sum(self.queue) self.numerator = x * self.denominator self.numerator += self.n * x - self.total self.total += x - self.queue[0] self.queue.append(x) self.queue = self.queue[-self.n:] return self.numerator / self.denominator