Better version of reactive style. Added README.md
This commit is contained in:
13
32-reactive/README.md
Normal file
13
32-reactive/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
|
||||||
@@ -1,21 +1,44 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
|
|
||||||
import sys, operator, string, os
|
import sys, operator, string, os, threading
|
||||||
from util import getch, cls
|
from util import getch, cls
|
||||||
from time import sleep
|
from time import sleep
|
||||||
|
|
||||||
def refresh_screen(data):
|
#
|
||||||
# clear screen
|
# The reactive infrastructure
|
||||||
cls()
|
#
|
||||||
print data
|
class FreqObserver(threading.Thread):
|
||||||
sys.stdout.flush()
|
def __init__(self, freqs):
|
||||||
|
threading.Thread.__init__(self)
|
||||||
|
self.daemon = True
|
||||||
|
# freqs is the data to be observed and reacted to
|
||||||
|
self._freqs = freqs
|
||||||
|
self._freqs_0 = sorted(self._freqs.iteritems(), key=operator.itemgetter(1), reverse=True)
|
||||||
|
self.start()
|
||||||
|
|
||||||
def update_display(tuples):
|
def run(self):
|
||||||
data_str = ""
|
while True:
|
||||||
for tf in tuples:
|
freqs_1 = sorted(self._freqs.iteritems(), key=operator.itemgetter(1), reverse=True)
|
||||||
data_str += str(tf[0]) + ' - ' + str(tf[1]) + '\n'
|
if (freqs_1[0:25] != self._freqs_0[0:25]):
|
||||||
refresh_screen(data_str)
|
self._update_display(freqs_1[0:25])
|
||||||
|
self._freqs_0 = freqs_1
|
||||||
|
sleep(0.01)
|
||||||
|
|
||||||
|
def _update_display(self, tuples):
|
||||||
|
def refresh_screen(data):
|
||||||
|
# clear screen
|
||||||
|
cls()
|
||||||
|
print data
|
||||||
|
sys.stdout.flush()
|
||||||
|
|
||||||
|
data_str = ""
|
||||||
|
for tf in tuples:
|
||||||
|
data_str += str(tf[0]) + ' - ' + str(tf[1]) + '\n'
|
||||||
|
refresh_screen(data_str)
|
||||||
|
|
||||||
|
#
|
||||||
|
# The active part, dataflow-like
|
||||||
|
#
|
||||||
automatic = False
|
automatic = False
|
||||||
def get_input():
|
def get_input():
|
||||||
global automatic
|
global automatic
|
||||||
@@ -72,25 +95,19 @@ def non_stop_words(filename):
|
|||||||
yield w
|
yield w
|
||||||
|
|
||||||
def count_and_sort(filename):
|
def count_and_sort(filename):
|
||||||
freqs_0 = ()
|
|
||||||
freqs_1 = ()
|
|
||||||
freqs = {}
|
freqs = {}
|
||||||
|
# The declaration for reactive observation of freqs
|
||||||
|
FreqObserver(freqs)
|
||||||
while get_input():
|
while get_input():
|
||||||
try:
|
try:
|
||||||
w = non_stop_words(filename).next()
|
w = non_stop_words(filename).next()
|
||||||
freqs[w] = 1 if w not in freqs else freqs[w]+1
|
freqs[w] = 1 if w not in freqs else freqs[w]+1
|
||||||
freqs_0 = freqs_1
|
|
||||||
freqs_1 = sorted(freqs.iteritems(), key=operator.itemgetter(1), reverse=True)
|
|
||||||
if (freqs_1[0:25] != freqs_0[0:25]):
|
|
||||||
update_display(freqs_1[0:25])
|
|
||||||
except StopIteration:
|
except StopIteration:
|
||||||
break
|
break
|
||||||
return freqs_1
|
|
||||||
|
|
||||||
#
|
#
|
||||||
# The main function
|
# The main function
|
||||||
#
|
#
|
||||||
|
|
||||||
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])
|
count_and_sort(sys.argv[1])
|
||||||
|
|||||||
Reference in New Issue
Block a user