U
    f                     @   s^  d Z ddlmZ ddlZddlZddlmZmZm	Z	m
Z
mZ ddlmZ ddlmZ ddlmZ dd	lmZ zddlZejZW n$ ek
r   G d
d deZY nX zddlmZ W n$ ek
r   dejkrЂ dZY nX dddddddddddddgZdZeefZdZdZdZdZ d Z!d!Z"e
d"Z#e
d#Z$eed$ ee#d$f e$f Z%ze%e	e$f Z&W n0 e'k
r   eed% ee	d%f e$f Z&Y nX d&d' Z(d(d) Z)d*d+ Z*d,d- Z+d.d Z,d/d Z-G d0d1 d1eZ.e. Z/dHd3dZ0d4d5 Z1d6d Z2d7d Z3d8d Z4dId:d;Z5d<d= Z6dJd>d?Z7dKd@dZ8dLdAdZ9dBd Z:dCd Z;dDd Z<e Z=dMdEdZ>dNdFdGZ?dS )Oz2Functions for working with nested data structures.    )abcN)MappingSequenceTextTypeVarUnion   )	_is_attrs)_is_namedtuple)_sequence_like)_sortedc                   @   s   e Zd ZdZdS )ObjectProxyz$Stub-class for `wrapt.ObjectProxy``.N)__name__
__module____qualname____doc__ r   r   4/tmp/pip-target-lpfmz8o1/lib/python/tree/__init__.pyr   !   s   r   )_treeZsphinx	is_nestedassert_same_structureunflatten_asflattenflatten_up_toflatten_with_pathflatten_with_path_up_tomap_structuremap_structure_up_tomap_structure_with_pathmap_structure_with_path_up_totraverseMAP_TO_NONEz0.1.8zThe shallow_tree's keys are not a subset of the input_tree's keys. The shallow_tree has the following keys that are not in the input_tree: {}.zThe two structures don't have the same sequence type. Input structure has type {input_type}, while shallow structure has type {shallow_type}.zThe two structures don't have the same sequence length. Input structure has length {input_length}, while shallow structure has length {shallow_length}.zThe input_tree has fewer elements than the shallow_tree. Input structure has length {input_size}, while shallow structure has length {shallow_size}.zVIf shallow structure is a sequence, input must also be a sequence. Input has type: {}.zpIf shallow structure is a sequence, input must also be a sequence. Input at path: {path} has type: {input_type}.KVzStructureKV[K, V]zStructure[V]c                    s    fdd j jD S )zReturns a list of (name, value) pairs from an attrs instance.

  The list will be sorted by name.

  Args:
    obj: an object.

  Returns:
    A list of (attr_name, attr_value) pairs.
  c                    s   g | ]}|j t |j fqS r   )namegetattr).0attrobjr   r   
<listcomp>   s   z$_get_attrs_items.<locals>.<listcomp>)	__class____attrs_attrs__r(   r   r(   r   _get_attrs_itemst   s    
r-   c                 c   s   t | D ]\}}|V  qd S N)_yield_sorted_items)iterable_vr   r   r   _yield_value   s    r3   c                 c   s   t | tjr*t| D ]}|| | fV  qnXt| rHt| D ]
}|V  q:n:t| rn| jD ]}|t| |fV  qVnt	| D ]
}|V  qvdS )a  Yield (key, value) pairs for `iterable` in a deterministic order.

  For Sequences, the key will be an int, the array index of a value.
  For Mappings, the key will be the dictionary key.
  For objects (e.g. namedtuples), the key will be the attribute name.

  In all cases, the keys will be iterated in sorted order.

  Args:
    iterable: an iterable.

  Yields:
    The iterable's (key, value) pairs, in order of sorted keys.
  N)

isinstancecollections_abcr   r   r	   r-   r
   _fieldsr%   	enumerate)r0   keyitemfieldr   r   r   r/      s    

