From 284fec90266749b44b51e3e19bda06257019bbac Mon Sep 17 00:00:00 2001 From: Bel LaPointe Date: Sun, 18 Sep 2022 11:45:22 -0600 Subject: [PATCH] poc ok --- src/.prototype/main.py | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/.prototype/main.py b/src/.prototype/main.py index 25db913..c888d0b 100644 --- a/src/.prototype/main.py +++ b/src/.prototype/main.py @@ -4,7 +4,7 @@ import threading import queue from sys import argv -def main(): +def _main(): def __input__(): return input() if argv[1:]: @@ -57,7 +57,7 @@ class InputBuffer(threading.Thread): while not self.done: try: state = InputBuffer.State.choose(state, self._run()) - latest = state.v + latest = state.vs if latest != previous: print(InputBuffer.now(), latest) previous = latest @@ -72,7 +72,7 @@ class InputBuffer(threading.Thread): if self.dequeue(deadline - InputBuffer.now()) == None: self.done = True return - return InputBuffer.State(now, self.pick(now)) + return InputBuffer.State(now, self.pick_n(now, 3)) def enqueue(self, k): self.q.put([ InputBuffer.now(), k.lower() ]) @@ -92,8 +92,8 @@ class InputBuffer(threading.Thread): self.pages[t].push(k) return got - def pick(self, t): - return self.pages.get(self.bucket(t), InputBuffer.Page()).pick() + def pick_n(self, t, n): + return self.pages.get(self.bucket(t), InputBuffer.Page()).pick_n(n) class Page: def __init__(self): @@ -102,19 +102,22 @@ class InputBuffer(threading.Thread): def push(self, k): self.inputs[k] = self.inputs.get(k, 0) + 1 - def pick(self): + def pick_n(self, n): if not self.inputs: return None options = [] for k,v in self.inputs.items(): for i in range(0, v): options.append(k) - return options[random.randint(0, len(options)-1)] + results = [] + for i in range(0, n): + results.append(options[random.randint(0, len(options)-1)]) + return list(set(results)) class State: - def __init__(self, t, v): + def __init__(self, t, vs): self.t = t - self.v = v + self.vs = vs def choose(a, b): if not a: @@ -126,7 +129,7 @@ class InputBuffer(threading.Thread): if b.t > a.t: latest = b oldest = a - if latest.v == None: + if not latest.vs: a_few_t_ago = InputBuffer.now() - InputBuffer.sticky_duration() if oldest.t > a_few_t_ago: return oldest