poc ok
parent
cbe790c90c
commit
284fec9026
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in New Issue