reader can read inputs and emit a pretty steady signal
parent
428eb765b0
commit
d4b2f27cd4
17
src/input.py
17
src/input.py
|
|
@ -1,17 +0,0 @@
|
||||||
class Input:
|
|
||||||
def __init__(self, reader):
|
|
||||||
self.reader = reader
|
|
||||||
|
|
||||||
def get(self):
|
|
||||||
return self.reader.read()
|
|
||||||
|
|
||||||
class StdinReader:
|
|
||||||
def read(self):
|
|
||||||
return input()
|
|
||||||
|
|
||||||
class FileReader:
|
|
||||||
def __init__(self, path):
|
|
||||||
self.f = open(path, "r")
|
|
||||||
|
|
||||||
def read(self):
|
|
||||||
return self.f.readline()
|
|
||||||
|
|
@ -1,5 +1,11 @@
|
||||||
|
import reader
|
||||||
|
import writer
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
raise Exception("not impl")
|
r = reader.Reader(reader.StdinReader())
|
||||||
|
w = writer.Writer()
|
||||||
|
for line in r.read():
|
||||||
|
w.write(line)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,63 @@
|
||||||
|
import threading
|
||||||
|
import queue
|
||||||
|
import time
|
||||||
|
import sys
|
||||||
|
import select
|
||||||
|
|
||||||
|
__interval__ = .1
|
||||||
|
|
||||||
|
class Reader:
|
||||||
|
def __init__(self, reader):
|
||||||
|
self.reader = reader
|
||||||
|
|
||||||
|
def read(self):
|
||||||
|
q = queue.Queue(maxsize=1)
|
||||||
|
|
||||||
|
class async_reader(threading.Thread):
|
||||||
|
def __init__(self, q, foo):
|
||||||
|
threading.Thread.__init__(self)
|
||||||
|
self.q = q
|
||||||
|
self.foo = foo
|
||||||
|
self.stopping = False
|
||||||
|
|
||||||
|
def stop(self):
|
||||||
|
self.stopping = True
|
||||||
|
|
||||||
|
def run(self):
|
||||||
|
while not self.stopping:
|
||||||
|
try:
|
||||||
|
self.q.put_nowait(self.foo())
|
||||||
|
except queue.Full:
|
||||||
|
pass
|
||||||
|
time.sleep(__interval__/2.0)
|
||||||
|
|
||||||
|
reader = async_reader(q, self.reader.read)
|
||||||
|
reader.start()
|
||||||
|
try:
|
||||||
|
while not reader.stopping:
|
||||||
|
try:
|
||||||
|
yield q.get(timeout=__interval__)
|
||||||
|
except queue.Empty:
|
||||||
|
yield None
|
||||||
|
pass
|
||||||
|
except KeyboardInterrupt as e:
|
||||||
|
reader.stop()
|
||||||
|
reader.join()
|
||||||
|
raise e
|
||||||
|
|
||||||
|
class StdinReader:
|
||||||
|
def read(self):
|
||||||
|
try:
|
||||||
|
if select.select([sys.stdin,],[],[],__interval__/2.0)[0]:
|
||||||
|
line = sys.stdin.readline()
|
||||||
|
return line.strip()
|
||||||
|
except Exception as e:
|
||||||
|
pass
|
||||||
|
return None
|
||||||
|
|
||||||
|
class FileReader:
|
||||||
|
def __init__(self, path):
|
||||||
|
self.f = open(path, "r")
|
||||||
|
|
||||||
|
def read(self):
|
||||||
|
return self.f.readline()
|
||||||
|
|
@ -1,10 +1,11 @@
|
||||||
import time
|
import time
|
||||||
|
|
||||||
class Output:
|
class Writer:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.previous = None
|
self.previous = Line(None)
|
||||||
|
|
||||||
def write(self, latest):
|
def write(self, v):
|
||||||
|
latest = Line(v)
|
||||||
chosen = Line.choose(self.previous, latest)
|
chosen = Line.choose(self.previous, latest)
|
||||||
if chosen.v != self.previous.v:
|
if chosen.v != self.previous.v:
|
||||||
print(chosen)
|
print(chosen)
|
||||||
Loading…
Reference in New Issue