def group_by_lab(yeasts: iter) -> Tuple[list, list]: other_yeasts = [] labs = Yeast.get_labs() yeast_labs = {} # Create lab objects for lab in labs: yeast_labs[lab] = { 'name': lab, 'yeasts': [], } # Assign yeasts (if possible) for yeast in yeasts: yeast_labs[yeast.lab]['yeasts'].append(yeast) # Filter labs with yeasts labs_filtered = [] for yeast_lab in yeast_labs.values(): if len(yeast_lab['yeasts']) > 10: labs_filtered.append(yeast_lab) else: other_yeasts.extend(yeast_lab['yeasts']) return other_yeasts, labs_filtered
def type_overview(request: HttpRequest, type_id: str) -> HttpResponse: types = Yeast.get_types() if type_id not in types: raise Http404('Unknown yeast type %s.' % type_id) type_name = types[type_id] yeasts_query = Yeast.objects.filter(type=type_id, recipes_count__gt=0) (yeasts, labs) = group_by_lab(yeasts_query.order_by('name')) most_popular = [] if yeasts_query.count() > 5: most_popular = yeasts_query.order_by('-recipes_count')[:5] meta = YeastOverviewMeta((type_id, type_name)).get_meta() context = { 'type_name': type_name, 'yeasts': yeasts, 'labs': labs, 'most_popular': most_popular, 'meta': meta, } return render(request, 'yeast/type.html', context)
def group_by_type(yeasts: iter) -> list: yeast_types = {} types = Yeast.get_types() # Create type object for type in types: yeast_types[type] = { 'id': type, 'name': types[type], 'yeasts': [], 'most_popular': [] } # Assign yeasts for yeast in yeasts: yeast_types[yeast.type]['yeasts'].append(yeast) # Filter types with yeasts types_filtered = [] for yeast_type in yeast_types.values(): if len(yeast_type['yeasts']): types_filtered.append(yeast_type) return types_filtered
def load_yeasts(): csv_file = load_csv('yeasts.csv') header = next(csv_file) for row in csv_file: if len(row) == 1: continue # Skip empty lines row = map(cast_values, row) data = dict(zip(header, row)) yeast_id = Yeast.create_id(data['name'], data['lab'], data['brand'], data['product_id']) try: yeast = Yeast.objects.get(pk=yeast_id) except Yeast.DoesNotExist: yeast = Yeast() pass yeast.id = yeast_id for field in data: setattr(yeast, field, data[field]) yeast.save()
def calculate_all_yeast_metrics(self, calculator: YeastMetricCalculator, yeast: Yeast) -> None: yeast.recipes_count = calculator.get_recipes_count(yeast)