poc ok
parent
cbe790c90c
commit
284fec9026
|
|
@ -4,7 +4,7 @@ import threading
|
||||||
import queue
|
import queue
|
||||||
from sys import argv
|
from sys import argv
|
||||||
|
|
||||||
def main():
|
def _main():
|
||||||
def __input__():
|
def __input__():
|
||||||
return input()
|
return input()
|
||||||
if argv[1:]:
|
if argv[1:]:
|
||||||
|
|
@ -57,7 +57,7 @@ class InputBuffer(threading.Thread):
|
||||||
while not self.done:
|
while not self.done:
|
||||||
try:
|
try:
|
||||||
state = InputBuffer.State.choose(state, self._run())
|
state = InputBuffer.State.choose(state, self._run())
|
||||||
latest = state.v
|
latest = state.vs
|
||||||
if latest != previous:
|
if latest != previous:
|
||||||
print(InputBuffer.now(), latest)
|
print(InputBuffer.now(), latest)
|
||||||
previous = latest
|
previous = latest
|
||||||
|
|
@ -72,7 +72,7 @@ class InputBuffer(threading.Thread):
|
||||||
if self.dequeue(deadline - InputBuffer.now()) == None:
|
if self.dequeue(deadline - InputBuffer.now()) == None:
|
||||||
self.done = True
|
self.done = True
|
||||||
return
|
return
|
||||||
return InputBuffer.State(now, self.pick(now))
|
return InputBuffer.State(now, self.pick_n(now, 3))
|
||||||
|
|
||||||
def enqueue(self, k):
|
def enqueue(self, k):
|
||||||
self.q.put([ InputBuffer.now(), k.lower() ])
|
self.q.put([ InputBuffer.now(), k.lower() ])
|
||||||
|
|
@ -92,8 +92,8 @@ class InputBuffer(threading.Thread):
|
||||||
self.pages[t].push(k)
|
self.pages[t].push(k)
|
||||||
return got
|
return got
|
||||||
|
|
||||||
def pick(self, t):
|
def pick_n(self, t, n):
|
||||||
return self.pages.get(self.bucket(t), InputBuffer.Page()).pick()
|
return self.pages.get(self.bucket(t), InputBuffer.Page()).pick_n(n)
|
||||||
|
|
||||||
class Page:
|
class Page:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
|
@ -102,19 +102,22 @@ class InputBuffer(threading.Thread):
|
||||||
def push(self, k):
|
def push(self, k):
|
||||||
self.inputs[k] = self.inputs.get(k, 0) + 1
|
self.inputs[k] = self.inputs.get(k, 0) + 1
|
||||||
|
|
||||||
def pick(self):
|
def pick_n(self, n):
|
||||||
if not self.inputs:
|
if not self.inputs:
|
||||||
return None
|
return None
|
||||||
options = []
|
options = []
|
||||||
for k,v in self.inputs.items():
|
for k,v in self.inputs.items():
|
||||||
for i in range(0, v):
|
for i in range(0, v):
|
||||||
options.append(k)
|
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:
|
class State:
|
||||||
def __init__(self, t, v):
|
def __init__(self, t, vs):
|
||||||
self.t = t
|
self.t = t
|
||||||
self.v = v
|
self.vs = vs
|
||||||
|
|
||||||
def choose(a, b):
|
def choose(a, b):
|
||||||
if not a:
|
if not a:
|
||||||
|
|
@ -126,7 +129,7 @@ class InputBuffer(threading.Thread):
|
||||||
if b.t > a.t:
|
if b.t > a.t:
|
||||||
latest = b
|
latest = b
|
||||||
oldest = a
|
oldest = a
|
||||||
if latest.v == None:
|
if not latest.vs:
|
||||||
a_few_t_ago = InputBuffer.now() - InputBuffer.sticky_duration()
|
a_few_t_ago = InputBuffer.now() - InputBuffer.sticky_duration()
|
||||||
if oldest.t > a_few_t_ago:
|
if oldest.t > a_few_t_ago:
|
||||||
return oldest
|
return oldest
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue