wip cb
parent
ef3815273e
commit
696b23d18c
29
poc/main.py
29
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():
|
||||
|
|
|
|||
Loading…
Reference in New Issue