def run(self): pages = WikiPage.all() pages.filter('unit', self.unit) page_iter = pages.run() ct = CrossTab() vals_by_field = defaultdict(set) fields = [field.name for field in forms[self.unit]() if isinstance(field, BaseRankingField)] for page in page_iter: author = page.author ranks = {'group_id': author.group_id} # want to tabulate: # ranks of each item: dogs=1, cats=2, bunnies=3 # networking group of the student for field in fields: value = getattr(page, field) if not value: continue for r in value: vals_by_field[field].add(r) ranks.update((item, n) for n, item in enumerate(value, start=1)) ct.add(**ranks) for field, values in vals_by_field.iteritems(): yield dict(zip(self.fields, [field])) for v in values: for row in ct.table(v, 'group_id'): yield dict(zip(self.fields, row)) yield {}
def run(self): pages = WikiPage.all() pages.filter('unit', self.unit) page_iter = pages.run() ct = CrossTab() vals_by_field = defaultdict(set) fields = [ field.name for field in forms[self.unit]() if isinstance(field, BaseRankingField) ] for page in page_iter: author = page.author ranks = {'group_id': author.group_id} # want to tabulate: # ranks of each item: dogs=1, cats=2, bunnies=3 # networking group of the student for field in fields: value = getattr(page, field) if not value: continue for r in value: vals_by_field[field].add(r) ranks.update( (item, n) for n, item in enumerate(value, start=1)) ct.add(**ranks) for field, values in vals_by_field.iteritems(): yield dict(zip(self.fields, [field])) for v in values: for row in ct.table(v, 'group_id'): yield dict(zip(self.fields, row)) yield {}
class CommentCountQuery(TableMakerMapper): KIND = WikiComment FIELDS = [ 'c%d' % n for n in range(40) ] def __init__(self, **kwargs): TableMakerMapper.__init__(self) self.tab = CrossTab() self.word_tab = CrossTab() self.names = {} _re_word_boundaries = re.compile(r'\b') def num_words(self, string): return len(self._re_word_boundaries.findall(string)) >> 1 def map(self, comment): email = comment.author_email if email not in self.names: self.names[email] = comment.author.name unit = comment.topic.unit if unit is None: unit = 'Profile' self.tab.add(email=email, comments_in_unit=unit) comment_text = Markup(comment.text).striptags() self.word_tab.add_count( self.num_words(comment_text), email=email, words_in_unit=unit) return ([], []) def finish(self): for comments_row, words_row in itertools.izip( self.tab.table('email', 'comments_in_unit'), self.word_tab.table('email', 'words_in_unit')): blank_or_email = comments_row[0] if ':' in blank_or_email: blank_or_email = blank_or_email.split(':', 1)[1] name = self.names.get(blank_or_email, '') row = [name, blank_or_email] for comment_cell, word_cell in zip( comments_row[1:], words_row[1:]): row.extend([comment_cell, word_cell]) self.add_row(dict(zip(self.FIELDS, row)))
class CommentCountQuery(TableMakerMapper): KIND = WikiComment FIELDS = ['c%d' % n for n in range(40)] def __init__(self, **kwargs): TableMakerMapper.__init__(self) self.tab = CrossTab() self.word_tab = CrossTab() self.names = {} _re_word_boundaries = re.compile(r'\b') def num_words(self, string): return len(self._re_word_boundaries.findall(string)) >> 1 def map(self, comment): email = comment.author_email if email not in self.names: self.names[email] = comment.author.name unit = comment.topic.unit if unit is None: unit = 'Profile' self.tab.add(email=email, comments_in_unit=unit) comment_text = Markup(comment.text).striptags() self.word_tab.add_count(self.num_words(comment_text), email=email, words_in_unit=unit) return ([], []) def finish(self): for comments_row, words_row in itertools.izip( self.tab.table('email', 'comments_in_unit'), self.word_tab.table('email', 'words_in_unit')): blank_or_email = comments_row[0] if ':' in blank_or_email: blank_or_email = blank_or_email.split(':', 1)[1] name = self.names.get(blank_or_email, '') row = [name, blank_or_email] for comment_cell, word_cell in zip(comments_row[1:], words_row[1:]): row.extend([comment_cell, word_cell]) self.add_row(dict(zip(self.FIELDS, row)))
class FixedUnitRankingQuery(TableMakerMapper): FIELDS = ['c%d' % n for n in xrange(30)] KIND = WikiPage FILTERS = [] def __init__(self, **kwargs): self.unit = kwargs['unit'] if not self.unit: raise ValueError('"unit" parameter is required for this query') self.FILTERS = [('unit', self.unit)] self.ct = CrossTab() self.vals_by_field = defaultdict(set) self.ranking_fields = [ field.name for field in forms[self.unit]() if isinstance(field, BaseRankingField) ] super(FixedUnitRankingQuery, self).__init__() def map(self, page): author = page.author # want to tabulate: # ranks of each item: dogs=1, cats=2, bunnies=3 # networking group of the student for field in self.ranking_fields: ranks = {'group_id': author.group_id} value = getattr(page, field) if not value: continue for r in value: self.vals_by_field[field].add(r) ranks.update((item, n) for n, item in enumerate(value, start=1)) self.ct.add(**ranks) def finish(self): # go over each field of the page for field, values in self.vals_by_field.iteritems(): self.add_row(dict(zip(self.FIELDS, [field]))) # Go over each value, giving its rankings for v in values: for row in self.ct.table(v, 'group_id'): self.add_row(dict(zip(self.FIELDS, row))) self.add_row({})
class FixedUnitRankingQuery(TableMakerMapper): FIELDS = ['c%d' % n for n in xrange(30)] KIND = WikiPage FILTERS = [] def __init__(self, **kwargs): self.unit = kwargs['unit'] if not self.unit: raise ValueError('"unit" parameter is required for this query') self.FILTERS = [('unit', self.unit)] self.ct = CrossTab() self.vals_by_field = defaultdict(set) self.ranking_fields = [field.name for field in forms[self.unit]() if isinstance(field, BaseRankingField)] super(FixedUnitRankingQuery, self).__init__() def map(self, page): author = page.author # want to tabulate: # ranks of each item: dogs=1, cats=2, bunnies=3 # networking group of the student for field in self.ranking_fields: ranks = {'group_id': author.group_id} value = getattr(page, field) if not value: continue for r in value: self.vals_by_field[field].add(r) ranks.update((item, n) for n, item in enumerate(value, start=1)) self.ct.add(**ranks) def finish(self): # go over each field of the page for field, values in self.vals_by_field.iteritems(): self.add_row(dict(zip(self.FIELDS, [field]))) # Go over each value, giving its rankings for v in values: for row in self.ct.table(v, 'group_id'): self.add_row(dict(zip(self.FIELDS, row))) self.add_row({})