def weekly_play_counts_js(self, user, start, end): wpcs = self.weekly_play_counts(user, start, end) n = 0 # number of data points so far # Cumulative average: # CA_i+1 = CA_i + ((x_i+1 - CA_i) / i+1) # where CA_i = last average, # x_i+1 = new entry's value. last_avg = 0.0 for date_idx, wpc in wpcs: n += 1 average = last_avg + (( wpc - last_avg) / n ) last_avg = average yield (ldates.js_timestamp_of_index(date_idx), wpc, average)
def user_weekly_plays_of_artists(self, user, artists, start, end): # initialise the results to a dictionary or artist -> week/playcount, # with all playcounts set to zero. means no need to handle missing # weeks in query set loop. prelim_data = [0] * ((end+1) - start) #dict((ldates.js_timestamp_of_index(x), 0) for x in xrange(start, end+1)) dates = [ ldates.js_timestamp_of_index(idx) for idx in xrange(start, end+1) ] results = dict((artist.id, prelim_data[:]) for artist in artists) for artist_wd in self.user_weeks_between(user, artists, start, end): # place in the appropriate slot in the result list (subtract start) results[artist_wd.artist_id][artist_wd.week_idx - start] = artist_wd.plays out = {} for artist in artists: out[artist] = zip(dates, results[artist.id]) return out