Massive renaming!

This commit is contained in:
Crista Lopes
2019-08-12 14:38:16 -07:00
parent e6c1238a56
commit 61d5f74ad9
90 changed files with 0 additions and 0 deletions

16
33-trinity/README.md Normal file
View File

@@ -0,0 +1,16 @@
Style #32
==============================
Constraints:
- The application is divided into three components: the model, the
view and the controller. The model represents the application's
data; the view represents a specific rendition of the data; the
controller provides for input/output, for populating/updating the
model and for invoking the right view.
Possible names:
- Trinity
- Model/View/Controller

83
33-trinity/tf-32-active.py Executable file
View 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,self._end = True, False
# freqs is the part of the model to be observed
self._freqs = freqs
self._freqs_0 = sorted(self._freqs.items(), key=operator.itemgetter(1), reverse=True)[:25]
self.start()
def run(self):
while not self._end:
self._update_view()
sleep(0.1)
self._update_view()
def stop(self):
self._end = True
def _update_view(self):
lock.acquire()
freqs_1 = sorted(self._freqs.items(), key=operator.itemgetter(1), reverse=True)[:25]
lock.release()
if (freqs_1 != self._freqs_0):
self._update_display(freqs_1)
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 = next(non_stop_words())
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

View File

@@ -0,0 +1,50 @@
#!/usr/bin/env python
import sys, re, operator, collections
class WordFrequenciesModel:
""" Models the data. In this case, we're only interested
in words and their frequencies as an end result """
freqs = {}
def __init__(self):
self._observers = []
def register(self, obs):
self._observers.append(obs)
def update(self, path_to_file):
try:
stopwords = set(open('../stop_words.txt').read().split(','))
words = re.findall('[a-z]{2,}', open(path_to_file).read().lower())
self.freqs = collections.Counter(w for w in words if w not in stopwords)
for obs in self._observers:
obs.render()
except IOError:
print("File not found")
self.freqs = {}
class WordFrequenciesView:
def __init__(self, model):
self._model = model
model.register(self)
def render(self):
sorted_freqs = sorted(self._model.freqs.items(), key=operator.itemgetter(1), reverse=True)
for (w, c) in sorted_freqs[:25]:
print(w, '-', c)
class WordFrequencyController:
def __init__(self, model, view):
self._model, self._view = model, view
def run(self):
self._model.update(sys.argv[1])
while True:
print("Next file: ")
sys.stdout.flush()
filename = sys.stdin.readline().strip()
self._model.update(filename)
m = WordFrequenciesModel()
v = WordFrequenciesView(m)
c = WordFrequencyController(m, v)
c.run()

46
33-trinity/tf-33.py Executable file
View File

@@ -0,0 +1,46 @@
#!/usr/bin/env python
import sys, re, operator, collections
class WordFrequenciesModel:
""" Models the data. In this case, we're only interested
in words and their frequencies as an end result """
freqs = {}
stopwords = set(open('../stop_words.txt').read().split(','))
def __init__(self, path_to_file):
self.update(path_to_file)
def update(self, path_to_file):
try:
words = re.findall('[a-z]{2,}', open(path_to_file).read().lower())
self.freqs = collections.Counter(w for w in words if w not in self.stopwords)
except IOError:
print("File not found")
self.freqs = {}
class WordFrequenciesView:
def __init__(self, model):
self._model = model
def render(self):
sorted_freqs = sorted(self._model.freqs.items(), key=operator.itemgetter(1), reverse=True)
for (w, c) in sorted_freqs[0:25]:
print(w, '-', c)
class WordFrequencyController:
def __init__(self, model, view):
self._model, self._view = model, view
view.render()
def run(self):
while True:
print("Next file: ")
sys.stdout.flush()
filename = sys.stdin.readline().strip()
self._model.update(filename)
self._view.render()
m = WordFrequenciesModel(sys.argv[1])
v = WordFrequenciesView(m)
c = WordFrequencyController(m, v)
c.run()

74
33-trinity/util.py Executable file
View File

@@ -0,0 +1,74 @@
import 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