r/   c                 C   s$   t | rtt| jdS t| S d S )Nr,   )r	   lenr%   r+   	structurer   r   r   _num_elements   s    r>   c                 C   s
   t | S )a$  Checks if a given structure is nested.

  >>> tree.is_nested(42)
  False
  >>> tree.is_nested({"foo": 42})
  True

  Args:
    structure: A structure to check.

  Returns:
    `True` if a given structure is nested, i.e. is a sequence, a mapping,
    or a namedtuple, and `False` otherwise.
  )r   Zis_sequencer<   r   r   r   r      s    c                 C   s
   t | S )a  Flattens a possibly nested structure into a list.

  >>> tree.flatten([[1, 2, 3], [4, [5], [[6]]]])
  [1, 2, 3, 4, 5, 6]

  If `structure` is not nested, the result is a single-element list.

  >>> tree.flatten(None)
  [None]
  >>> tree.flatten(1)
  [1]

  In the case of dict instances, the sequence consists of the values,
  sorted by key to ensure deterministic behavior. This is true also for
  :class:`~collections.OrderedDict` instances: their sequence order is
  ignored, the sorting order of keys is used instead. The same convention
  is followed in :func:`~tree.unflatten`. This correctly unflattens dicts
  and ``OrderedDict``\ s after they have been flattened, and also allows
  flattening an ``OrderedDict`` and then unflattening it back using a
  corresponding plain dict, or vice-versa.

  Dictionaries with non-sortable keys cannot be flattened.

  >>> tree.flatten({100: 'world!', 6: 'Hello'})
  ['Hello', 'world!']

  Args:
    structure: An arbitrarily nested structure.

  Returns:
    A list, the flattened version of the input `structure`.

  Raises:
    TypeError: If `structure` is or contains a mapping with non-sortable keys.
  )r   r   r<   r   r   r   r      s    $c                   @   s   e Zd Zdd Zdd ZdS )
