Пример #1
0
    def execute(self, maximum_list_length):
        """Prompt user for a file and have them enter values to write into it.

        Arguments:
            maximum_list_length(int): The maximum allowed list length.
        """
        output_file = io.prompt_valid_file_name(
            'Please enter the file to write: ')

        if os.path.isfile(output_file):
            self.prompt_overwrite_or_change(output_file, maximum_list_length)

        if not os.path.exists(os.path.dirname(output_file)):
            self.directory_does_not_exist(output_file, maximum_list_length)

        total_num_entries = int(io.get_and_confirm_input(
            'How many numbers will you enter: '))
        num_entries_given = 0
        entries = []

        while num_entries_given < total_num_entries:
            value = io.get_and_confirm_list(
                'Entry {}: '.format(num_entries_given + 1),
                max_list_length=maximum_list_length)
            entries.append(value)
            num_entries_given += 1

        io.write_lists_to_file(output_file, entries)
        print 'Results written to {}'.format(output_file)
Пример #2
0
    def execute(self, maximum_list_length):
        """Prompt user for input file and output file and go through input
        file line-by-line adding data.

        Arguments:
            maximum_list_length(int): The maximum allowed list length.
        """
        input_file = io.prompt_existant_file_name(
            'Please enter file to read from: ')
        data = io.read_lists_from_file(input_file)
        output_file = prompt_for_output_file(input_file)

        updated_results = []
        for index, each in enumerate(data):
            if updated_results:
                print_results_so_far(updated_results)

            print 'Next Item:'
            print each

            choice = io.choose_from_list(
                'What would you like to do',
                ['Keep', 'Add Item Before', 'Add Item After', 'Keep Rest'])

            if choice == 'Keep':
                updated_results.append(each)
            elif choice == 'Add Item Before':
                new_list = io.get_and_confirm_list(
                    'New item: ', max_list_length=maximum_list_length)
                updated_results.append(new_list)
                updated_results.append(each)
            elif choice == 'Add Item After':
                new_list = io.get_and_confirm_list(
                    'New item: ', max_list_length=maximum_list_length)
                updated_results.append(each)
                updated_results.append(new_list)
            else:
                updated_results += data[index:]

        io.write_lists_to_file(output_file, updated_results)
        print 'Results written to {}'.format(output_file)
Пример #3
0
    def execute(self, maximum_list_length):
        """Prompt user for input and output files then go through input file
        line-by-line, updated values, and write results to output file.

        Arguments:
            maximum_list_length(int): The maximum allowed list length
        """
        input_file = io.prompt_existant_file_name(
            'Please enter file to modify: ')
        output_file = prompt_for_output_file(input_file)
        data = io.read_lists_from_file(input_file)

        updated_results = []
        for index, each in enumerate(data):
            if updated_results:
                print_results_so_far(updated_results)

            print 'Next Item:'
            print each

            choice = io.choose_from_list(
                'What would you like to do',
                ['Keep', 'Change', 'Delete', 'Keep Rest'])
            if choice == 'Keep':
                updated_results.append(each)
            elif choice == 'Change':
                new_item = io.get_and_confirm_list(
                    'New Item: ', max_list_length=maximum_list_length)
                updated_results.append(new_item)
            elif choice == 'Delete':
                pass
            else:
                updated_results += data[index:]

        io.write_lists_to_file(output_file, updated_results)
        print 'Results written to {}'.format(output_file)