SHARE
TWEET

Untitled

Spiritreader Jun 30th, 2020 858 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class RingBuffer():
  2.     """
  3.    I stole this (with permission)
  4.    """
  5.     def __init__(self, max_length, reactivity_factor=10):
  6.         self.max_length = max_length
  7.         self.index = 0
  8.         self.rb = []
  9.         self.intermed = []
  10.         self.reactivity_factor = reactivity_factor
  11.  
  12.     def add(self, x):
  13.         if len(self.rb) < self.max_length:
  14.             self.rb.append(x)
  15.             self.index += 1
  16.         else:
  17.             if len(self.intermed) < self.__stability():
  18.                 self.intermed.append(x)
  19.             else:
  20.                 x = sum(self.intermed) / len(self.intermed)
  21.                 if self.index >= self.max_length:
  22.                     self.index = 0
  23.                 self.rb[self.index] = x
  24.                 self.index += 1        
  25.                 self.intermed.clear()
  26.  
  27.     def __stability(self):
  28.         if self.reactivity_factor > self.max_length:
  29.             self.reactivity_factor = self.max_length
  30.         return (int)(self.max_length / self.reactivity_factor)
  31.  
  32.     def clear(self):
  33.         self.rb = []
  34.         self.index = 0
  35.  
  36.     def avg(self):
  37.         return sum(self.rb) / len(self.rb)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top