Ejemplo n.º 1
0
Archivo: 8A.py Proyecto: etscrivner/dse
    def execute(self):
        print '8A: Load CSV file and sort by selected column'
        print

        file_name = io.prompt_existant_file_name('CSV file to sort: ')
        data = io.read_csv_file(file_name)

        if not data:
            print 'ERROR: File contains no data.'
            sys.exit(1)

        column_names = data[0].keys()
        sort_column = io.choose_from_list('Column to sort on', column_names)

        try:
            for each in data:
                each[sort_column] = float(each[sort_column])
        except ValueError:
            print 'ERROR: Column {} contains non-integer value.'.format(
                sort_column)
            sys.exit(1)

        sorted_data = sorted(data, key=lambda item: item[sort_column])

        table = display_table.DisplayTable(column_names)
        for each in sorted_data:
            table.add_row(each.values())
        table.display()
Ejemplo n.º 2
0
Archivo: 4B.py Proyecto: 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))
Ejemplo n.º 3
0
Archivo: 8B.py Proyecto: etscrivner/dse
    def execute(self, maximum_list_length):
        """Prompt user for a file and display the values from that file.

        Arguments:
            maximum_list_length(int): The maximum allowed list length (Unused)
        """
        input_file = io.prompt_existant_file_name(
            'Please enter the file to read: ')
        values = io.read_lists_from_file(input_file)
        for each in values:
            print each
Ejemplo n.º 4
0
Archivo: 8B.py Proyecto: etscrivner/dse
 def execute(self, _):
     """Sort file contents"""
     input_file = io.prompt_existant_file_name('File to sort:')
     data = io.read_lists_from_file(input_file)
     if not data:
         print 'ERROR: File {} is empty.'.format(input_file)
         sys.exit()
     output_file = self.prompt_output_file('File to output to:')
     sort_column = io.choose_from_list('Sort column', range(len(data[0])))
     for idx, value in enumerate(data):
         try:
             data[idx][sort_column] = float(data[idx][sort_column])
         except ValueError:
             print 'ERROR: Column {} is non-numeric'.format(sort_column)
             sys.exit()
     sorted_data = sort.merge_sort(data, key=lambda x: x[sort_column])
     io.write_lists_to_file(output_file, sorted_data)
     print 'Sorted contents written to {}'.format(output_file)
Ejemplo n.º 5
0
Archivo: 8B.py Proyecto: etscrivner/dse
    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)
Ejemplo n.º 6
0
Archivo: 8B.py Proyecto: etscrivner/dse
    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)