def test_dict_product(self): input_m = OrderedDict([("a", [1, 2, 3]), ("b", [4, 5]), ("c", "d")]) result = list(u.dict_product(input_m)) expected = [{ 'a': 1, 'b': 4, 'c': 'd' }, { 'a': 1, 'b': 5, 'c': 'd' }, { 'a': 2, 'b': 4, 'c': 'd' }, { 'a': 2, 'b': 5, 'c': 'd' }, { 'a': 3, 'b': 4, 'c': 'd' }, { 'a': 3, 'b': 5, 'c': 'd' }] self.assertListEqual(result, expected)
def test_dict_product(): input_m = OrderedDict([("a", [1, 2, 3]), ("b", [4, 5]), ("c", "d")]) result = list(u.dict_product(input_m)) expected = [{ 'a': 1, 'b': 4, 'c': 'd' }, { 'a': 1, 'b': 5, 'c': 'd' }, { 'a': 2, 'b': 4, 'c': 'd' }, { 'a': 2, 'b': 5, 'c': 'd' }, { 'a': 3, 'b': 4, 'c': 'd' }, { 'a': 3, 'b': 5, 'c': 'd' }] assert result == expected
def expand_experiment_config(items: ExpConf) -> List[Experiment]: """Expand out the experiment config for job submission to Cloud. """ if isinstance(items, list): return list( itertools.chain.from_iterable( [expand_experiment_config(m) for m in items])) tupleized_items = u.tupleize_dict(items) return [u.expand_compound_dict(d) for d in u.dict_product(tupleized_items)]
def check_dictproduct(test_dict): self.assertListEqual( test_dict['after_dictproduct'], list(u.dict_product(test_dict['after_tupleization'])))
def test_compound_key_handling(): """tests the full assembly line transforming a configuration dictionary including compound keys into a list of dictionaries for passing to the script """ tests = [{ 'input': { '[a,b]': [['c', 'd'], ['e', 'f']] }, 'after_tupleization': { ('a', 'b'): [('c', 'd'), ('e', 'f')] }, 'after_dictproduct': [{ ('a', 'b'): ('c', 'd') }, { ('a', 'b'): ('e', 'f') }], 'after_expansion': [{ 'a': 'c', 'b': 'd' }, { 'a': 'e', 'b': 'f' }] }, { 'input': { '[a,b]': ['c', 'd'] }, 'after_tupleization': { ('a', 'b'): ('c', 'd') }, 'after_dictproduct': [{ ('a', 'b'): ('c', 'd') }], 'after_expansion': [{ 'a': 'c', 'b': 'd' }] }, { 'input': { 'hi': 'there', '[k1,k2]': [['v1a', 'v2a'], ['v1b', 'v2b']] }, 'after_tupleization': { 'hi': 'there', ('k1', 'k2'): [('v1a', 'v2a'), ('v1b', 'v2b')] }, 'after_dictproduct': [{ 'hi': 'there', ('k1', 'k2'): ('v1a', 'v2a') }, { 'hi': 'there', ('k1', 'k2'): ('v1b', 'v2b') }], 'after_expansion': [{ 'hi': 'there', 'k1': 'v1a', 'k2': 'v2a' }, { 'hi': 'there', 'k1': 'v1b', 'k2': 'v2b' }] }, { 'input': { 'hi': 'there', '[a,b]': ['c', 'd'] }, 'after_tupleization': { 'hi': 'there', ('a', 'b'): ('c', 'd') }, 'after_dictproduct': [{ 'hi': 'there', ('a', 'b'): ('c', 'd') }], 'after_expansion': [{ 'hi': 'there', 'a': 'c', 'b': 'd' }] }, { 'input': { '[a,b]': [0, 1] }, 'after_tupleization': { ('a', 'b'): (0, 1) }, 'after_dictproduct': [{ ('a', 'b'): (0, 1) }], 'after_expansion': [{ 'a': 0, 'b': 1 }] }, { 'input': { '[a,b]': [[0, 1]] }, 'after_tupleization': { ('a', 'b'): [(0, 1)] }, 'after_dictproduct': [{ ('a', 'b'): (0, 1) }], 'after_expansion': [{ 'a': 0, 'b': 1 }] }, { 'input': { 'hi': 'blueshift', '[a,b]': [[0, 1]] }, 'after_tupleization': { 'hi': 'blueshift', ('a', 'b'): [(0, 1)] }, 'after_dictproduct': [{ 'hi': 'blueshift', ('a', 'b'): (0, 1) }], 'after_expansion': [{ 'hi': 'blueshift', 'a': 0, 'b': 1 }] }] for test in tests: assert test['after_tupleization'] == c.tupleize_dict(test['input']) assert test['after_expansion'] == list( c.expand_compound_dict(test['after_dictproduct'])) assert test['after_expansion'] == c.expand_experiment_config( test['input']) assert test['after_dictproduct'] == list( u.dict_product(test['after_tupleization']))