<commit_before>from django.core.exceptions import ObjectDoesNotExist


class Scales(object):
    """
    For calculating the weight of a product or basket
    """
    def __init__(self, attribute_code='weight', default_weight=None):
        self.attribute = attribute_code
        self.default_weight = default_weight

    def weigh_product(self, product):
        try:
            attr_val = product.attribute_values.get(attribute__code=self.attribute)
        except ObjectDoesNotExist:
            if self.default_weight is None:
                raise ValueError("No attribute %s found for product %s" % (self.attribute, product))
            weight = self.default_weight
        else:
            weight = attr_val.value
        return float(weight) if weight is not None else 0.0

    def weigh_basket(self, basket):
        weight = 0.0
        for line in basket.lines.all():
            weight += self.weigh_product(line.product)
        return weight
<commit_msg>Basket weight takes line quantity into account<commit_after>from django.core.exceptions import ObjectDoesNotExist


class Scales(object):
    """
    For calculating the weight of a product or basket
    """
    def __init__(self, attribute_code='weight', default_weight=None):
        self.attribute = attribute_code
        self.default_weight = default_weight

    def weigh_product(self, product):
        try:
            attr_val = product.attribute_values.get(attribute__code=self.attribute)
        except ObjectDoesNotExist:
            if self.default_weight is None:
                raise ValueError("No attribute %s found for product %s" % (self.attribute, product))
            weight = self.default_weight
        else:
            weight = attr_val.value
        return float(weight) if weight is not None else 0.0

    def weigh_basket(self, basket):
        weight = 0.0
        for line in basket.lines.all():
            weight += self.weigh_product(line.product) * line.quantity
        return weight

{'fraction_non_alphanumeric': 0.07927747114902157, 'fraction_numerical': 0.004014049172102358, 'mean_word_length': 2.5607142857142855, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4309322', 'n_tokens_mistral': 535, 'n_tokens_neox': 521, 'n_words': 151}