def test_save(): """ Test save and load """ reset_options_instance() sys.argv += ['-o', 'tests/sgd.yaml', '--nested.message', 'save'] path_yaml = 'tests/saved.yaml' Options().save(path_yaml) with open(path_yaml, 'r') as yaml_file: options_yaml = yaml.safe_load(yaml_file) assert (OptionsDict(options_yaml) == OptionsDict({ "message": "sgd", "sgd": True, "nested": { "message": "save" } })) reset_options_instance() sys.argv += ['-o', 'tests/saved.yaml'] assert (Options().options == OptionsDict({ "path_opts": "tests/saved.yaml", "message": "sgd", "sgd": True, "nested": { "message": "save" } }))
def test_initialize_options_source_optionsdict(): reset_options_instance() source = OptionsDict({ 'dataset': 124, 'model': { 'criterion': 'I am a criterion', 'network': 'I am a network', }, }) Options(source, run_parser=False) assert Options().options == source assert Options().source == source.asdict()
def test_merge_dictionaries(): """ Merge two dictionnary """ dict1 = {'exp': {'dir': 'lol1', 'resume': None}} dict2 = {'exp': {'dir': 'lol2'}} dict1 = OptionsDict(dict1) dict2 = OptionsDict(dict2) merge_dictionaries(dict1, dict2) assert (dict1 == OptionsDict( {'exp': OptionsDict({ 'dir': 'lol2', 'resume': None })}))
def test_include(): """ Test include Expected behavior: .. code-block:: bash $ python tests/test_options.py -o tests/sgd.yaml { "path_opts": "test/sgd.yaml", "message": "sgd", "sgd": true, "nested": { "message": "lol" } } """ reset_options_instance() sys.argv += ['-o', 'tests/sgd.yaml'] assert Options().options == OptionsDict({ "path_opts": "tests/sgd.yaml", "message": "sgd", "sgd": True, "nested": { "message": "lol" } })
def test_load_yaml_opts(): """ Load options using static method (no singleton) """ reset_options_instance() opt = Options.load_yaml_opts('tests/default.yaml') assert (opt == OptionsDict({'message': 'default'})) assert Options._Options__instance is None
def test_overwrite(): """ Test overwrite Expected behavior: .. code-block:: bash $ python tests/test_options.py -o tests/sgd.yaml --nested.message lolilol` { "path_opts": "tests/sgd.yaml", "message": "sgd", "sgd": true, "nested": { "message": "lolilol" } } """ reset_options_instance() sys.argv += ['-o', 'tests/sgd.yaml', '--nested.message', 'lolilol'] assert (Options().options == OptionsDict({ "path_opts": "tests/sgd.yaml", "message": "sgd", "sgd": True, "nested": { "message": "lolilol" } }))
def test_as_dict(): """ Copy OptionsDict in a new dictionary of type :mod:`dict` """ dict1 = { 'exp': { 'dir': 'lol1', 'resume': None } } assert (dict1 == OptionsDict(dict1).asdict())
def test_initialize_options_source_dict_3(): reset_options_instance() source1 = { 'dataset': 123, 'model': { 'criterion': 'I am a criterion', 'network': 'I am a network', }, } Options(source1, run_parser=False) assert Options().options == OptionsDict(source1) assert Options().source == source1 source2 = { 'Micael': 'is the best', 'Remi': 'is awesome', } Options(source2, run_parser=False) assert Options().options == OptionsDict(source1) assert Options().source == source1
def test_include_list(): reset_options_instance() sys.argv += ['-o', 'tests/sgd_list_include.yaml'] assert Options().options == OptionsDict({ "path_opts": "tests/sgd_list_include.yaml", "message": "sgd", "sgd": True, "nested": { "message": "lol" }, "database": "db", })
def test_path_opts(): """ Test path given in argument Expected behavior: .. code-block:: bash $ python tests/test_options.py --path_opts test/default.yaml { "path_opts": "test/default.yaml", "message": "default" } """ reset_options_instance() sys.argv += ['-o', 'tests/default.yaml'] assert (Options().options == OptionsDict({'path_opts': 'tests/default.yaml', 'message': 'default'}))
def test_initialize_arguments_callback(): reset_options_instance() sys.argv += ['-o', 'tests/default.yaml'] source = { 'dataset': 'mydataset', 'model': 'mymodel', } def arguments_callback_a(instance, arguments, options_dict): arguments.dataset = arguments.dataset + 'a' arguments.model = arguments.model + 'a' return arguments Options(source, arguments_callback=arguments_callback_a) source_a = { 'path_opts': 'tests/default.yaml', 'dataset': 'mydataseta', 'model': 'mymodela', } assert Options().options == OptionsDict(source_a) assert Options().source == source
""" # Merge two dictionnary and transform to dict - cli: `python test/test_options5.py` - expected behavior: ``` OptionsDict({'exp': OptionsDict({'dir': 'lol2', 'resume': None})}) {'exp': {'dir': 'lol2', 'resume': None}} ``` """ from bootstrap.lib.options import merge_dictionaries from bootstrap.lib.options import OptionsDict if __name__ == '__main__': dict1 = { 'exp': { 'dir': 'lol1', 'resume': None } } dict2 = { 'exp': { 'dir': 'lol2' } } dict1 = OptionsDict(dict1) dict2 = OptionsDict(dict2) merge_dictionaries(dict1, dict2) print(dict1)