Example #1
0
File: 9A.py Project: etscrivner/dse
    def get_file_name(self):
        """Display a prompt to the user requesting data.

        Returns:
            str: The file name given by the user.
        """
        return io.prompt_valid_file_name('Enter test data file:')
Example #2
0
File: 8B.py Project: etscrivner/dse
    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)
Example #3
0
File: 4B.py Project: etscrivner/dse
def main():
    """Application entry point"""
    print "This program will read or write numbers to or from a given file."
    print 'Read - Read and display numbers in a file'
    print 'Write - Input numbers to write to a file'
    print 'Add - Go through file line-by-line and add new numbers'
    print 'Modify - Go through file line by line and modify numbers'
    mode = io.choose_from_list(
        'Choose a mode',
        ['Read', 'Write', 'Add', 'Modify']
    )
    mode = mode.lower()

    if mode in ['write']:
        file_path = io.prompt_valid_file_name('Please enter a file name: ')
    else:
        file_path = io.prompt_existant_file_name('Please enter a file name: ')

    if mode == 'read':
        read_file(file_path)
    elif mode == 'write':
        write_file(file_path)
    elif mode == 'add':
        add_file(file_path)
    elif mode == 'modify':
        modify_file(file_path)
    else:
        raise RuntimeError('Invalid option {}'.format(mode))
Example #4
0
File: 9B.py Project: etscrivner/dse
    def get_file_name(self):
        """Display a prompt to the user requesting data.

        Returns:
            str: The file name given by the user.
        """
        try:
            return io.prompt_valid_file_name('Enter test data file:')
        except RuntimeError as re:
            self.display_error('Invalid or missing test file provided.')
            raise re
Example #5
0
File: 4B.py Project: etscrivner/dse
def write_file(file_path):
    """Requests a series of numerical inputs and writes them to the file.

    Arguments:
        file_path(basestring): The path to the file
    """
    if os.path.isfile(file_path):
        should_overwrite = io.yes_no_prompt(
            'File {} exists. Overwrite?'.format(file_path))
        # If the file shouldn't be overwritten, abort
        if not should_overwrite:
            enter_different_file = io.yes_no_prompt(
                'Would you like to enter a different file name?')
            if enter_different_file:
                file_path = io.prompt_valid_file_name('New file name: ')
                write_file(file_path)
            sys.exit('Aborting. Not overwriting existing file.')

    if not os.path.exists(os.path.dirname(file_path)):
        print 'ERROR: Directory for file {} does not exist'.format(file_path)
        io.prompt_try_again_or_abort()
        file_path = io.prompt_valid_file_name('New file name: ')
        write_file(file_path)

    total_num_entries = io.get_and_confirm_input(
        'How many numbers will you enter: ')
    total_num_entries = int(total_num_entries)

    num_entries_given = 0

    with open(file_path, 'w') as wfile:
        while num_entries_given < total_num_entries:
            value = io.get_and_confirm_float(
                'Entry #{}: '.format(num_entries_given + 1))
            value = float(value)
            wfile.write('{}\n'.format(value))
            num_entries_given += 1
Example #6
0
File: 8B.py Project: etscrivner/dse
def prompt_for_output_file(original_file_name):
    """Prompt for the file to output data into

    Arguments:
        original_file_name(str): The original file name.

    Returns:
        str: The new file name if a new file is entered, otherwise the original
             file name.
    """
    print 'Original file: {}'.format(original_file_name)
    choice = io.binary_choice('Output file (s)ame/(n)ew file? ', 's', 'n')
    if choice == 'n':
        new_file_name = io.prompt_valid_file_name('New file name: ')
        return new_file_name
    return original_file_name
Example #7
0
File: 8B.py Project: etscrivner/dse
    def prompt_output_file(self, prompt, maximum_attempts=5):
        """Prompt the user to enter a valid output file.

        Arguments:
            prompt(basestring): The prompt to be displayed.
            maximum_attempts(int): Default 5, the maximum number of attempts
                a user has to enter a valid output file name.

        Returns:
            basestring: The output file path.
        """
        while maximum_attempts > 0:
            output_file = io.prompt_valid_file_name(prompt)
            if not os.path.exists(output_file):
                return output_file
            if io.yes_no_prompt(
                    'File {} already exists. Overwrite?'.format(output_file)):
                return output_file
            maximum_attempts -= 1
        raise RuntimeError('Maximum attempts exceeded.')