backend/__db_management__/create_products.py
def treat_data(cls):
        """
        This method treats the data mocked by mockaroo by eleminating the repeated name products.
        """
        with open(file=cls.data, mode="r", encoding="utf-8") as file:

            # Saving the product names into a list
            product_names = []
            data = []

            # Defining the dict reader for the names
            reader = DictReader(f=file, fieldnames=cls.headers)
            next(reader)
            for row in reader:
                product_names.append(row.get(cls.headers[0]))
                data.append(row)

        with open(file=cls.data, mode="w", encoding="utf-8", newline="") as new_file:
            not_repeated = []
            repeated = []

            # Algorithm to separate repeated from the not repeated
            for row in data:

                if product_names.count(row.get(cls.headers[0])) == 1:
                    not_repeated.append(row)
                    continue

                already_in = list(filter(lambda el: el[cls.headers[0]] == row[cls.headers[0]], repeated))
                if len(already_in) == 0:
                    repeated.append(row)    

            # Instanciating the DictWriter and writing the headers
            writer = DictWriter(f=new_file, fieldnames=cls.headers)
            writer.writeheader()

            # Saving the not repeated data and one of each repeated
            for row in not_repeated:
                writer.writerow(row)
            if len(repeated) > 0:
                print(f"\nFound {len(repeated)} products repeated:")
                for row in repeated:
                    print(f"'{row[cls.headers[0]]}' found more than once!")
                    writer.writerow(row)

        This method treats the data mocked by mockaroo by eleminating the repeated name products.
        
This method treats the data mocked by mockaroo by eleminating the repeated name products.
This method treats the data mocked by mockaroo by eleminating the repeated name products .
def treat_data(cls):
        with open(file=cls.data, mode="r", encoding="utf-8") as file:
            product_names = []
            data = []
            reader = DictReader(f=file, fieldnames=cls.headers)
            next(reader)
            for row in reader:
                product_names.append(row.get(cls.headers[0]))
                data.append(row)
        with open(file=cls.data, mode="w", encoding="utf-8", newline="") as new_file:
            not_repeated = []
            repeated = []
            for row in data:
                if product_names.count(row.get(cls.headers[0])) == 1:
                    not_repeated.append(row)
                    continue
                already_in = list(filter(lambda el: el[cls.headers[0]] == row[cls.headers[0]], repeated))
                if len(already_in) == 0:
                    repeated.append(row)    
            writer = DictWriter(f=new_file, fieldnames=cls.headers)
            writer.writeheader()
            for row in not_repeated:
                writer.writerow(row)
            if len(repeated) > 0:
                print(f"\nFound {len(repeated)} products repeated:")
                for row in repeated:
                    print(f"'{row[cls.headers[0]]}' found more than once!")
                    writer.writerow(row)
def treat_data ( cls ) : with open ( file = cls . data , mode = "r" , encoding = "utf-8" ) as file : product_names = [ ] data = [ ] reader = DictReader ( f = file , fieldnames = cls . headers ) next ( reader ) for row in reader : product_names . append ( row . get ( cls . headers [ 0 ] ) ) data . append ( row ) with open ( file = cls . data , mode = "w" , encoding = "utf-8" , newline = "" ) as new_file : not_repeated = [ ] repeated = [ ] for row in data : if product_names . count ( row . get ( cls . headers [ 0 ] ) ) == 1 : not_repeated . append ( row ) continue already_in = list ( filter ( lambda el : el [ cls . headers [ 0 ] ] == row [ cls . headers [ 0 ] ] , repeated ) ) if len ( already_in ) == 0 : repeated . append ( row ) writer = DictWriter ( f = new_file , fieldnames = cls . headers ) writer . writeheader ( ) for row in not_repeated : writer . writerow ( row ) if len ( repeated ) > 0 : print ( f"\nFound {len(repeated)} products repeated:" ) for row in repeated : print ( f"'{row[cls.headers[0]]}' found more than once!" ) writer . writerow ( row )
This method treats the data mocked by mockaroo by eleminating the repeated name products.
This method treats the data mocked by mockaroo by eleminating the repeated name products .
"""
        This method treats the data mocked by mockaroo by eleminating the repeated name products.
        """ # Saving the product names into a list # Defining the dict reader for the names # Algorithm to separate repeated from the not repeated # Instanciating the DictWriter and writing the headers # Saving the not repeated data and one of each repeated
cls