Moved a get_input to util. Fixed method stop
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
|
|
||||||
import sys, operator, string, os, threading
|
import sys, operator, string, os, threading
|
||||||
from util import getch, cls
|
from util import getch, cls, get_input
|
||||||
from time import sleep
|
from time import sleep
|
||||||
|
|
||||||
lock = threading.Lock()
|
lock = threading.Lock()
|
||||||
@@ -29,7 +29,7 @@ class FreqObserver(threading.Thread):
|
|||||||
self._freqs_0 = freqs_1
|
self._freqs_0 = freqs_1
|
||||||
sleep(0.01)
|
sleep(0.01)
|
||||||
|
|
||||||
def stop():
|
def stop(self):
|
||||||
self._end = True
|
self._end = True
|
||||||
|
|
||||||
def _update_display(self, tuples):
|
def _update_display(self, tuples):
|
||||||
@@ -47,21 +47,6 @@ class FreqObserver(threading.Thread):
|
|||||||
#
|
#
|
||||||
# The active part, dataflow-like
|
# The active part, dataflow-like
|
||||||
#
|
#
|
||||||
interactive = True
|
|
||||||
def get_input():
|
|
||||||
global interactive
|
|
||||||
if not interactive:
|
|
||||||
return True
|
|
||||||
|
|
||||||
while True:
|
|
||||||
key = ord(getch())
|
|
||||||
if key == 32: # space bar
|
|
||||||
return True
|
|
||||||
elif key == 27: # ESC
|
|
||||||
interactive = False
|
|
||||||
return True
|
|
||||||
else: pass
|
|
||||||
|
|
||||||
def characters():
|
def characters():
|
||||||
c = f.read(1)
|
c = f.read(1)
|
||||||
if c != "":
|
if c != "":
|
||||||
@@ -104,6 +89,8 @@ def count_and_sort():
|
|||||||
freqs = {}
|
freqs = {}
|
||||||
# The declaration for reactive observation of freqs
|
# The declaration for reactive observation of freqs
|
||||||
observer = FreqObserver(freqs)
|
observer = FreqObserver(freqs)
|
||||||
|
# Let's get input from the user, or let her
|
||||||
|
# feed the input automatically
|
||||||
while get_input():
|
while get_input():
|
||||||
try:
|
try:
|
||||||
w = non_stop_words().next()
|
w = non_stop_words().next()
|
||||||
@@ -112,6 +99,7 @@ def count_and_sort():
|
|||||||
lock.release()
|
lock.release()
|
||||||
except StopIteration:
|
except StopIteration:
|
||||||
# Let's wait for the observer thread to die gracefully
|
# Let's wait for the observer thread to die gracefully
|
||||||
|
observer.stop()
|
||||||
sleep(1)
|
sleep(1)
|
||||||
break
|
break
|
||||||
|
|
||||||
|
|||||||
@@ -59,3 +59,17 @@ def cls():
|
|||||||
os.system(['clear','cls'][os.name == 'nt'])
|
os.system(['clear','cls'][os.name == 'nt'])
|
||||||
|
|
||||||
|
|
||||||
|
interactive = True
|
||||||
|
def get_input():
|
||||||
|
global interactive
|
||||||
|
if not interactive:
|
||||||
|
return True
|
||||||
|
|
||||||
|
while True:
|
||||||
|
key = ord(getch())
|
||||||
|
if key == 32: # space bar
|
||||||
|
return True
|
||||||
|
elif key == 27: # ESC
|
||||||
|
interactive = False
|
||||||
|
return True
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user