class StorerSetting: def __init__(self, config_file, settings): self.configuration = Configuration(config_file, settings) self.readfile = FileReader(config_file) """'save_settings_config_file' method saves the settings values in the config file""" def read_setting_dic(self, configuration_obj): num = len(configuration_obj.settings) cadena = '' ln = "\n" for i in range(num): cadena += self.configuration.config[i]+":"+self.configuration.settings[self.configuration.config[i]]+ln return cadena def save_txt_config_file(self): if self.readfile.file_exist() == True or self.readfile.file_exist() == False: f = open(self.readfile.config_file,"w") #f.write(self.configuration.config[0]+":"+self.configuration.settings[self.configuration.config[0]]+ln+self.configuration.config[1]+":"+self.configuration.settings[self.configuration.config[1]]+ln+self.configuration.config[2]+":"+self.configuration.settings[self.configuration.config[2]]+ln) f.write(self.read_setting_dic(self.configuration)) f.close() return "File Created"
def setUp(self): self.read_file = FileReader("config.ini") self.read_file1 = FileReader("config_empty.ini") self.read_file2 = FileReader("config_no_rigth_format.ini") self.read_file01 = FileReader("anything.ini") self.read_file02 = FileReader("anything.xls") self.out_put_file_not_exist = '' self.file_empty = " " self.right_format = "['Output_file:Txt']['Default_alghoritm:Backtracking']['Dificult_level:Easier']"
class SudokuGenerator: ''' Generator class will create new sudoku games, based on already stored files ''' level = '' files = [] path_easy = 'Sudoku_Levels/Easy/' path_medium = 'Sudoku_Levels/Medium/' path_hard = 'Sudoku_Levels/Hard/' def __init__(self,config_file): ''' Constructor: define which is the difficult level set by the user at config.ini file ''' self.readfile = FileReader(config_file) self.level = self.readfile.read_dificult_level() def retrieve_file_names(self): """ retrieve_file_names method retrieves all file names stored based on difficult level """ if self.level.strip()== "Easier": self.files = [f for f in os.listdir(self.path_easy) if f.endswith('.txt')] return self.files elif self.level.strip()== "Medium": self.files = [f for f in os.listdir(self.path_medium) if f.endswith('.txt')] return self.files elif self.level.strip()== "Difficult": self.files = [f for f in os.listdir(self.path_hard) if f.endswith('.txt')] return self.files else: return "There is not Sudoku game for this difficult level" def select_file(self): file_list = self.retrieve_file_names() num_files = len(file_list) num_f = randint(0, num_files-1) return file_list[num_f] def read_file(self): value = [] files = self.select_file() if self.level.strip() == "Easier": f=open(self.path_easy+files,'r') value = f.readlines() f.close() if len(value) == 9: return value
class TestFileReaderClassAndMethods(unittest.TestCase): def setUp(self): self.read_file = FileReader("config.ini") self.read_file1 = FileReader("config_empty.ini") self.read_file2 = FileReader("config_no_rigth_format.ini") self.read_file01 = FileReader("anything.ini") self.read_file02 = FileReader("anything.xls") self.out_put_file_not_exist = '' self.file_empty = " " self.right_format = "['Output_file:Txt']['Default_alghoritm:Backtracking']['Dificult_level:Easier']" def test_verify_config_file_exist(self): exist_file = self.read_file.file_exist() self.assertEqual(True, exist_file) def test_verify_config_file_is_empty(self): empty_file = self.read_file.file_is_empty() self.assertEqual(False, empty_file) def test_when_config_file_is_empty_return_True(self): empty_file = self.read_file1.file_is_empty() self.assertEqual(True, empty_file) def test_verify_config_file_has_right_extension_ini(self): format_file_name = self.read_file.right_configuration_extension() self.assertTrue(format_file_name) def test_verify_config_file_has_the_new_custom_values(self): custom_setting_file = self.read_file.file_exist() self.assertEqual(True, custom_setting_file) def test_just_right_extension_file_is_read(self): self.assertTrue(self.read_file.right_configuration_extension()) def test_that_a_no_defined_extension_file_is_not_taken_into_account(self): self.assertFalse(self.read_file02.right_configuration_extension()) def test_that_config_file_has_the_right_info_according_to_defiened_format(self): read0=self.read_file.right_contain_of_the_ini_file() self.assertEqual(read0,self.right_format) def test_an_error_is_raised_when_there_is_not_file(self): read01=self.read_file01.file_exist() self.failureException(read01)
class TestFileReaderClassAndMethods(unittest.TestCase): def setUp(self): self.read_file = FileReader("config.ini") self.read_file1 = FileReader("config_empty.ini") self.read_file2 = FileReader("config_no_rigth_format.ini") self.read_file01 = FileReader("anything.ini") self.read_file02 = FileReader("anything.xls") self.out_put_file_not_exist = '' self.file_empty = " " self.right_format = "['Output_file:Txt']['Default_alghoritm:Backtracking']['Dificult_level:Easier']" def test_verify_config_file_exist(self): exist_file = self.read_file.file_exist() self.assertEqual(True, exist_file) def test_verify_config_file_is_empty(self): empty_file = self.read_file.file_is_empty() self.assertEqual(False, empty_file) def test_when_config_file_is_empty_return_True(self): empty_file = self.read_file1.file_is_empty() self.assertEqual(True, empty_file) def test_verify_config_file_has_right_extension_ini(self): format_file_name = self.read_file.right_configuration_extension() self.assertTrue(format_file_name) def test_verify_config_file_has_the_new_custom_values(self): custom_setting_file = self.read_file.file_exist() self.assertEqual(True, custom_setting_file) def test_just_right_extension_file_is_read(self): self.assertTrue(self.read_file.right_configuration_extension()) def test_that_a_no_defined_extension_file_is_not_taken_into_account(self): self.assertFalse(self.read_file02.right_configuration_extension()) def test_that_config_file_has_the_right_info_according_to_defiened_format( self): read0 = self.read_file.right_contain_of_the_ini_file() self.assertEqual(read0, self.right_format) def test_an_error_is_raised_when_there_is_not_file(self): read01 = self.read_file01.file_exist() self.failureException(read01)
def __init__(self, config_file, settings): self.configuration = Configuration(config_file, settings) self.readfile = FileReader(config_file)
def __init__(self,config_file): ''' Constructor: define which is the difficult level set by the user at config.ini file ''' self.readfile = FileReader(config_file) self.level = self.readfile.read_dificult_level()