From 696b23d18cef88773185410b9d75535dea929fa8 Mon Sep 17 00:00:00 2001 From: bel Date: Sun, 10 Apr 2022 16:42:10 -0600 Subject: [PATCH] wip cb --- poc/main.py | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/poc/main.py b/poc/main.py index 0b8bfdc..fc3aa8e 100644 --- a/poc/main.py +++ b/poc/main.py @@ -2,15 +2,32 @@ from sys import stdin, stdout import time class Bucket: - def __init__(self, N, R): + def __init__(self, N, R, T, CB): self.q = 0.0 self.N = N self.R = R - self.__last = 0 + self.T = T + self.CB = CB + self.__last_pop = 0 + self.__last_state = False def push(self): return self.__push_c(1) + def pop(self): + return self.__pop() + + def __mod(self, foo): + result = foo() + self.__cb() + + def __cb(self): + new_state = self.q > self.T + if new_state == self.__last_state: + return + self.__last_state = new_state + self.CB(new_state) + def __push_c(self, c): self.__pop() if self.q+c > self.N: @@ -20,21 +37,21 @@ class Bucket: def __pop(self): now = self.__now() - remove_up_to = (now - self.__last) / self.R + remove_up_to = (now - self.__last_pop) / self.R if remove_up_to > self.q: remove_up_to = self.q self.q -= remove_up_to - self.__last = now + self.__last_pop = now def __now(self): return time.time() def main(stream): - bucket = Bucket(3, 1) + bucket = Bucket(3, 1, 2, lambda state: print("state is now", state)) while readline(): print(bucket.push(), file=stdout) - print(f'q={bucket.q}, N={bucket.N}, R={bucket.R}', file=stdout) + #print(f'q={bucket.q}, N={bucket.N}, R={bucket.R}', file=stdout) stdout.flush() def readline():