Renamed 32 to active view to better reflect the program
This commit is contained in:
13
32-active-view/README.md
Normal file
13
32-active-view/README.md
Normal file
@@ -0,0 +1,13 @@
|
||||
Style #32
|
||||
==============================
|
||||
|
||||
Constraints:
|
||||
|
||||
- Variables are updated automatically based on changing values of
|
||||
other variables. Variable values possibly depend on data streams
|
||||
(keyboard, mouse, etc.)
|
||||
|
||||
Possible names:
|
||||
|
||||
- Reactive
|
||||
- Data-to-data
|
||||
83
32-active-view/tf-32.py
Executable file
83
32-active-view/tf-32.py
Executable file
@@ -0,0 +1,83 @@
|
||||
#!/usr/bin/env python
|
||||
import sys, operator, string, os, threading, re
|
||||
from util import getch, cls, get_input
|
||||
from time import sleep
|
||||
|
||||
lock = threading.Lock()
|
||||
|
||||
#
|
||||
# The active view
|
||||
#
|
||||
class FreqObserver(threading.Thread):
|
||||
def __init__(self, freqs):
|
||||
threading.Thread.__init__(self)
|
||||
self.daemon = True
|
||||
self._end = False
|
||||
# freqs is the part of the model to be observed
|
||||
self._freqs = freqs
|
||||
self._freqs_0 = sorted(self._freqs.iteritems(), key=operator.itemgetter(1), reverse=True)
|
||||
self.start()
|
||||
|
||||
def run(self):
|
||||
while not self._end:
|
||||
self._update_view()
|
||||
sleep(0.1)
|
||||
|
||||
def stop(self):
|
||||
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 refresh_screen(data):
|
||||
# clear screen
|
||||
cls()
|
||||
print data
|
||||
sys.stdout.flush()
|
||||
|
||||
data_str = ""
|
||||
for (w, c) in tuples:
|
||||
data_str += str(w) + ' - ' + str(c) + '\n'
|
||||
refresh_screen(data_str)
|
||||
|
||||
#
|
||||
# The model
|
||||
#
|
||||
class WordsCounter:
|
||||
freqs = {}
|
||||
def count(self):
|
||||
def non_stop_words():
|
||||
stopwords = set(open('../stop_words.txt').read().split(',') + list(string.ascii_lowercase))
|
||||
for line in f:
|
||||
yield [w for w in re.findall('[a-z]{2,}', line.lower()) if w not in stopwords]
|
||||
|
||||
words = non_stop_words().next()
|
||||
lock.acquire()
|
||||
for w in words:
|
||||
self.freqs[w] = 1 if w not in self.freqs else self.freqs[w]+1
|
||||
lock.release()
|
||||
|
||||
#
|
||||
# The controller
|
||||
#
|
||||
print "Press space bar to fetch words from the file one by one"
|
||||
print "Press ESC to switch to automatic mode"
|
||||
model = WordsCounter()
|
||||
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
|
||||
|
||||
|
||||
75
32-active-view/util.py
Executable file
75
32-active-view/util.py
Executable file
@@ -0,0 +1,75 @@
|
||||
import sys, os
|
||||
|
||||
#
|
||||
# getch in a platform-independent way
|
||||
# Credit: http://code.activestate.com/recipes/134892/
|
||||
#
|
||||
class _Getch:
|
||||
"""Gets a single character from standard input. Does not echo to the
|
||||
screen."""
|
||||
def __init__(self):
|
||||
try:
|
||||
self.impl = _GetchWindows()
|
||||
except ImportError:
|
||||
try:
|
||||
self.impl = _GetchMacCarbon()
|
||||
except(AttributeError, ImportError):
|
||||
self.impl = _GetchUnix()
|
||||
|
||||
def __call__(self): return self.impl()
|
||||
|
||||
|
||||
class _GetchUnix:
|
||||
def __init__(self):
|
||||
import tty, sys
|
||||
|
||||
def __call__(self):
|
||||
import sys, tty, termios
|
||||
fd = sys.stdin.fileno()
|
||||
old_settings = termios.tcgetattr(fd)
|
||||
try:
|
||||
tty.setraw(sys.stdin.fileno())
|
||||
ch = sys.stdin.read(1)
|
||||
finally:
|
||||
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
|
||||
return ch
|
||||
|
||||
class _GetchWindows:
|
||||
def __init__(self):
|
||||
import msvcrt
|
||||
def __call__(self):
|
||||
import msvcrt
|
||||
return msvcrt.getch()
|
||||
|
||||
class _GetchMacCarbon:
|
||||
def __init__(self):
|
||||
import Carbon
|
||||
|
||||
def __call__(self):
|
||||
import Carbon
|
||||
if Carbon.Evt.EventAvail(0x0008)[0]==0: # 0x0008 is the keyDownMask
|
||||
return ''
|
||||
else:
|
||||
(what,msg,when,where,mod)=Carbon.Evt.GetNextEvent(0x0008)[1]
|
||||
return chr(msg & 0x000000FF)
|
||||
|
||||
getch = _Getch()
|
||||
|
||||
def cls():
|
||||
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