Better active view example -- connect to mvc

This commit is contained in:
Crista Lopes
2014-01-02 09:04:29 -08:00
parent ff89e81aa5
commit b3d36641d3

View File

@@ -1,37 +1,40 @@
#!/usr/bin/env python #!/usr/bin/env python
import sys, operator, string, os, threading, re
import sys, operator, string, os, threading
from util import getch, cls, get_input from util import getch, cls, get_input
from time import sleep from time import sleep
lock = threading.Lock() lock = threading.Lock()
# #
# The reactive infrastructure # The active view
# #
class FreqObserver(threading.Thread): 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 self._end = False
# freqs is the data to be observed and reacted to # freqs is the part of the model to be observed
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 not self._end: while not self._end:
lock.acquire() self._update_view()
freqs_1 = sorted(self._freqs.iteritems(), key=operator.itemgetter(1), reverse=True) sleep(0.1)
lock.release() self._update_view()
if (freqs_1[0:25] != self._freqs_0[0:25]):
self._update_display(freqs_1[0:25])
self._freqs_0 = freqs_1
sleep(0.01)
def stop(self): def stop(self):
self._end = True self._end = True
def _update_view(self):
lock.acquire()
freqs_1 = sorted(self._freqs.iteritems(), key=operator.itemgetter(1), reverse=True)
lock.release()
if (freqs_1[0:25] != self._freqs_0[0:25]):
self._update_display(freqs_1[0:25])
self._freqs_0 = freqs_1
def _update_display(self, tuples): def _update_display(self, tuples):
def refresh_screen(data): def refresh_screen(data):
# clear screen # clear screen
@@ -45,69 +48,37 @@ class FreqObserver(threading.Thread):
refresh_screen(data_str) refresh_screen(data_str)
# #
# The active part, dataflow-like # The model
# #
def characters(): class WordsCounter:
c = f.read(1)
if c != "":
yield c
else:
raise StopIteration()
def all_words():
found_word = False
start_char = True
while not found_word:
try:
c = characters().next()
except StopIteration:
raise StopIteration()
if start_char == True:
word = ""
if c.isalnum():
# We found the start of a word
word = c.lower()
start_char = False
else:
if c.isalnum():
word += c.lower()
else:
# We found the end of a word, emit it
start_char = True
found_word = True
yield word
def non_stop_words():
stopwords = set(open('../stop_words.txt').read().split(',') + list(string.ascii_lowercase))
while True:
w = all_words().next()
if not w in stopwords:
yield w
def count_and_sort():
freqs = {} freqs = {}
# The declaration for reactive observation of freqs def count(self):
observer = FreqObserver(freqs) def non_stop_words():
# Let's get input from the user, or let her stopwords = set(open('../stop_words.txt').read().split(',') + list(string.ascii_lowercase))
# feed the input automatically for line in f:
while get_input(): yield [w for w in re.findall('[a-z]{2,}', line.lower()) if w not in stopwords]
try:
w = non_stop_words().next() words = non_stop_words().next()
lock.acquire() lock.acquire()
freqs[w] = 1 if w not in freqs else freqs[w]+1 for w in words:
lock.release() self.freqs[w] = 1 if w not in self.freqs else self.freqs[w]+1
except StopIteration: lock.release()
# Let's wait for the observer thread to die gracefully
observer.stop()
sleep(1)
break
# #
# The main function # The controller
# #
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"
with open(sys.argv[1])as f: model = WordsCounter()
count_and_sort() view = FreqObserver(model.freqs)
with open(sys.argv[1]) as f:
while get_input():
try:
model.count()
except StopIteration:
# Let's wait for the view thread to die gracefully
view.stop()
sleep(1)
break