1def extract_list(config, section): 2 from collections import Iterable 3 l = [] 4 if 'modules' in config: 5 for module in config['modules']: 6 for k, v in module.items(): 7 for x in v: 8 if section in x: 9 if isinstance(x[section], Iterable) and not isinstance(x[section], str): 10 for y in x[section]: 11 l.append(y) 12 else: 13 l.append(x[section]) 14 return l 15 16 17def to_d(l): 18 """ 19 Converts list of dicts to dict. 20 """ 21 _d = {} 22 for x in l: 23 for k, v in x.items(): 24 _d[k] = v 25 return _d 26 27 28def test_to_d(): 29 l = [{'a': 'b'}, {'c': 'd'}] 30 d = {'a': 'b', 'c': 'd'} 31 assert to_d(l) == d 32 33 34def to_l(x): 35 """ 36 Converts list of dicts to dict. 37 """ 38 if isinstance(x, str): 39 return [x] 40 else: 41 return x 42 43 44def test_to_l(): 45 assert to_l('foo') == ['foo'] 46 assert to_l(['foo', 'bar']) == ['foo', 'bar'] 47