Changeset 199
- Timestamp:
- 08/23/08 16:27:40 (10 months ago)
- Location:
- trunk/shakespeare
- Files:
-
- 7 modified
- 1 copied
-
controllers/stats.py (modified) (2 diffs)
-
stats.py (modified) (1 diff)
-
templates/stats/index.html (modified) (1 diff)
-
templates/stats/text.html (modified) (1 diff)
-
templates/stats/word.html (copied) (copied from trunk/shakespeare/templates/stats/text.html) (3 diffs)
-
tests/__init__.py (modified) (2 diffs)
-
tests/functional/test_stats.py (modified) (2 diffs)
-
tests/test_stats.py (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/shakespeare/controllers/stats.py
r193 r199 11 11 def index(self): 12 12 return render('stats/index') 13 13 14 def text_index(self): 15 # only get those texts with stats 16 c.texts = model.Material.query.all() 17 import shakespeare.controllers.text 18 ctrl = shakespeare.controllers.text.TextController() 19 return ctrl.index() 20 14 21 def text(self, id): 15 22 text_name = id 16 23 text = model.Material.byName(text_name) 24 # no id or no text by that id 25 if not text: 26 return self.text_index() 17 27 stats = shakespeare.stats.Stats() 18 28 c.text = text … … 23 33 return render('stats/text') 24 34 35 def word_index(self): 36 return '' 37 38 def word(self, id): 39 if id is None: 40 return self.word_index() 41 word = id 42 c.word = word 43 stats = shakespeare.stats.Stats() 44 c.stats = stats.word_stats(word) 45 # will not have that many texts so do not need to limit c.stats 46 data = [ (s.text.title, s.freq) for s in c.stats ] 47 c.img_url = self.vertical_bar_chart(data) 48 return render('stats/word') 49 25 50 # TODO: factor this out to its module (?) 26 51 def vertical_bar_chart(self, data, width=500): 52 if not data: 53 return '' 27 54 # tranpose 28 55 tdata = zip(*data) -
trunk/shakespeare/stats.py
r192 r199 60 60 61 61 def text_stats(self, text): 62 '''Return word statistics for text, most popular word first.''' 63 stats = model.Statistic.query.order_by(model.Statistic.freq.desc()).all() 62 '''Statistics for text, most popular word first.''' 63 stats = model.Statistic.query.filter_by(text=text).order_by( 64 model.Statistic.freq.desc() 65 ).all() 64 66 return stats 65 67 68 def word_stats(self, word): 69 '''Statistics for word (i.e. which texts use it) in order or 70 usage.''' 71 stats = model.Statistic.query.filter_by(word=word).order_by( 72 model.Statistic.freq.desc() 73 ).all() 74 return stats 75 -
trunk/shakespeare/templates/stats/index.html
r187 r199 5 5 6 6 <div py:match="content"> 7 <p>This section provides statistical information about word occurences in 8 the various textual materials available on the site. If you know the name 9 of a text you can get information by visiting ./text_name/ 7 <p>This section provides statistical information about the various textual 8 materials available on the site. 10 9 </p> 10 <p> 11 Currently information is provided organized by: 12 </p> 13 <ul> 14 <li> 15 <a href="${h.url_for(controller='stats', action='text', 16 id=None)}">Text</a> 17 </li> 18 <li> 19 <a href="${h.url_for(controller='stats', action='word', 20 id=None)}">Word</a> 21 </li> 22 </ul> 11 23 </div> 12 24 -
trunk/shakespeare/templates/stats/text.html
r193 r199 9 9 alt="Word Statistics Bar Chart" /> 10 10 11 <table border="1" style="margin-left: 550px;"> 11 <p py:if="not c.stats"> 12 Sorry, no statistics are available for ${c.text.title} (name: ${c.text.name}) 13 </p> 14 <table style="margin-left: 550px;"> 12 15 <thead> 13 16 <tr> -
trunk/shakespeare/templates/stats/word.html
r193 r199 2 2 xmlns:xi="http://www.w3.org/2001/XInclude"> 3 3 4 <py:def function="page_title">Stats for ${c.text.title}</py:def>4 <py:def function="page_title">Stats for '${c.word}'</py:def> 5 5 6 6 <div py:match="content"> … … 9 9 alt="Word Statistics Bar Chart" /> 10 10 11 <table border="1" style="margin-left: 550px;"> 11 <p py:if="not c.stats"> 12 Sorry, no statistics are available for ${c.text.title} (name: ${c.text.name}) 13 </p> 14 <table style="margin-left: 550px;"> 12 15 <thead> 13 16 <tr> 14 17 <th>Index</th> 15 18 <th> 16 Word19 Text 17 20 </th> 18 21 <th> … … 27 30 </td> 28 31 <td> 29 ${stat. word}32 ${stat.text.title} 30 33 </td> 31 34 <td> -
trunk/shakespeare/tests/__init__.py
r187 r199 18 18 from routes import url_for 19 19 20 __all__ = ['url_for', 'TestController', 'make_fixture' ]20 __all__ = ['url_for', 'TestController', 'make_fixture', 'make_fixture2' ] 21 21 22 22 here_dir = os.path.dirname(os.path.abspath(__file__)) … … 66 66 return sonnet18 67 67 68 def make_fixture2(): 69 import shakespeare.model as model 70 sonnet18_name = 'test_sonnet18_2' 71 sonnet18 = model.Material.byName(sonnet18_name) 72 if not sonnet18: 73 sonnet18 = model.Material(name=sonnet18_name, 74 title='Sonnet 18 Duplicate', 75 ) 76 model.Session.flush() 77 sonnet18.content = sonnet18_text 78 return sonnet18 68 79 69 80 class TestController(object): -
trunk/shakespeare/tests/functional/test_stats.py
r187 r199 8 8 9 9 text = make_fixture() 10 text2 = make_fixture2() 10 11 11 12 def setUp(self): … … 21 22 res = self.app.get(url) 22 23 assert 'Stats' in res 24 25 def test_text_stats_index(self): 26 url = url_for(controller='stats', action='text', id=None) 27 res = self.app.get(url) 28 assert self.text.name in res 29 assert self.text2.name in res 23 30 24 def test_stats(self): 25 text = make_fixture() 31 def test_text_stats(self): 26 32 url = url_for(controller='stats', action='text', id=self.text.name) 27 33 res = self.app.get(url) 28 34 assert 'summer' in res 29 35 36 # TODO: stats for a text with no associated items 37 def test_text_no_stats(self): 38 url = url_for(controller='stats', action='text', id=self.text2.name) 39 res = self.app.get(url) 40 assert 'Sorry, no statistics' in res 41 42 def test_word_stats(self): 43 shakespeare.tests.test_stats.stats_fixture(self.text2) 44 word = 'summer' 45 url = url_for(controller='stats', action='word', id=word) 46 res = self.app.get(url) 47 assert 'summer' in res 48 assert self.text.title in res 49 assert self.text2.title in res 50 assert '3' in res 51 -
trunk/shakespeare/tests/test_stats.py
r187 r199 18 18 self.stats = shakespeare.stats.Stats() 19 19 self.text = make_fixture() 20 self.text2 = make_fixture2() 20 21 model.Session.begin() 21 22 … … 44 45 45 46 def test_text_stats(self): 47 # create stats for at least 2 texts to make sure we only pick up one 46 48 stats_fixture(self.text) 49 stats_fixture(self.text2) 47 50 48 51 stats = self.stats.text_stats(self.text) … … 54 57 assert stats[2].word == 'summer' 55 58 assert stats[2].freq == 3 59 60 def test_word_stats(self): 61 stats_fixture(self.text) 62 stats_fixture(self.text2) 63 stats = self.stats.word_stats('summer') 64 assert len(stats) == 2 65 assert stats[0].text.name == self.text.name 66 assert stats[0].freq == 3 67 # same text so should be the same! 68 assert stats[0].freq == stats[1].freq 56 69 57 70 58
