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
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)
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)
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)