def collapsed_score(intervals): return calcAvg([calcAvg(numList) for numList in intervals])
def showBookRangeGraphHelper(params, synthesizedParams, startDate, endDate, startDateISO, endDateISO, numIntervals): info = awsRequest.Cmds().interval_by_day(False, startDate, endDate, numIntervals) class DataTable(object): pass class ColumnDescriptor(object): def __init__(self, columnType, value): self.columnType = columnType self.value = value def __str__(self): return self.value __unicode__ = __str__ asin_interval_scores = info['asin_interval_scores'] date_intervals = info['date_intervals'] interval_asin_scores = info['interval_asin_scores'] book_titles_by_asin = info['book_titles_by_asin'] interval_asin_tweets = info['interval_asin_tweets'] interval_asin_tweet_percentages = info['interval_asin_tweet_percentages'] ranked_asins = sortedAsinsByRank(asin_interval_scores) dateFormatter = awsRequest.DateFormatterSelector(date_intervals) num_asins = len(ranked_asins) dataTable = DataTable() dataTable.addColumns = ([ColumnDescriptor('string', 'month')] + [ColumnDescriptor('number', book_titles_by_asin[asin]) for asin in ranked_asins ]) rows = [] for i in range(len(date_intervals)): this_interval = date_intervals[i] row = (["'%s'" % dateFormatter.format(this_interval)] + ["%.3g" % (calcAvg(interval_asin_scores[i].get(this_asin, [])) + (10.0 * interval_asin_tweet_percentages[i].get(this_asin, 0))) for this_asin in ranked_asins]) rows.append(row) dataTable.addRows = rows asinPosTweetCounts = dict([(this_asin, []) for this_asin in ranked_asins]) for i in range(len(date_intervals)): for this_asin in ranked_asins: tweets = interval_asin_tweets[i].get(this_asin, []) asinPosTweetCounts[this_asin].append(len(tweets)) asinPosCollapsedTweetCounts = [] for this_asin in ranked_asins: asinPosCollapsedTweetCounts.append(",".join([str(a) for a in asinPosTweetCounts[this_asin]])) dataTable.asinPosCollapsedTweetCounts = asinPosCollapsedTweetCounts import pprint print "Hey, dataTable.rows...." pprint.pprint(dataTable.addRows) books = Book.objects.all() book_dict = dict([(bookObject.asin, bookObject) for bookObject in books]) import sys sys.stderr.write("medium urls: %s\n" % ", ".join([book_dict[asin].mediumProductImageURL for asin in ranked_asins])) prevWeekStartDate = startDate - datetime.timedelta(numIntervals) prevWeekEndDate = startDate; prevWeekLinkOK = not awsRequest.checkRange(prevWeekStartDate, prevWeekEndDate) nextWeekStartDate = endDate nextWeekEndDate = endDate + datetime.timedelta(numIntervals) nextWeekLinkOK = not awsRequest.checkRange(nextWeekStartDate, nextWeekEndDate) return render_to_response("amazon/showBookRankGraph.django.html", { 'book_asins': ranked_asins, 'book_titles': [book_titles_by_asin[asin] for asin in ranked_asins], 'product_small_urls': [book_dict[asin].smallProductImageURL for asin in ranked_asins], 'product_medium_urls': [book_dict[asin].mediumProductImageURL for asin in ranked_asins], 'startDate': startDate, 'endDate': endDate, 'isoDates': [toYYMMDD(dt) for dt in date_intervals], 'startDateISO': startDateISO, 'endDateISO': endDateISO, 'endDateSub1ISO': toYYMMDD(endDate - datetime.timedelta(1)), 'dataTable': dataTable, 'prevWeekStartDate': toYYMMDD(prevWeekStartDate), 'prevWeekEndDate': toYYMMDD(prevWeekEndDate), 'prevWeekLinkOK': prevWeekLinkOK, 'nextWeekStartDate': toYYMMDD(nextWeekStartDate), 'nextWeekEndDate': toYYMMDD(nextWeekEndDate), 'nextWeekLinkOK': nextWeekLinkOK, })