Andrius Andrius - 2 months ago 19
Python Question

Python - generating parent/child dict structure

I have method:

@staticmethod
def get_blocks():
"""Public method that can be extended to add new blocks.

First item is the most parent. Last item is the most child.
Returns:
blocks (list)
"""
return ['header', 'body', 'footer']


As docstring describes, this method can be extended , to return any kind of blocks in particular order.

So I want to make a mapping that would indicate which block is parent/child to each other (only caring about "nearest" parent/child).

def _get_blocks_mapping(blocks):
mp = {'parent': {}, 'child': {}}
if not blocks:
return mp
mp['parent'][blocks[0]] = None
mp['child'][blocks[-1]] = None
blocks_len = len(blocks)
if blocks_len > 1:
mp['parent'][blocks[-1]] = blocks[-2]
for i in range(1, len(blocks)-1):
mp['parent'][blocks[i]] = blocks[i-1]
mp['child'][blocks[i]] = blocks[i+1]
return mp


So result if we have three blocks like in
get_blocks
method is this:

{
'parent': {
'header': None,
'body': 'header',
'footer': 'body',
},
'child': {
'header': 'body',
'body': 'footer',
'footer': None
}
}


Well it works, but it is kind of hacky to me. So maybe someone could suggest a better way to create such mapping? (or maybe there is some used way of creating parent/child mapping? Using different structure than I intend to use?)

Answer

You want to loop over the list in pairs, giving you the natural parent-child relationships:

mp = {'parent': {}, 'child': {}}
if blocks:
    mp['parent'][blocks[0]] = mp['child'][blocks[-1]] = None
    for parent, child in zip(blocks, blocks[1:]):
        mp['parent'][child] = parent
        mp['child'][parent] = child

zip() here pairs up each block with the next one in the list.

Demo:

>>> blocks = ['header', 'body', 'footer']
>>> mp = {'parent': {}, 'child': {}}
>>> if blocks:
...     mp['parent'][blocks[0]] = mp['child'][blocks[-1]] = None
...     for parent, child in zip(blocks, blocks[1:]):
...         mp['parent'][child] = parent
...         mp['child'][parent] = child
...
>>> from pprint import pprint
>>> pprint(mp)
{'child': {'body': 'footer', 'footer': None, 'header': 'body'},
 'parent': {'body': 'header', 'footer': 'body', 'header': None}}
Comments