Word Cloud: docstrings + pep8
This commit is contained in:
@@ -9,7 +9,6 @@ If student have answered - words he entered and cloud.
|
||||
import json
|
||||
import logging
|
||||
|
||||
from lxml import etree
|
||||
from pkg_resources import resource_string
|
||||
from xmodule.raw_module import RawDescriptor
|
||||
from xmodule.x_module import XModule
|
||||
@@ -25,24 +24,51 @@ def pretty_bool(value):
|
||||
|
||||
|
||||
class WordCloudDescriptorFields(object):
|
||||
# Name of poll to use in links to this poll
|
||||
display_name = String(help="Display name for this module", scope=Scope.settings)
|
||||
num_inputs = Integer(help="Number of inputs", scope=Scope.settings, default=5)
|
||||
num_top_words = Integer(help="Number of max words, which will be displayed.", scope=Scope.settings, default=250)
|
||||
display_student_percents = Boolean(help="Dispaly usage percents for each word.", scope=Scope.settings, default=True)
|
||||
"""Word Cloud fields from xml for descriptor."""
|
||||
display_name = String(
|
||||
help="Display name for this module",
|
||||
scope=Scope.settings
|
||||
)
|
||||
num_inputs = Integer(
|
||||
help="Number of inputs.",
|
||||
scope=Scope.settings,
|
||||
default=5
|
||||
)
|
||||
num_top_words = Integer(
|
||||
help="Number of max words, which will be displayed.",
|
||||
scope=Scope.settings,
|
||||
default=250
|
||||
)
|
||||
display_student_percents = Boolean(
|
||||
help="Dispaly usage percents for each word.",
|
||||
scope=Scope.settings,
|
||||
default=True
|
||||
)
|
||||
|
||||
|
||||
class WordCloudFields(object):
|
||||
|
||||
submitted = Boolean(help="Whether this student has posted words to the cloud", scope=Scope.user_state, default=False)
|
||||
student_words = List(help="Student answer", scope=Scope.user_state, default=[])
|
||||
|
||||
all_words = Object(help="All possible words from other students", scope=Scope.content)
|
||||
top_words = Object(help="Top N words for word cloud", scope=Scope.content)
|
||||
submitted = Boolean(
|
||||
help="Whether this student has posted words to the cloud.",
|
||||
scope=Scope.user_state,
|
||||
default=False
|
||||
)
|
||||
student_words = List(
|
||||
help="Student answer.",
|
||||
scope=Scope.user_state,
|
||||
default=[]
|
||||
)
|
||||
all_words = Object(
|
||||
help="All possible words from other students.",
|
||||
scope=Scope.content
|
||||
)
|
||||
top_words = Object(
|
||||
help="Top N words for word cloud.",
|
||||
scope=Scope.content
|
||||
)
|
||||
|
||||
|
||||
class WordCloudModule(WordCloudFields, WordCloudDescriptorFields, XModule):
|
||||
"""WordCloud Module"""
|
||||
"""WordCloud Xmodule"""
|
||||
js = {
|
||||
'coffee': [resource_string(__name__, 'js/src/javascript_loader.coffee')],
|
||||
'js': [resource_string(__name__, 'js/src/word_cloud/logme.js'),
|
||||
@@ -61,7 +87,9 @@ class WordCloudModule(WordCloudFields, WordCloudDescriptorFields, XModule):
|
||||
return json.dumps({
|
||||
'status': 'success',
|
||||
'submitted': True,
|
||||
'display_student_percents': pretty_bool(self.display_student_percents),
|
||||
'display_student_percents': pretty_bool(
|
||||
self.display_student_percents
|
||||
),
|
||||
'student_words': {
|
||||
word: self.all_words[word] for word in self.student_words
|
||||
},
|
||||
@@ -86,14 +114,19 @@ class WordCloudModule(WordCloudFields, WordCloudDescriptorFields, XModule):
|
||||
"""Convert words dictionary for client API."""
|
||||
list_to_return = []
|
||||
percents = 0
|
||||
current_num_top_words = len(top_words)
|
||||
for num, word_tuple in enumerate(top_words.iteritems()):
|
||||
if num == current_num_top_words - 1:
|
||||
if num == len(top_words) - 1:
|
||||
percent = 100 - percents
|
||||
else:
|
||||
percent = round(100.0 * word_tuple[1] / total_count)
|
||||
percents += percent
|
||||
list_to_return.append({'text': word_tuple[0], 'size': word_tuple[1], 'percent': percent})
|
||||
list_to_return.append(
|
||||
{
|
||||
'text': word_tuple[0],
|
||||
'size': word_tuple[1],
|
||||
'percent': percent
|
||||
}
|
||||
)
|
||||
return list_to_return
|
||||
|
||||
def top_dict(self, dict_obj, amount):
|
||||
@@ -160,20 +193,20 @@ class WordCloudModule(WordCloudFields, WordCloudDescriptorFields, XModule):
|
||||
})
|
||||
|
||||
def get_html(self):
|
||||
"""Renders parameters to template."""
|
||||
params = {
|
||||
"""Template rendering."""
|
||||
context = {
|
||||
'element_id': self.location.html_id(),
|
||||
'element_class': self.location.category,
|
||||
'ajax_url': self.system.ajax_url,
|
||||
'num_inputs': int(self.num_inputs),
|
||||
'submitted': self.submitted
|
||||
}
|
||||
self.content = self.system.render_template('word_cloud.html', params)
|
||||
self.content = self.system.render_template('word_cloud.html', context)
|
||||
return self.content
|
||||
|
||||
|
||||
class WordCloudDescriptor(WordCloudDescriptorFields, RawDescriptor):
|
||||
|
||||
"""Descriptor for WordCloud Xmodule."""
|
||||
module_class = WordCloudModule
|
||||
template_dir_name = 'word_cloud'
|
||||
stores_state = True
|
||||
|
||||
Reference in New Issue
Block a user