Better reactive -- lock properly, remove unused arguments, send "end" to deamon thread

This commit is contained in:
Crista Lopes
2013-10-17 21:14:17 -07:00
parent f491a9f722
commit d9ff0f84bd

View File

@@ -4,6 +4,8 @@ import sys, operator, string, os, threading
from util import getch, cls from util import getch, cls
from time import sleep from time import sleep
lock = threading.Lock()
# #
# The reactive infrastructure # The reactive infrastructure
# #
@@ -11,19 +13,25 @@ class FreqObserver(threading.Thread):
def __init__(self, freqs): def __init__(self, freqs):
threading.Thread.__init__(self) threading.Thread.__init__(self)
self.daemon = True self.daemon = True
self._end = False
# freqs is the data to be observed and reacted to # freqs is the data to be observed and reacted to
self._freqs = freqs self._freqs = freqs
self._freqs_0 = sorted(self._freqs.iteritems(), key=operator.itemgetter(1), reverse=True) self._freqs_0 = sorted(self._freqs.iteritems(), key=operator.itemgetter(1), reverse=True)
self.start() self.start()
def run(self): def run(self):
while True: while not self._end:
lock.acquire()
freqs_1 = sorted(self._freqs.iteritems(), key=operator.itemgetter(1), reverse=True) freqs_1 = sorted(self._freqs.iteritems(), key=operator.itemgetter(1), reverse=True)
lock.release()
if (freqs_1[0:25] != self._freqs_0[0:25]): if (freqs_1[0:25] != self._freqs_0[0:25]):
self._update_display(freqs_1[0:25]) self._update_display(freqs_1[0:25])
self._freqs_0 = freqs_1 self._freqs_0 = freqs_1
sleep(0.01) sleep(0.01)
def stop():
self._end = True
def _update_display(self, tuples): def _update_display(self, tuples):
def refresh_screen(data): def refresh_screen(data):
# clear screen # clear screen
@@ -54,21 +62,19 @@ def get_input():
return True return True
else: pass else: pass
f = open(sys.argv[1]) def characters():
def characters(filename):
c = f.read(1) c = f.read(1)
if c != "": if c != "":
yield c yield c
else: else:
raise StopIteration() raise StopIteration()
def all_words(filename): def all_words():
found_word = False found_word = False
start_char = True start_char = True
while not found_word: while not found_word:
try: try:
c = characters(filename).next() c = characters().next()
except StopIteration: except StopIteration:
raise StopIteration() raise StopIteration()
@@ -87,22 +93,26 @@ def all_words(filename):
found_word = True found_word = True
yield word yield word
def non_stop_words(filename): def non_stop_words():
stopwords = set(open('../stop_words.txt').read().split(',') + list(string.ascii_lowercase)) stopwords = set(open('../stop_words.txt').read().split(',') + list(string.ascii_lowercase))
while True: while True:
w = all_words(filename).next() w = all_words().next()
if not w in stopwords: if not w in stopwords:
yield w yield w
def count_and_sort(filename): def count_and_sort():
freqs = {} freqs = {}
# The declaration for reactive observation of freqs # The declaration for reactive observation of freqs
FreqObserver(freqs) observer = FreqObserver(freqs)
while get_input(): while get_input():
try: try:
w = non_stop_words(filename).next() w = non_stop_words().next()
lock.acquire()
freqs[w] = 1 if w not in freqs else freqs[w]+1 freqs[w] = 1 if w not in freqs else freqs[w]+1
lock.release()
except StopIteration: except StopIteration:
# Let's wait for the observer thread to die gracefully
sleep(1)
break break
# #
@@ -110,6 +120,6 @@ def count_and_sort(filename):
# #
print "Press space bar to fetch words from the file one by one" print "Press space bar to fetch words from the file one by one"
print "Press ESC to switch to automatic mode" print "Press ESC to switch to automatic mode"
count_and_sort(sys.argv[1]) with open(sys.argv[1])as f:
count_and_sort()