def test_init_obj_with_dict(): config = { 'config': { 'foo': { 'in_value': 2, 'out_value': 3, }, }, 'foo1': { '$Foo': '=/config/foo', 'get_params_x2()': 'get_params_x2', }, 'foo2': { '$Foo': '=/foo1/get_params_x2()', }, } s = Scake(config, class_mapping=globals()) s.run(debug=True) assert s['/foo1/get_params_x2()'] == { 'in_value': 2 * 2, 'out_value': 3 * 2, } assert s['/foo2'].get_params_x2() == { 'in_value': 2 * 2 * 2, 'out_value': 3 * 2 * 2, }
def test_flow(): config = { 'settings': { 'en': 'hello', 'fr': 'bonjour' }, 'french': { '$Person': { 'content': '=/settings/fr' }, 'out()': '__call__' }, 'eng': { '$Person': { 'content': '=/settings/en' }, 'out()': { 'say_what': { 'content': '=/french/out()' } } } } s = Scake(config, class_mapping=globals()) s.run() assert isinstance(s['/french'], Person) assert s['/french'].content == 'bonjour' assert s['/french'].out == 'bonjour' assert s['/french/out()'] == 'bonjour' assert isinstance(s['/eng'], Person) assert s['/eng'].content == 'hello' assert s['/eng'].say == 'bonjour' assert s['/eng/out()'] == 'bonjour'
def test_ref_obj_complex_1(): config = { 'const': { 'size': 10, }, 'transforms': { 'resize': { '$Foo': { 'x': '=/const/size', # <= bug risk!! } } }, 'gvnet': { 'main': '=/aug/compose', 'train': { 'cifar10': [ '=/transforms/resize', ] } }, 'aug': { 'compose': { '$Bar': { 'y': '=/gvnet/train/cifar10', } } }, 'dataset': { 'train': { '$Bar': { 'y': '=/gvnet/main', } } }, 'dataloader': { 'train': { '$Bar': { 'y': '=/dataset/train', } } }, 'trainer': { 'cifar': { '$Bar': { 'y': '=/dataloader/train', } } } } s = Scake(config, class_mapping=globals()) s.run(debug=True) trainer = s['/trainer/cifar'] dataloader = trainer.y dataset = dataloader.y compose = dataset.y transform_list = compose.y resize = transform_list[0] assert isinstance(resize, Foo)
def test_ref_list_simple(): config = { 'f0': [1, 2, 3], 'f1': '=/f0', } s = Scake(config, class_mapping=globals()) s.run(debug=True) assert s['/f1'] == [1, 2, 3]
def test_ref_obj_1_level(): config = { 'foo': { '$Foo': { 'x': 2, } }, 'fptr': '=/foo', } s = Scake(config, class_mapping=globals()) s.run(debug=True) assert isinstance(s['/fptr'], Foo)
def test_ref_list_obj(): config = { 'foo': { '$Foo': { 'x': 10, } }, 'f0': [1, '=/foo', 3], 'f1': '=/f0', } s = Scake(config, class_mapping=globals()) s.run(debug=True) assert isinstance(s['/f0'][1], Foo) assert isinstance(s['/f1'][1], Foo)
def test_similar_keys(): config = { 'person': { '$Person': { 'content': 'en', }, }, 'person_1': { '$Person': { 'content': 'fr', }, }, } s = Scake(config, class_mapping=globals()) s.run(debug=True) assert True
def test_ref_dict_multi_level(): config = { "config": { "myvar_one": { "x": 10, "y": 20, }, "myvar_two": { "x": 100, "y": 200, }, }, "myvar": { "one": { "$Foo": "=/config/myvar_one" }, "two": { "$Foo": "=/config/myvar_two" }, }, "mybar": { "$Bar": { "x": "=/myvar", "y": 1, }, }, } s = Scake(config, class_mapping=globals()) s.run(debug=True) mybar = s['/mybar'] assert isinstance(mybar, Bar) assert mybar.get_y() == 1 assert "one" in mybar.get_x() assert "two" in mybar.get_x() _logger.warning(mybar.get_x()) _logger.warning(s["/myvar/one"]) _logger.warning(s["/myvar/two"]) assert isinstance(mybar.get_x()["one"], Foo) assert isinstance(mybar.get_x()["two"], Foo) assert mybar.get_x()["one"]() == 30 assert mybar.get_x()["two"]() == 300
def test_ref_list_1(): config = { 'v0': { 'v1': { 'v2': [10, 20, 30], } }, 'foo': { '$Foo': { 'x': '=/v0/v1/v2', } }, 'out': '=/foo', } s = Scake(config, class_mapping=globals()) s.run(debug=True) assert s['/foo'].x == [10, 20, 30] assert s['/out'].x == [10, 20, 30]
def test_ref_obj_2_level(): config = { 'foo': { '$Foo': { 'x': 2, } }, 'f1': '=/foo', 'f2': { '$Bar': { 'foo': '=/f1', } } } s = Scake(config, class_mapping=globals()) s.run(debug=True) assert isinstance(s['/f1'], Foo) assert isinstance(s['/f2'], Bar) assert isinstance(s['/f2'].foo, Foo)
def test_ref_obj_attr(): config = { 'foo_obj': { '$Foo': { 'in_value': 2, 'out_value': 3, } }, 'fout_obj': { '$Foo': { 'in_value': '=/foo_obj.out_value', 'out_value': 0, }, 'out()': '__call__' } } s = Scake(config, class_mapping=globals()) s.run(debug=True) assert s['/fout_obj/out()'] == 3
def test_ref_list_3(): config = { 'v0': { 'v1': { 'v2': [10, 20, 30], } }, 'foo': { '$Foo': { 'x': '=/v0/v1/v2', } }, 'out': { '$Foo': { 'x': [1, '=/foo', 2], } } } s = Scake(config, class_mapping=globals()) s.run(debug=True) assert isinstance((s['/out'].x)[1], Foo)
def test_ref_obj_method(): config = { 'foo_obj': { '$Foo': { 'in_value': 2, 'out_value': 3, } }, 'bar_obj': { '$Bar': { 'foo_ptr': '=/foo_obj.get_value', }, 'out()': { '__call__': { 'x': 10, } } } } s = Scake(config, class_mapping=globals()) s.run(debug=True) assert s['/bar_obj/out()'] == 15
def test_attr_depend(): config = { 'group': { 'key1': 10, 'key2': 20 }, 'todo': { 'one': '=/group/key1', 'two': { 'value': '=/todo/one' } } } s = Scake(config) assert s['/group/key1'] == 10 assert s['/todo/one'] == s['/group/key1'] assert s['/todo/two/value'] == 10
def test_attr_only(): config = { 'group_1': { 'attr_1': 1, 'attr_2': 2 }, 'group_2': { 'group_2a': { 'attr_2a_1': 'x', 'attr_2a_2': 'y', }, 'group_2b': { 'attr_2b_1': 'z', 'attr_2b_2': [{ 'e0': { 'k0': 10 } }, { 'e1': [20, 30] }] } } } s = Scake(config) print(s) assert s['/group_1/attr_1'] == 1 assert s['/group_1/attr_2'] == 2 assert s['/group_1'] == config['group_1'] assert s['/group_2/group_2a/attr_2a_1'] == 'x' assert s['/group_2/group_2a/attr_2a_2'] == 'y' assert s['/group_2/group_2b/attr_2b_1'] == 'z' assert s['/group_2/group_2b/attr_2b_2/0/e0/k0'] == 10 assert s['/group_2/group_2b/attr_2b_2/1/e1/0'] == 20 assert s['/group_2/group_2b/attr_2b_2/1/e1/1'] == 30 assert s['/group_2/group_2a'] == config['group_2']['group_2a'] assert s['/group_2/group_2b'] == config['group_2']['group_2b'] assert s['/group_2'] == config['group_2'] assert len(s) == 17
def main(yaml_path): with open(yaml_path) as f: config = yaml.safe_load(f) s = Scake(config, class_mapping=globals()) s.run() pass
def test_scake_with_lib(): config = {'lib_obj': {'$datetime.date': [2020, 8, 20]}} s = Scake(config, class_mapping=globals()) s.run() assert True
def test_ref_list_4(): config = { 'transforms': { 't1': { '$Foo': { 'x': 5, } }, 't2': { '$Foo': { 'x': 10, } }, 't3': { '$Foo': { 'x': 17, } }, 'offical_list': [ '=/wrapper', '=/transforms/t3', ] }, 'v0': { 'v1': { 'v2': [ '=/transforms/t1', '=/transforms/t2', ], } }, 'seq_obj': { '$Foo': { 'x': '=/v0/v1/v2', } }, 'wrapper': { '$Wrapper': { 'x': '=/seq_obj', } }, 'compose': { '$Foo': { 'x': '=/transforms/offical_list', } }, 'aug': { 'train': { 'main': '=/compose', } }, 'dataset': { '$Foo': { 'x': '=/aug/train/main', } }, 'dataloader': { '$Foo': { 'x': '=/dataset', } } } s = Scake(config, class_mapping=globals()) s.run(debug=True) compose = s['/compose'] offical_list = compose.x wrapper = offical_list[0] assert isinstance(wrapper.x, Foo)