class ImportData: extension_file_csv = ".csv" def __init__(self): """ Contructor class Import Data class in charge to import sudokus data from different inputs such as csv or txt """ self.csv_handler = CSVHandler() self.cmd_handler = CMDHandler() def read_sudoku_data_from_file(self, sudoku_file): """ Read the sudoku data form files such as csv or txt Returns False if the data are invalid or the file was not able to read, return a matrix[][] wihich contains all the sudoku data and their sudoku size Keyword arguments: sudoku_file -- the path of the file which contains the sudoku data """ ext = self.get_extension_file(sudoku_file) matrix_sudoku = [] if ext == self.extension_file_csv: matrix_sudoku = self.csv_handler.get_rows_sudoku_data(sudoku_file, ",") return matrix_sudoku def read_sudoku_data_from_line(self, line): """ Read the sudoku data form a line entered form comand line Return a matrix[][] wihich contains the sudoku data and its sudoku size Keyword arguments: line -- the line string entered by the user """ matrix_sudoku = self.cmd_handler.get_rows_sudoku_data(line) return matrix_sudoku def get_extension_file(self, filename): """ Gess the extension of a given filename Keyword arguments: filename -- the filename to guess the extention """ double_extensions = ['tar.gz', 'tar.bz2'] root , ext = os.path.splitext(filename) if any([filename.endswith(x) for x in double_extensions]): root, first_ext = os.path.splitext(root) ext = first_ext + ext return ext
def test_read_sudoku_from_valid_csv(self): sudoku_invalid_size = -1 csv_import = CSVHandler() sudoku = csv_import.get_rows_sudoku_data('testinvalidcsv.csv', ',') expected = "316578492\n" + \ "529134768\n" + \ "487629531\n" + \ "263415987\n" + \ "974863125\n" + \ "851792643\n" + \ "138947256\n" self.assertEquals(sudoku[0][0], expected) self.assertEquals(sudoku[0][1], sudoku_invalid_size)
def test_read_sudoku_from_valid_csv(self): sudoku_size = 9 csv_import = CSVHandler() exp_val = "316578492\n" + \ "529134768\n" + \ "487629531\n" + \ "263415987\n" + \ "974863125\n" + \ "851792643\n" + \ "138947256\n" + \ "692351874\n" + \ "745286319\n" sudoku = csv_import.get_rows_sudoku_data('testvalidcsv.csv', ',') self.assertEquals(exp_val, sudoku[0][0]) self.assertEquals(sudoku_size, sudoku[0][1])
def test_read_sudoku_from_empty_csv(self): expected = [] csv_import = CSVHandler() sudoku = csv_import.get_rows_sudoku_data('empty.csv', ',') self.assertEquals(sudoku, expected)
def test_read_sudoku_from_non_exist_csv(self): csv_import = CSVHandler() sudoku = csv_import.get_rows_sudoku_data('non_exist.csv', ',') expected = [] self.assertEquals(sudoku, expected)