示例#1
0
    def load_categories(self):
        for filename in os.listdir(self.database):
            category_name = os.path.splitext(filename)[0]

            category = Category(self.database, category_name, self.stop_list)
            if category.load():
                self.total_articles += category.article_count
                self.categories[category_name] = category
示例#2
0
    def load_categories(self):
        for filename in os.listdir(self.database):
            category_name = os.path.splitext(filename)[0]

            category = Category(self.database, category_name, self.stop_list)
            if category.load():
                self.total_articles += category.article_count
                self.categories[category_name] = category
    def find_substitute(self):
        """
        used for finding a substitute from a product selected by the user
        """
        print('Sélectionnez la catégorie:')

        # display categories
        Category.listing()

        # ask the user to choose a category
        category_user = input(':')
        try:
            category_user = int(category_user)
            assert 0 < category_user <= len(Category.load())
        except ValueError:
            return self.find_substitute()
        except AssertionError:
            return self.find_substitute()

        # load product from the chosen category
        for id, category in Category.load().items():
            if int(category_user) == id:

                # load and display products
                products = Product.load(category.id)
                print('Sélectionnez un produit:')
                Product.listing(products)

                # ask the user to choose a product
                product_user = input(':')
                try:
                    product_user = int(product_user)
                    assert 0 < product_user <= len(products) + 1
                except ValueError:
                    self.find_substitute()
                except AssertionError:
                    self.find_substitute()

                # display the chosen product
                for product in products:
                    if product_user == product['id']:
                        self.user_product = product['product']
                        print(product['product'].display())

                # find 5 max substituts from the chosen product
                substituts = Substitute.find(self.user_product)

                # check if there is no substituts for the chosen product
                if len(substituts) == 0:
                    print('Aucun substitut trouvé')
                    # if yes the user is redirected to the main menu
                    return self.play()
                else:
                    # if not, substituts are displayed
                    Substitute.listing(substituts)

                    # ask the user if he wants to save a substitut
                    # if not, the user is redirected to the main menu
                    # if yes he has to choose a substitut
                    if self.save_substitute():
                        substitut_user = input(
                            'Veuillez indiquer le'
                            'chiffre correspondant au produit\n:')
                        try:
                            substitut_user = int(substitut_user)
                            assert 0 < substitut_user <= len(substituts)
                        except ValueError:
                            print('erreur valeur')
                            return self.find_substitute()
                        except AssertionError:
                            print('erreur assertion')
                            return self.find_substitute()

                        # save the chosen substitut
                        # back to the main menu
                        for product in substituts:
                            if substitut_user == product['id']:
                                Substitute(product['product'].id,
                                           self.user_product.id).save()
                                print('Produit sauvegardé')
                                self.play()