<commit_before>from .dataerror import DataValidationError
class Shopcart(object):
    """
    This is model for the shop carts
    Assumptions:
        - In memory persistence
        - The fields of a shopcart include:
            - user id
            - Dictionary of products { product_id: quantity_of_product}
        - The total price of the shopcart will be dynamically calculated
    """
    __data = []
    __index = 0

    def __init__(self, uid=0, products=None):
        """
        :param uid: user id
        :param products: dict of products <products id, quantity of product>
        """
        self.uid = int(uid)
        self.products = self.__validate_products(products)
        
    def save(self):
        """ Saves a Shopcart in the database """
        Shopcart.__data.append(self)

    def add_product(self, pid, quant=1):
        """ Adds a tuple of product, quantity to the product dict """
        pq_tup = (pid,quant)
        self.products.update( self.__validate_products(pq_tup) )

    @staticmethod
    def all():
        """ Query that returns all Shopcarts """
        return Shopcart.__data

    @staticmethod
    def remove_all():
        """ Remove all Shopcarts from memory """
        Shopcart.__data = []

    @staticmethod
    def __validate_products(products):
        """ Validates products or raises an error"""
        try:
            # Product is none so set an empty list
            if products is None:
                return {}
            
            #Not None
            # Tuple of product, quantity 
            if type(products) == tuple  and len(products)==2 and all(isinstance(pq,int) for pq in products): 
                return {products[0]:products[1]}

            # Just a Product id, set default quantity to 1
            if type(products) == int: 
                return {products:1}

            if type(products) != dict : 
                raise DataValidationError("Invalid format for products")

            
            # Products is a dict of proper tuples
            if ( all( isinstance(pid,int) for pid in products.keys() ) and
                 all( (isinstance(q,int) and (q > 0)) for q in products.values() ) ):
                return products

            #Products not valid
            raise DataValidationError("Invalid format for products")

        except DataValidationError as e:
            print('ERROR: Data Validation error' +e.args[0])
            return {}<commit_msg>Validating products dont handles error anymore<commit_after>from .dataerror import DataValidationError
class Shopcart(object):
    """
    This is model for the shop carts
    Assumptions:
        - In memory persistence
        - The fields of a shopcart include:
            - user id
            - Dictionary of products { product_id: quantity_of_product}
        - The total price of the shopcart will be dynamically calculated
    """
    __data = []
    __index = 0

    def __init__(self, uid=0, products=None):
        """
        :param uid: user id
        :param products: dict of products <products id, quantity of product>
        """
        self.uid = int(uid)
        try:
            self.products = self.__validate_products(products)
        except DataValidationError as e:
            print('ERROR: Data Validation error' +e.args[0])           

    def save(self):
        """ Saves a Shopcart in the database """
        Shopcart.__data.append(self)

    def add_product(self, pid, quant=1):
        """ Adds a tuple of product, quantity to the product dict """
        pq_tup = (pid,quant)
        try:
            self.products.update( self.__validate_products(pq_tup) )
        except DataValidationError as e:
            print('ERROR: Data Validation error' +e.args[0])


    @staticmethod
    def all():
        """ Query that returns all Shopcarts """
        return Shopcart.__data

    @staticmethod
    def remove_all():
        """ Remove all Shopcarts from memory """
        Shopcart.__data = []

    @staticmethod
    def __validate_products(products):
        """ Validates products or raises an error"""
        # Product is none so set an empty list
        if products is None:
            return {}

        #Not None
        # Tuple of product, quantity 
        if type(products) == tuple  and len(products)==2 and all(isinstance(pq,int) for pq in products): 
            return {products[0]:products[1]}

        # Just a Product id, set default quantity to 1
        if type(products) == int: 
            return {products:1}

        if type(products) != dict : 
            raise DataValidationError("Invalid format for products")

        # Products is a dict of proper tuples
        if ( all( isinstance(pid,int) for pid in products.keys() ) and
             all( (isinstance(q,int) and (q > 0)) for q in products.values() ) ):
            return products

        #Products not valid
        raise DataValidationError("Invalid format for products")
{'fraction_non_alphanumeric': 0.08975393303751512, 'fraction_numerical': 0.0042355788624445345, 'mean_word_length': 2.0256253813300793, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '1633171', 'n_tokens_mistral': 1312, 'n_tokens_neox': 1255, 'n_words': 452}