_DotStringc                 C   s   dS N.r   selfr   r   r   __str__   s    z_DotString.__str__c                 C   s   dS r@   r   rB   r   r   r   __repr__   s    z_DotString.__repr__N)r   r   r   rD   rE   r   r   r   r   r?      s   r?   Tc              
   C   sx   zt | || W n` ttfk
rr } z>ttdd | }ttdd |}t|d|||f W 5 d}~X Y nX dS )a  Asserts that two structures are nested in the same way.

  >>> tree.assert_same_structure([(0, 1)], [(2, 3)])

  Note that namedtuples with identical name and fields are always considered
  to have the same shallow structure (even with `check_types=True`).

  >>> Foo = collections.namedtuple('Foo', ['a', 'b'])
  >>> AlsoFoo = collections.namedtuple('Foo', ['a', 'b'])
  >>> tree.assert_same_structure(Foo(0, 1), AlsoFoo(2, 3))

  Named tuples with different names are considered to have different shallow
  structures:

  >>> Bar = collections.namedtuple('Bar', ['a', 'b'])
  >>> tree.assert_same_structure(Foo(0, 1), Bar(2, 3))
  Traceback (most recent call last):
    ...
  TypeError: The two structures don't have the same nested structure.
  ...

  Args:
    a: an arbitrarily nested structure.
    b: an arbitrarily nested structure.
    check_types: if `True` (default) types of sequences are checked as
      well, including the keys of dictionaries. If set to `False`, for example
      a list and a tuple of objects will look the same if they have the same
      size. Note that namedtuples with identical name and fields are always
      considered to have the same shallow structure.

  Raises:
    ValueError: If the two structures do not have the same number of elements or
      if the two structures are not nested in the same way.
    TypeError: If the two structures differ in the type of sequence in any of
      their substructures. Only possible if `check_types` is `True`.
  c                 S   s   t S r.   _DOTr1   r   r   r   <lambda>      z'assert_same_structure.<locals>.<lambda>c                 S   s   t S r.   rF   rH   r   r   r   rI     rJ   z9%s
Entire first structure:
%s
Entire second structure:
%sN)r   r   
ValueError	TypeErrorstrr   type)abcheck_typeseZstr1Zstr2r   r   r   r      s    %c                 C   s^   g }t | D ]H}t|r>t|||\}}|t|| |}q|||  |d7 }q||fS )a  Helper function for ``unflatten_as``.

  Args:
    structure: Substructure (list / tuple / dict) to mimic.
    flat: Flattened values to output substructure for.
    index: Index at which to start reading from flat.

  Returns:
    The tuple (new_index, child), where:
      * new_index - the updated index into `flat` having processed `structure`.
      * packed - the subset of `flat` corresponding to `structure`,
                 having started at `index`, and packed into the same nested
                 format.

  Raises:
    ValueError: if `structure` contains more elements than `flat`
      (assuming indexing starts from `index`).
  r   )r3   r   _packed_nest_with_indicesappendr   )r=   ZflatindexpackedsZ	new_indexchildr   r   r   rS   &  s    
rS   c                 C   s   t |stdt||t | sHt|dkr@tdt| |d S t| }t|t|kr|tdt|t|| |f t| |d\}}t| |S )aF  Unflattens a sequence into a given structure.

  >>> tree.unflatten_as([[1, 2], [[3], [4]]], [5, 6, 7, 8])
  [[5, 6], [[7], [8]]]

  If `structure` is a scalar, `flat_sequence` must be a single-element list;
  in this case the return value is ``flat_sequence[0]``.

  >>> tree.unflatten_as(None, [1])
  1

  If `structure` is or contains a dict instance, the keys will be sorted to
  pack the flat sequence in deterministic order. This is true also for
  :class:`~collections.OrderedDict` instances: their sequence order is
  ignored, the sorting order of keys is used instead. The same convention
  is followed in :func:`~tree.flatten`. This correctly unflattens dicts
  and ``OrderedDict``\ s after they have been flattened, and also allows
  flattening an ``OrderedDict`` and then unflattening it back using a
  corresponding plain dict, or vice-versa.

  Dictionaries with non-sortable keys cannot be unflattened.

  >>> tree.unflatten_as({1: None, 2: None}, ['Hello', 'world!'])
  {1: 'Hello', 2: 'world!'}

  Args:
    structure: Arbitrarily nested structure.
    flat_sequence: Sequence to unflatten.

  Returns:
    `flat_sequence` unflattened into `structure`.

  Raises:
    ValueError: If `flat_sequence` and `structure` have different
      element counts.
    TypeError: If `structure` is or contains a mapping with non-sortable keys.
  z-flat_sequence must be a sequence not a {}:
{}r   z6Structure is a scalar but len(flat_sequence) == %d > 1r   zyCould not pack sequence. Structure had %d elements, but flat_sequence had %d elements.  Structure: %s, flat_sequence: %s.)	r   rL   formatrN   r;   rK   r   rS   r   )r=   Zflat_sequenceZflat_structurer1   rV   r   r   r   r   E  s(    & c                    s   t  std  |s td|dd}|rFtdd|  |dd D ]}t|d	 ||d
 qRt|d	  fddtt	t
| D S )a  Maps `func` through given structures.

  >>> structure = [[1], [2], [3]]
  >>> tree.map_structure(lambda v: v**2, structure)
  [[1], [4], [9]]
  >>> tree.map_structure(lambda x, y: x * y, structure, structure)
  [[1], [4], [9]]
  >>> Foo = collections.namedtuple('Foo', ['a', 'b'])
  >>> structure = Foo(a=1, b=2)
  >>> tree.map_structure(lambda v: v * 2, structure)
  Foo(a=2, b=4)

  Args:
    func: A callable that accepts as many arguments as there are structures.
    *structures: Arbitrarily nested structures of the same layout.
    **kwargs: The only valid keyword argument is `check_types`. If `True`
      (default) the types of components within the structures have
      to be match, e.g. ``tree.map_structure(func, [1], (1,))`` will raise
      a `TypeError`, otherwise this is not enforced. Note that namedtuples
      with identical name and fields are considered to be the same type.

  Returns:
    A new structure with the same layout as the given ones. If the
    `structures` have components of varying types, the resulting structure
    will use the same types as ``structures[0]``.

  Raises:
    TypeError: If `func` is not callable.
    ValueError: If the two structures do not have the same number of elements or
      if the two structures are not nested in the same way.
    TypeError: If `check_types` is `True` and any two `structures`
      differ in the types of their components.
    ValueError: If no structures were given or if a keyword argument other
      than `check_types` is provided.
  zfunc must be callable, got: %sz#Must provide at least one structurerQ   Tz8Only valid keyword arguments are `check_types` not: `%s`z`, `r   Nr   rQ   c                    s   g | ]} | qS r   r   )r&   argsfuncr   r   r*     s     z!map_structure.<locals>.<listcomp>)callablerL   rK   popjoinkeysr   r   zipmapr   )r]   
structureskwargsrQ   otherr   r\   r   r     s     $c                 O   s   t |d | f||S )a  Maps `func` through given structures.

  This is a variant of :func:`~tree.map_structure` which accumulates
  a *path* while mapping through the structures. A path is a tuple of
  indices and/or keys which uniquely identifies the positions of the
  arguments passed to `func`.

  >>> tree.map_structure_with_path(
  ...     lambda path, v: (path, v**2),
  ...     [{"foo": 42}])
  [{'foo': ((0, 'foo'), 1764)}]

  Args:
    func: A callable that accepts a path and as many arguments as there are
      structures.
    *structures: Arbitrarily nested structures of the same layout.
    **kwargs: The only valid keyword argument is `check_types`. If `True`
      (default) the types of components within the structures have to be match,
      e.g. ``tree.map_structure_with_path(func, [1], (1,))`` will raise a
      `TypeError`, otherwise this is not enforced. Note that namedtuples with
      identical name and fields are considered to be the same type.

  Returns:
    A new structure with the same layout as the given ones. If the
    `structures` have components of varying types, the resulting structure
    will use the same types as ``structures[0]``.

  Raises:
    TypeError: If `func` is not callable or if the `structures` do not
      have the same layout.
    TypeError: If `check_types` is `True` and any two `structures`
      differ in the types of their components.
    ValueError: If no structures were given or if a keyword argument other
      than `check_types` is provided.
  r   r   )r]   rd   re   r   r   r   r     s    $r   c           	      c   s   t | ts,t | tjtjfs8t| s8t| s8||fV  nRtt|}t| D ]<\}}||f }|| }t	|||dD ]\}}||fV  qtqLdS )a  Yields (path, value) pairs of input_tree flattened up to shallow_tree.

  Args:
    shallow_tree: Nested structure. Traverse no further than its leaf nodes.
    input_tree: Nested structure. Return the paths and values from this tree.
      Must have the same upper structure as shallow_tree.
    path: Tuple. Optional argument, only used when recursing. The path from the
      root of the original shallow_tree, down to the root of the shallow_tree
      arg of this recursive call.

  Yields:
    Pairs of (path, value), where path the tuple path of a leaf node in
    shallow_tree, and value is the value of the corresponding node in
    input_tree.
  )pathN)
r4   _TEXT_OR_BYTESr5   r   r   r
   r	   dictr/   _yield_flat_up_to)	shallow_tree
input_treerh   shallow_keyZshallow_subtreesubpathZinput_subtreeZ	leaf_pathZ
leaf_valuer   r   r   rk     s&    

rk   c              
   '   s   t  fdd|D  }z,|D ]"}t | \}}|dd | V  qW nR tk
r } z4t dd}td|jd  d	|d  d
|W 5 d}~X Y nX dS )z<Same as `_yield_flat_up_to`, but takes multiple input trees.c                    s   g | ]}t  |qS r   )rk   )r&   rm   rl   r   r   r*     s   z*_multiyield_flat_up_to.<locals>.<listcomp>Nr   paths)r   zCould not find key 'r   z' in some `input_trees`. Please ensure the structure of all `input_trees` are compatible with `shallow_tree`. The last valid path yielded was rA   )rb   KeyErrorlocalsgetrK   r[   )rl   Zinput_treesZzipped_iteratorsZpaths_and_valuesrq   valuesrR   r   rp   r   _multiyield_flat_up_to  s    rv   c                    s  t | rt |sH|dk	r6ttjt|t|dnttt|t| tr^t| j	}nt| }|rt||st
| d}t
|d}|r|rt| |sttjt||dn.t| tjrt|tjsttjt||dt|t| krttjt|t| dn,t|t| k r<ttjt|t| dt| }t|  fdd}|D ]6\}	}
||	}t|
||dk	r||	f nd|d	 q\dS )
aE  Asserts that `shallow_tree` is a shallow structure of `input_tree`.

  That is, this function recursively tests if each key in shallow_tree has its
  corresponding key in input_tree.

  Examples:

  The following code will raise an exception:

  >>> shallow_tree = {"a": "A", "b": "B"}
  >>> input_tree = {"a": 1, "c": 2}
  >>> _assert_shallow_structure(shallow_tree, input_tree)
  Traceback (most recent call last):
    ...
  ValueError: The shallow_tree's keys are not a subset of the input_tree's ...

  The following code will raise an exception:

  >>> shallow_tree = ["a", "b"]
  >>> input_tree = ["c", ["d", "e"], "f"]
  >>> _assert_shallow_structure(shallow_tree, input_tree)
  Traceback (most recent call last):
    ...
  ValueError: The two structures don't have the same sequence length. ...

  By setting check_types=False, we drop the requirement that corresponding
  nodes in shallow_tree and input_tree have to be the same type. Sequences
  are treated equivalently to Mappables that map integer keys (indices) to
  values. The following code will therefore not raise an exception:

  >>> _assert_shallow_structure({0: "foo"}, ["foo"], check_types=False)

  Args:
    shallow_tree: an arbitrarily nested structure.
    input_tree: an arbitrarily nested structure.
    path: if not `None`, a tuple containing the current path in the nested
      structure. This is only used for more informative errror messages.
    check_types: if `True` (default) the sequence types of `shallow_tree` and
      `input_tree` have to be the same.

  Raises:
    TypeError: If `shallow_tree` is a sequence but `input_tree` is not.
    TypeError: If the sequence types of `shallow_tree` are different from
      `input_tree`. Only raised if `check_types` is `True`.
    ValueError: If the sequence lengths of `shallow_tree` are different from
      `input_tree`.
  N)rh   
input_typeF)rw   shallow_type)Zinput_lengthZshallow_length)Z
input_sizeZshallow_sizec                    s2    D ]\}}|| kr|  S qt t| gd S r.   )rK   _SHALLOW_TREE_HAS_INVALID_KEYSrY   )rn   Z	input_keyinput_branchZ
input_iterr   r   get_matching_input_branchr  s    
z<_assert_shallow_structure.<locals>.get_matching_input_branchrZ   )r   rL   ._IF_SHALLOW_IS_SEQ_INPUT_MUST_BE_SEQ_WITH_PATHrY   listrN   $_IF_SHALLOW_IS_SEQ_INPUT_MUST_BE_SEQr4   r   __wrapped__r
   r   Zsame_namedtuples"_STRUCTURES_HAVE_MISMATCHING_TYPESr5   r   r>   rK   $_STRUCTURES_HAVE_MISMATCHING_LENGTHS%_INPUT_TREE_SMALLER_THAN_SHALLOW_TREEr/   _assert_shallow_structure)rl   rm   rh   rQ   rx   Zshallow_is_namedtupleZinput_is_namedtupleZshallow_iterr|   rn   Zshallow_branchrz   r   r{   r   r     st    3
 




r   c                 C   s$   t | |d|d dd t| |D S )a  Flattens `input_structure` up to `shallow_structure`.

  All further nested components in `input_structure` are retained as-is.

  >>> structure = [[1, 1], [2, 2]]
  >>> tree.flatten_up_to([None, None], structure)
  [[1, 1], [2, 2]]
  >>> tree.flatten_up_to([None, [None, None]], structure)
  [[1, 1], 2, 2]

  If `shallow_structure` and `input_structure` are not nested, the
  result is a single-element list:

  >>> tree.flatten_up_to(42, 1)
  [1]
  >>> tree.flatten_up_to(42, [1, 2, 3])
  [[1, 2, 3]]

  Args:
    shallow_structure: A structure with the same (but possibly more shallow)
      layout as `input_structure`.
    input_structure: An arbitrarily nested structure.
    check_types: If `True`, check that each node in shallow_tree has the
      same type as the corresponding node in `input_structure`.

  Returns:
    A list, the partially flattened version of `input_structure` wrt
    `shallow_structure`.

  Raises:
    TypeError: If the layout of `shallow_structure` does not match that of
      `input_structure`.
    TypeError: If `check_types` is `True` and `shallow_structure` and
      `input_structure` differ in the types of their components.
  Nrh   rQ   c                 S   s   g | ]\}}|qS r   r   )r&   r1   r2   r   r   r   r*     s     z!flatten_up_to.<locals>.<listcomp>)r   rk   shallow_structureZinput_structurerQ   r   r   r   r     s    $   c                 C   s   t | |d|d tt| |S )a  Flattens `input_structure` up to `shallow_structure`.

  This is a combination of :func:`~tree.flatten_up_to` and
  :func:`~tree.flatten_with_path`

  Args:
    shallow_structure: A structure with the same (but possibly more shallow)
      layout as `input_structure`.
    input_structure: An arbitrarily nested structure.
    check_types: If `True`, check that each node in shallow_tree has the
      same type as the corresponding node in `input_structure`.

  Returns:
    A list of ``(path, item)`` pairs corresponding to the partially flattened
    version of `input_structure` wrt `shallow_structure`.

  Raises:
    TypeError: If the layout of `shallow_structure` does not match that of
      `input_structure`.
    TypeError: If `input_structure` is or contains a mapping with non-sortable
      keys.
    TypeError: If `check_types` is `True` and `shallow_structure` and
      `input_structure` differ in the types of their components.
  r   r   )r   r~   rk   r   r   r   r   r     s       c                    s   t |  fddf||S )a  Maps `func` through given structures up to `shallow_structure`.

  This is a variant of :func:`~tree.map_structure` which only maps
  the given structures up to `shallow_structure`. All further nested
  components are retained as-is.

  >>> structure = [[1, 1], [2, 2]]
  >>> tree.map_structure_up_to([None, None], len, structure)
  [2, 2]
  >>> tree.map_structure_up_to([None, [None, None]], str, structure)
  ['[1, 1]', ['2', '2']]

  Args:
    shallow_structure: A structure with layout common to all `structures`.
    func: A callable that accepts as many arguments as there are structures.
    *structures: Arbitrarily nested structures of the same layout.
    **kwargs: No valid keyword arguments.
  Raises:
    ValueError: If `func` is not callable or if `structures` have different
      layout or if the layout of `shallow_structure` does not match that of
      `structures` or if no structures were given.

  Returns:
    A new structure with the same layout as `shallow_structure`.
  c                    s    | S r.   r   )r1   r[   r\   r   r   rI     rJ   z%map_structure_up_to.<locals>.<lambda>rg   )r   r]   rd   re   r   r\   r   r     s    
c                 O   sD   d|krt d ~g }t| f| D ]}|||  q&t| |S )a  Maps `func` through given structures up to `shallow_structure`.

  This is a combination of :func:`~tree.map_structure_up_to` and
  :func:`~tree.map_structure_with_path`

  Args:
    shallow_structure: A structure with layout common to all `structures`.
    func: A callable that accepts a path and as many arguments as there are
      structures.
    *structures: Arbitrarily nested structures of the same layout.
    **kwargs: No valid keyword arguments.

  Raises:
    ValueError: If `func` is not callable or if `structures` have different
      layout or if the layout of `shallow_structure` does not match that of
      `structures` or if no structures were given.

  Returns:
    Result of repeatedly applying `func`. Has the same structure layout
    as `shallow_tree`.
  rQ   zDThe use of `check_types` is deprecated and does not have any effect.)loggingwarningrv   rT   r   )r   r]   rd   re   resultsZpath_and_valuesr   r   r   r     s    
c                 C   s   t t| | S )a\  Flattens a possibly nested structure into a list.

  This is a variant of :func:`~tree.flattens` which produces a list of
  pairs: ``(path, item)``.  A path is a tuple of indices and/or keys
  which uniquely identifies the position of the corresponding ``item``.

  >>> tree.flatten_with_path([{"foo": 42}])
  [((0, 'foo'), 42)]

  Args:
    structure: An arbitrarily nested structure.

  Returns:
    A list of ``(path, item)`` pairs corresponding to the flattened version
    of the input `structure`.

  Raises:
    TypeError:
      If ``structure`` is or contains a mapping with non-sortable keys.
  )r~   rk   r<   r   r   r   r     s    c                    s   t  fdd||dS )aE  Traverses the given nested structure, applying the given function.

  The traversal is depth-first. If ``top_down`` is True (default), parents
  are returned before their children (giving the option to avoid traversing
  into a sub-tree).

  >>> visited = []
  >>> tree.traverse(visited.append, [(1, 2), [3], {"a": 4}], top_down=True)
  [(1, 2), [3], {'a': 4}]
  >>> visited
  [[(1, 2), [3], {'a': 4}], (1, 2), 1, 2, [3], 3, {'a': 4}, 4]

  >>> visited = []
  >>> tree.traverse(visited.append, [(1, 2), [3], {"a": 4}], top_down=False)
  [(1, 2), [3], {'a': 4}]
  >>> visited
  [1, 2, (1, 2), 3, [3], 4, {'a': 4}, [(1, 2), [3], {'a': 4}]]

  Args:
    fn: The function to be applied to each sub-nest of the structure.

      When traversing top-down:
        If ``fn(subtree) is None`` the traversal continues into the sub-tree.
        If ``fn(subtree) is not None`` the traversal does not continue into
        the sub-tree. The sub-tree will be replaced by ``fn(subtree)`` in the
        returned structure (to replace the sub-tree with None, use the special
        value :data:`MAP_TO_NONE`).

      When traversing bottom-up:
        If ``fn(subtree) is None`` the traversed sub-tree is returned unaltered.
        If ``fn(subtree) is not None`` the sub-tree will be replaced by
        ``fn(subtree)`` in the returned structure (to replace the sub-tree
        with None, use the special value :data:`MAP_TO_NONE`).

    structure: The structure to traverse.
    top_down: If True, parent structures will be visited before their children.

  Returns:
    The structured output from the traversal.
  c                    s    |S r.   r   )r1   xfnr   r   rI   S  rJ   ztraverse.<locals>.<lambda>)top_down)traverse_with_pathr   r=   r   r   r   r   r    *  s    )c                    s    fddd|S )a.  Traverses the given nested structure, applying the given function.

  The traversal is depth-first. If ``top_down`` is True (default), parents
  are returned before their children (giving the option to avoid traversing
  into a sub-tree).

  >>> visited = []
  >>> tree.traverse_with_path(
  ...  lambda path, subtree: visited.append((path, subtree)),
  ...  [(1, 2), [3], {"a": 4}],
  ...  top_down=True)
  [(1, 2), [3], {'a': 4}]
  >>> visited == [
  ...  ((), [(1, 2), [3], {'a': 4}]),
  ...  ((0,), (1, 2)),
  ...  ((0, 0), 1),
  ...  ((0, 1), 2),
  ...  ((1,), [3]),
  ...  ((1, 0), 3),
  ...  ((2,), {'a': 4}),
  ...  ((2, 'a'), 4)]
  True

  >>> visited = []
  >>> tree.traverse_with_path(
  ...  lambda path, subtree: visited.append((path, subtree)),
  ...  [(1, 2), [3], {"a": 4}],
  ...  top_down=False)
  [(1, 2), [3], {'a': 4}]
  >>> visited == [
  ...  ((0, 0), 1),
  ...  ((0, 1), 2),
  ...  ((0,), (1, 2)),
  ...  ((1, 0), 3),
  ...  ((1,), [3]),
  ...  ((2, 'a'), 4),
  ...  ((2,), {'a': 4}),
  ... ((), [(1, 2), [3], {'a': 4}])]
  True

  Args:
    fn: The function to be applied to the path to each sub-nest of the structure
      and the sub-nest value.
      When traversing top-down: If ``fn(path, subtree) is None`` the traversal
        continues into the sub-tree. If ``fn(path, subtree) is not None`` the
        traversal does not continue into the sub-tree. The sub-tree will be
        replaced by ``fn(path, subtree)`` in the returned structure (to replace
        the sub-tree with None, use the special
        value :data:`MAP_TO_NONE`).
      When traversing bottom-up: If ``fn(path, subtree) is None`` the traversed
        sub-tree is returned unaltered. If ``fn(path, subtree) is not None`` the
        sub-tree will be replaced by ``fn(path, subtree)`` in the returned
        structure (to replace the sub-tree
        with None, use the special value :data:`MAP_TO_NONE`).
    structure: The structure to traverse.
    top_down: If True, parent structures will be visited before their children.

  Returns:
    The structured output from the traversal.
  c                    sz    fddfdd}rJ }|dkr8| S |t krDdS |S n,| } |}|dkrf|S |t krrdS |S dS )z#Recursive traversal implementation.c                    s   | \}} |f |S r.   r   )r9   Zsubtree_pathZsubtree)rh   traverse_implr   r   
subtree_fn  s    z=traverse_with_path.<locals>.traverse_impl.<locals>.subtree_fnc                      s$   t  rt tt S  S d S r.   )r   r   rc   r/   r   )r=   r   r   r   traverse_subtrees  s
    zDtraverse_with_path.<locals>.traverse_impl.<locals>.traverse_subtreesN)r!   )rh   r=   r   retZtraversed_structurer   r   r   )rh   r=   r   r   r     s     

z)traverse_with_path.<locals>.traverse_implr   r   r   r   r   r   r   V  s    > r   )T)r   )NT)T)T)T)T)@r   collectionsr   r5   r   systypingr   r   r   r   r   sequencer	   r
   r   r   Zwraptr   ImportErrorobjecttreer   modules__all____version__rM   bytesri   ry   r   r   r   r   r}   r"   r#   ZStructureKVZ	StructurerL   r-   r3   r/   r>   r   r   r?   rG   r   rS   r   r   r   rk   rv   r   r   r   r   r   r   r!   r    r   r   r   r   r   <module>   s   



 "'	
0;6(
!  
s
, 
 !!
,