I'm trying match a dictionary with predefined raid configurations with a dictionary that contains the physical disk layout.
raid_config =
{ 'server1': [{'name' : 'data', 'disks' : 3, 'block' : 300}],
'server2': [{'name' : 'data', 'disks' : 8, 'block' : 2200}],
'server3': [{'name' : 'data', 'disks' : 2, 'block' : 300}, {'name' : 'data2', 'disks' : 2, 'block' : 300}, {'name' : 'data3', 'disks' : 2, 'block' : 1800}, {'name' : 'data', 'disks' : 8, 'block' : 300}],
'server4': [{'name' : 'data', 'disks' : 3, 'block' : 300}, {{'name' : 'data2', 'disks' : 8, 'block' : 880}]
}
disks =
{300: ['bay0', 'bay1', bay10'], 880: ['bay2', 'bay3', 'bay4', 'bay5', 'bay6', 'bay7', 'bay8', 'bay9']}
Previously I wrote a comprehension that summed the disk sizes for the raid configs and for the physical disks and matched those. That doesn't work anymore since that would result in multiple matches for different layouts.
print({sum(v['disks']*v['size'] for v in vs) : k for k, vs in raid_config.items()}.get(sum(k*v for k,v in {k : len(v) for k,v in disks.items()}.items())))
-->
server4
I'm struggling to come up with a minimalistic approach to get an exact match. What would be the best approach here?
I figured it out myself but I'm sure it can be done a little neater so input is still welcome.
configs = {}
for k, v in raid_config.items():
configs[k] = {}
for d in v:
if d.get('size') in configs[k]:
configs[k][d.get('size')] += d.get('disks')
else:
configs[k][d.get('size')] = d.get('disks')
^-- pretty sure that can be done in a single line
Which results in:
{'server1': {300: 3}, 'server2': {2200: 8}, 'server3': {300: 12, 1800: 2},'server4': {300: 3, 880: 8}}
Find the matching config:
for k,v in configs.items():
if disks == v:
print(k)
Result:
server4