def test_auto_name_if_needed(): params = Parameters() file_name = 'out.txt' params.update_parameter('OUTPUT_FILE', file_name) assert dea_utils.auto_name_if_needed(params, 'txt') == file_name params.update_parameter('OUTPUT_FILE', '') data_file_name = 'dataFileForAutoName.xls' params.update_parameter('DATA_FILE', data_file_name) assert dea_utils.auto_name_if_needed( params, 'csv') == 'dataFileForAutoName_result.csv' params.update_parameter('OUTPUT_FILE', 'auto') assert dea_utils.auto_name_if_needed( params, 'xls') == 'dataFileForAutoName_result.xls' with pytest.raises(ValueError) as excinfo: dea_utils.auto_name_if_needed(params, 'haha') assert str(excinfo.value) == 'haha is not supported output format'
def test_main_correct_params(): filename = 'tests/params_new_format.txt' params = parse_parameters_from_file(filename) auto_name = auto_name_if_needed(params, 'xlsx') main(filename) assert os.path.exists(auto_name) is True os.remove(auto_name)
def test_main_csv(): filename = 'tests/params_to_test_main_csv.txt' params = parse_parameters_from_file(filename) auto_name = auto_name_if_needed(params, 'xlsx') main(filename, sheet_name_usr='******') assert os.path.exists(auto_name) is True os.remove(auto_name)
def test_auto_name_if_needed(): params = Parameters() file_name = 'out.txt' params.update_parameter('OUTPUT_FILE', file_name) assert dea_utils.auto_name_if_needed(params, 'txt') == file_name params.update_parameter('OUTPUT_FILE', '') data_file_name = 'dataFileForAutoName.xls' params.update_parameter('DATA_FILE', data_file_name) assert dea_utils.auto_name_if_needed( params, 'csv') == 'dataFileForAutoName_result.csv' params.update_parameter('OUTPUT_FILE', 'auto') assert dea_utils.auto_name_if_needed( params, 'xls') == 'dataFileForAutoName_result.xls' with pytest.raises(ValueError) as excinfo: dea_utils.auto_name_if_needed(params, 'haha') assert str(excinfo.value) == 'haha is not supported output format'
def post_process_solutions(self, solutions, params, param_strs, all_ranks, run_date, total_seconds): ''' See base class. ''' output_file = auto_name_if_needed(self.params, self.output_format, self.output_dir) if output_file: categorical = self.params.get_parameter_value( 'CATEGORICAL_CATEGORY') if not categorical.strip(): categorical = None if output_file.endswith('.xls'): work_book = xlwt.Workbook() elif output_file.endswith('.xlsx'): work_book = XlsxWorkbook() elif output_file.endswith('.csv'): work_book = TxtWriter(os.path.splitext(output_file)[0]) else: raise ValueError('File {0} has unsupported output format'.format (output_file)) writer = XLSWriter(self.params, work_book, run_date, total_seconds, ranks=all_ranks, categorical=categorical) try: for count, sol in enumerate(solutions): writer.write_data(sol, param_strs[count]) work_book.save(output_file) except ValueError: work_book = TxtWriter(os.path.splitext(output_file)[0]) writer = XLSWriter(self.params, work_book, run_date, total_seconds, ranks=all_ranks, categorical=categorical) for count, sol in enumerate(solutions): writer.write_data(sol, param_strs[count]) work_book.save(output_file)
def test_main_output_dir(): filename = 'tests/params_new_format.txt' params = parse_parameters_from_file(filename) auto_name = auto_name_if_needed(params, 'xls') output_dir = 'tests' main(filename, output_format='xls', output_dir=output_dir) output_name = os.path.join(output_dir, auto_name) assert os.path.exists(output_name) is True os.remove(output_name)
def post_process_solutions(self, solutions, params, param_strs, all_ranks, run_date, total_seconds): ''' See base class. ''' output_file = auto_name_if_needed(self.params, self.output_format, self.output_dir) if output_file: categorical = self.params.get_parameter_value( 'CATEGORICAL_CATEGORY') if not categorical.strip(): categorical = None if output_file.endswith('.xls'): work_book = xlwt.Workbook() elif output_file.endswith('.xlsx'): work_book = XlsxWorkbook() elif output_file.endswith('.csv'): work_book = TxtWriter(os.path.splitext(output_file)[0]) else: raise ValueError( 'File {0} has unsupported output format'.format( output_file)) writer = XLSWriter(self.params, work_book, run_date, total_seconds, ranks=all_ranks, categorical=categorical) try: for count, sol in enumerate(solutions): writer.write_data(sol, param_strs[count]) work_book.save(output_file) except ValueError: work_book = TxtWriter(os.path.splitext(output_file)[0]) writer = XLSWriter(self.params, work_book, run_date, total_seconds, ranks=all_ranks, categorical=categorical) for count, sol in enumerate(solutions): writer.write_data(sol, param_strs[count]) work_book.save(output_file)