def venues_from_top_n_categories(m=40, n=5): """ Gets the top m venues from the top n categories ordered by shout_count :param m: the number of venues from each category to return :type m: int :param n: the number of categories to return :type n: int :return: 2 lists - one for categories, and one for venues :rtype: ([], []) """ cats = Venue.raw('SELECT cat_id, cat_name ' 'FROM venue ' 'GROUP BY cat_id ' 'ORDER BY sum(shout_count) DESC ' 'LIMIT ?', n) cat_list = [] venue_list = [] for v in cats.execute(): vens = [ven for ven in (Venue .select() .where(Venue.cat_id == v.cat_id) .order_by(-Venue.shout_count) .limit(m))] cat_list.append(v.cat_name) venue_list.extend(vens) return cat_list, venue_list
def get_categories(n): """ Gets set of categories for venues whose shout_count > n. :param n: shout_count > n :type n: int :return: set of category names :rtype: [str] """ return [ven.cat_name for ven in Venue.raw('select distinct cat_name ' 'from venue ' 'where id in ' '(select venue.id ' 'from venue ' 'where shout_count > ?)', n)]