o
    Yi                     @   s   zd dl mZ W n ey   d dlmZ Y nw d dlZddlmZmZmZm	Z	 ddl
mZmZmZmZ eeee	dZeeeedZ							dd
dZdd ZdddZdS )    )MappingN   )tuple_reducerpath_reducerdot_reducerunderscore_reducer)tuple_splitterpath_splitterdot_splitterunderscore_splitter)tuplepathdot
underscorer   F c              	      s   t tf t| stdt| f dur$dk r$tdttr-t i d fdd	  | dd S )	a  Flatten `Mapping` object.

    Parameters
    ----------
    d : dict-like object
        The dict that will be flattened.
    reducer : {'tuple', 'path', 'underscore', 'dot', Callable}
        The key joining method. If a `Callable` is given, the `Callable` will be
        used to reduce.
        'tuple': The resulting key will be tuple of the original keys.
        'path': Use `os.path.join` to join keys.
        'underscore': Use underscores to join keys.
        'dot': Use dots to join keys.
    inverse : bool
        Whether you want invert the resulting key and value.
    max_flatten_depth : Optional[int]
        Maximum depth to merge.
    enumerate_types : Sequence[type]
        Flatten these types using `enumerate`.
        For example, if we set `enumerate_types` to ``(list,)``,
        `list` indices become keys: ``{'a': ['b', 'c']}`` -> ``{('a', 0): 'b', ('a', 1): 'c'}``.
    keep_empty_types : Sequence[type]
        By default, ``flatten({1: 2, 3: {}})`` will give you ``{(1,): 2}``, that is, the key ``3``
        will disappear.
        This is also applied for the types in `enumerate_types`, that is,
        ``flatten({1: 2, 3: []}, enumerate_types=(list,))`` will give you ``{(1,): 2}``.
        If you want to keep those empty values, you can specify the types in `keep_empty_types`:

        >>> flatten({1: 2, 3: {}}, keep_empty_types=(dict,))
        {(1,): 2, (3,): {}}

    Returns
    -------
    flat_dict : dict
    z3argument type %s is not in the flattenalbe types %sNr   z,max_flatten_depth should not be less than 1.c           	         s   t | r	t| nt| }d}|D ]?\}}d}||}t |r;d u s*|k r; ||d |d}|s:t |s;qrB||}}|v rMtd|||< q|S )NFTr   )depthparentduplicated key '{}')
isinstance	enumeratesix	viewitems
ValueErrorformat)	_dr   r   key_value_iterablehas_itemkeyvalueflat_key	has_child_flattenenumerate_types	flat_dictflattenable_typesinversekeep_empty_typesmax_flatten_depthreducerr   M/home/ubuntu/.local/lib/python3.10/site-packages/flatten_dict/flatten_dict.pyr"   V   s"   



zflatten.<locals>._flatten)r   )N)r   r   r   r   typestrREDUCER_DICT)dr)   r&   r(   r#   r'   r   r!   r*   flatten   s    +



r/   c                 C   sb   |sJ |d }t |dkr|| v rtd||| |< dS | |i } t| |dd | dS )zSet a value to a sequence of nested keys.

    Parameters
    ----------
    d : Mapping
    keys : Sequence[str]
    value : Any
    r   r   r   N)lenr   r   
setdefaultnested_set_dict)r.   keysr   r   r   r   r*   r2   u   s   	r2   c                 C   sP   t |tr	t| }i }t| D ]\}}|r||}}||}t||| q|S )a  Unflatten dict-like object.

    Parameters
    ----------
    d : dict-like object
        The dict that will be unflattened.
    splitter : {'tuple', 'path', 'underscore', 'dot', Callable}
        The key splitting method. If a Callable is given, the Callable will be
        used to split `d`.
        'tuple': Use each element in the tuple key as the key of the unflattened dict.
        'path': Use `pathlib.Path.parts` to split keys.
        'underscore': Use underscores to split keys.
        'dot': Use underscores to split keys.
    inverse : bool
        Whether you want to invert the key and value before flattening.

    Returns
    -------
    unflattened_dict : dict
    )r   r,   SPLITTER_DICTr   r   r2   )r.   splitterr&   unflattened_dictr   r   	key_tupler   r   r*   	unflatten   s   

r8   )r   FNr   r   )r   F)collections.abcr   ImportErrorcollectionsr   reducersr   r   r   r   	splittersr   r	   r
   r   r-   r4   r/   r2   r8   r   r   r   r*   <module>   s4    

Z