map_tree

treepathmap.map_tree(potential_tree: Union[Sequence, Mapping], parent_path_map_item: Optional[treepathmap.selectables.TreeNodeItem] = None, item_is_a_leaf: Optional[Callable[[Any], bool]] = None, modify_default_path_map_item: Optional[Callable[[treepathmap.selectables.TreeNodeItem], treepathmap.selectables.TreeNodeItem]] = None)treepathmap.maps.PathMap

Maps a nested collection to a PathMap.

Parameters
  • potential_tree – The potential nested tree to be mapped.

  • parent_path_map_item (Optional[TreeNodeItem]) – The path map item of the parent container, the potential tree is located in. The default option None means the potential tree is the root container.

  • item_is_a_leaf (Optional[DetectATreeLeaf]) – The custom Callable item_is_a_leaf defines if an item is a leaf or a node. By default treenodedefinition.this_item_is_a_leaf is used.

  • modify_default_path_map_item (Optional[Callable[[TreeNodeItem], TreeNodeItem]]) – Defines a Callable, which enables an additional declaration of tree paths and meta potential_tree of the default real path TreeNodeItem directly after its creation.

Raises

TypeError – if tree_item_to_map is not a Sequence or Mapping.

Returns

PathMap

Examples

>>> from treepathmap.maps import map_tree
>>> sample_tree = {"1st": [["a set", "of"], ["items"]]}
>>> sample_map = map_tree(sample_tree)
>>> print(sample_map)
         meta_attributes
->1st               ////
->1st->0            ////
->1st->1            ////
>>> def add_path_and_meta_attributes(a_path_map_item):
...     a_path_map_item.add_meta_attributes({"some": "metadata"})
...     # don't give this additional path example to much credit.
...     a_nonsense_path = 0
...     the_value_of_this_item = a_path_map_item.prime_value
...     for char in str(the_value_of_this_item):
...         a_nonsense_path += ord(char)
...     # the additional path is set to location 1, first place after
...     # the real path generated by the default mapping method.
...     a_path_map_item.set_tree_path(1, a_nonsense_path)
...     return a_path_map_item
...
>>> extended_path_map_items = map_tree_items(
...     sample_tree,
...     modify_default_path_map_item=add_path_and_meta_attributes
... )
...
>>> extended_path_map_items.print_full_items()
TreePath:
    path-0: ->1st
    path-1: ->2158
    metadata: {'some': 'metadata'}
    parent container type: dict
TreePath:
    path-0: ->1st->0
    path-1: ->1090
    metadata: {'some': 'metadata'}
    parent container type: list
TreePath:
    path-0: ->1st->1
    path-1: ->808
    metadata: {'some': 'metadata'}
    parent container type: list