
    i                          " S  S5      r g)c                   J    \ rS rSrSr\S\S4S jrS rS	S jr	\
S 5       rSrg)
DictExporter   a  
Tree to dictionary exporter.

Every node is converted to a dictionary with all instance
attributes as key-value pairs.
Child nodes are exported to the children attribute.
A list of dictionaries.

Keyword Args:
    dictcls: class used as dictionary. :any:`dict` by default.
    attriter: attribute iterator for sorting and/or filtering.
    childiter: child iterator for sorting and/or filtering.
    maxlevel (int): Limit export to this number of levels.

>>> from pprint import pprint  # just for nice printing
>>> from anytree import AnyNode
>>> from anytree.exporter import DictExporter
>>> root = AnyNode(a="root")
>>> s0 = AnyNode(a="sub0", parent=root)
>>> s0a = AnyNode(a="sub0A", b="foo", parent=s0)
>>> s0b = AnyNode(a="sub0B", parent=s0)
>>> s1 = AnyNode(a="sub1", parent=root)

>>> exporter = DictExporter()
>>> pprint(exporter.export(root))
{'a': 'root',
 'children': [{'a': 'sub0',
               'children': [{'a': 'sub0A', 'b': 'foo'}, {'a': 'sub0B'}]},
              {'a': 'sub1'}]}

The attribute iterator `attriter` may be used for filtering too.
For example, just dump attributes named `a`:

>>> exporter = DictExporter(attriter=lambda attrs: [(k, v) for k, v in attrs if k == "a"])
>>> pprint(exporter.export(root))
{'a': 'root',
 'children': [{'a': 'sub0', 'children': [{'a': 'sub0A'}, {'a': 'sub0B'}]},
              {'a': 'sub1'}]}

The child iterator `childiter` can be used for sorting and filtering likewise:

>>> exporter = DictExporter(childiter=lambda children: [child for child in children if "0" in child.a])
>>> pprint(exporter.export(root))
{'a': 'root',
 'children': [{'a': 'sub0',
               'children': [{'a': 'sub0A', 'b': 'foo'}, {'a': 'sub0B'}]}]}
Nc                 4    Xl         X l        X0l        X@l        g N)dictclsattriter	childitermaxlevel)selfr   r   r	   r
   s        J/app/mltbenv/lib/python3.13/site-packages/anytree/exporter/dictexporter.py__init__DictExporter.__init__2   s     "     c                 z    U R                   =(       d    S nU R                  XR                  X R                  5      $ )zExport tree starting at `node`.c                     U $ r    )attr_valuess    r   <lambda>%DictExporter.export.<locals>.<lambda>:   s    r   )r   _DictExporter__exportr   r	   )r   noder   s      r   exportDictExporter.export8   s*    ==E%D}}T<<>>JJr   c                     U" U R                  U5      5      nU" U5      nU R                  nUb  XX:  a?  U" UR                  5       V	s/ s H  n	U R                  XX4US-   S9PM     n
n	U
(       a  XS'   U$ s  sn	f )Nr   )levelchildren)_iter_attr_valuesr
   r   r   )r   r   r   r   r	   r   r   datar
   childr   s              r   __exportDictExporter.__export=   s    t55d;<{#==u/ 't}}55E ehQRS5   #+Z s   A5c              #   l   #    U R                   R                  5        H  u  pUS;   a  M  X4v   M     g 7f)N)_NodeMixin__children_NodeMixin__parent)__dict__items)r   kvs      r   r   DictExporter._iter_attr_valuesJ   s3      MM'')DABB$J *s   24)r   r	   r   r
   )r   )__name__
__module____qualname____firstlineno____doc__dictlistr   r   r   staticmethodr   __static_attributes__r   r   r   r   r      s7    .`  $ddT !K
  r   r   N)r   r   r   r   <module>r3      s   O Or   