o
    ¹­§i#  ã                   @  sº   d dl mZ d dlZd dlmZmZmZ d dlmZm	Z	 d dl
mZ d dlmZ d dlmZ er<d dlmZ d d	lmZ d+dd„Zd,dd„Zd-dd„Zeƒ ddddedd œd.d)d*„ƒZdS )/é    )ÚannotationsN)ÚIterableÚMappingÚSequence)ÚTYPE_CHECKINGÚAny)Úunstable)Ú	DataFrame)ÚN_INFER_DEFAULT)ÚJSONEncoder)ÚSchemaÚdataú/dict[Any, Any] | Sequence[dict[Any, Any] | Any]Ú	separatorÚstrÚ	max_levelÚintÚencoderr   Úreturnú+dict[Any, Any] | list[dict[Any, Any]] | Anyc                   sV   ˆdkr)i }t | tƒrt| ˆˆˆ d}|S t | tƒr'‡ ‡‡fdd„| D ƒ}|S |S | S )Nr   )r   r   r   r   c                   s   g | ]
}t |ˆˆˆ d ‘qS )©r   r   r   )Ú_simple_json_normalize)Ú.0Úrow©r   r   r   © úO/home/app/Keep/.python/lib/python3.10/site-packages/polars/convert/normalize.pyÚ
<listcomp>#   s    úüÿz*_simple_json_normalize.<locals>.<listcomp>)Ú
isinstanceÚdictÚ_normalize_json_orderedÚlist)r   r   r   r   Znormalized_json_objectZnormalized_json_listr   r   r   r      s"   
ü
õù	r   r   Ú
key_stringÚnormalized_dictúdict[str, Any]c              	   C  sˆ   t | tƒr>|dkr6|r|› |› nd}|d }|  ¡ D ]\}}	|r'|› |› n|}
t|	|
||||d q|S || ƒ||< |S | ||< |S )a   
    Main recursive function.

    Designed for the most basic use case of `pl.json_normalize(data)`,
    intended as a performance improvement.

    Parameters
    ----------
    data : Any
        Type dependent on types contained within nested Json
    key_string : str
        New key (with separator(s) in) for data
    normalized_dict : dict
        The new normalized/flattened Json dict
    separator : str, default '.'
        Nested records will generate names separated by sep,
        e.g., for sep='.', { 'foo' : { 'bar' : 0 } } -> foo.bar
    max_level
        recursion depth
    encoder
        Custom JSON encoder; if not given, `json.dumps` is used.
    r   Ú é   ©r   r"   r#   r   r   r   )r   r   ÚitemsÚ_normalize_json)r   r"   r#   r   r   r   Zkey_rootZnested_max_levelÚkeyÚvalueZnew_keyr   r   r   r)   2   s&   
úür)   c           	      C  sX   i i }}|   ¡ D ]\}}t|tƒr|||< q	|||< q	t|di |||d}i |¥|¥S )a  
    Order the top level keys and then recursively go to depth.

    Parameters
    ----------
    data
        Deserialized JSON objects (dict or list of dicts)
    separator
        Nested records will generate names separated by sep. e.g.,
        for `separator=".", {"foo": {"bar": 0}}` -> foo.bar.
    max_level
        Max number of levels(depth of dict) to normalize.
    encoder
        Custom JSON encoder; if not given, `json.dumps` is used.

    Returns
    -------
    dict or list of dicts, matching `normalized_json_object`
    r%   r'   )r(   r   r   r)   )	r   r   r   r   Ztop_Znested_dataÚkÚvZnested_r   r   r   r    g   s   



úr    Ú.T)r   r   ÚschemaÚstrictÚinfer_schema_lengthr   ú
int | Noner/   úSchema | Noner0   Úboolr1   úJSONEncoder | Noner	   c                C  sš   |du rd}|d7 }t | tƒrt| ƒdkrt|dS t | tƒr#| g} nt | tƒr2t | tƒs2t| ƒ} nd}t|ƒ‚|du r?t	j
}tt| |||d|||dS )	uš  
    Normalize semi-structured deserialized JSON data into a flat table.

    Dictionary objects that will not be unnested/normalized are encoded
    as json string data. Unlike it pandas' counterpart, this function will
    not encode dictionaries as objects at any level.

    .. warning::
        This functionality is considered **unstable**. It may be changed
        at any point without it being considered a breaking change.

    Parameters
    ----------
    data
        Deserialized JSON objects.
    separator
        Nested records will generate names separated by sep. e.g.,
        for `separator=".", {"foo": {"bar": 0}}` -> foo.bar.
    max_level
        Max number of levels(depth of dict) to normalize.
        If None, normalizes all levels.
    schema
        Overwrite the `Schema` when the normalized data is passed to
        the `DataFrame` constructor.
    strict
        Whether Polars should be strict when constructing the DataFrame.
    infer_schema_length
        Number of rows to take into consideration to determine the schema.
    encoder
        Custom JSON encoder function; if not given, `json.dumps` is used.

    Examples
    --------
    >>> data = [
    ...     {
    ...         "id": 1,
    ...         "name": "Cole Volk",
    ...         "fitness": {"height": 180, "weight": 85},
    ...     },
    ...     {
    ...         "id": 2,
    ...         "name": "Faye Raker",
    ...         "fitness": {"height": 155, "weight": 58},
    ...     },
    ...     {
    ...         "name": "Mark Reg",
    ...         "fitness": {"height": 170, "weight": 78},
    ...     },
    ... ]
    >>> pl.json_normalize(data, max_level=1)
    shape: (3, 4)
    â”Œâ”€â”€â”€â”€â”€â”€â”¬â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”¬â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”¬â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”
    â”‚ id   â”† name       â”† fitness.height â”† fitness.weight â”‚
    â”‚ ---  â”† ---        â”† ---            â”† ---            â”‚
    â”‚ i64  â”† str        â”† i64            â”† i64            â”‚
    â•žâ•â•â•â•â•â•â•ªâ•â•â•â•â•â•â•â•â•â•â•â•â•ªâ•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•ªâ•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•¡
    â”‚ 1    â”† Cole Volk  â”† 180            â”† 85             â”‚
    â”‚ 2    â”† Faye Raker â”† 155            â”† 58             â”‚
    â”‚ null â”† Mark Reg   â”† 170            â”† 78             â”‚
    â””â”€â”€â”€â”€â”€â”€â”´â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”´â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”´â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”˜

    Normalize to a specific depth, using a custom JSON encoder
    (note that `orson.dumps` encodes to bytes, not str).

    >>> import orjson
    >>> pl.json_normalize(data, max_level=0, encoder=orjson.dumps)
    shape: (3, 3)
    â”Œâ”€â”€â”€â”€â”€â”€â”¬â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”¬â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”
    â”‚ id   â”† name       â”† fitness                       â”‚
    â”‚ ---  â”† ---        â”† ---                           â”‚
    â”‚ i64  â”† str        â”† binary                        â”‚
    â•žâ•â•â•â•â•â•â•ªâ•â•â•â•â•â•â•â•â•â•â•â•â•ªâ•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•¡
    â”‚ 1    â”† Cole Volk  â”† b"{"height":180,"weight":85}" â”‚
    â”‚ 2    â”† Faye Raker â”† b"{"height":155,"weight":58}" â”‚
    â”‚ null â”† Mark Reg   â”† b"{"height":170,"weight":78}" â”‚
    â””â”€â”€â”€â”€â”€â”€â”´â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”´â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”˜
    Nl        r&   r   )r/   z expected list or dict of objectsr   )r/   r0   r1   )r   r   Úlenr	   r   r   r   r!   Ú
ValueErrorÚjsonÚdumpsr   )r   r   r   r/   r0   r1   r   Úmsgr   r   r   Újson_normalize’   s0   X


ü÷r;   )
r   r   r   r   r   r   r   r   r   r   )r   r   r"   r   r#   r$   r   r   r   r   r   r   r   r$   )
r   r$   r   r   r   r   r   r   r   r$   )r   r   r   r   r   r2   r/   r3   r0   r4   r1   r2   r   r5   r   r	   )Ú
__future__r   r8   Úcollections.abcr   r   r   Útypingr   r   Zpolars._utils.unstabler   Zpolars.dataframer	   Zpolars.datatypes.constantsr
   Zpolars._typingr   Zpolars.schemar   r   r)   r    r;   r   r   r   r   Ú<module>   s*   

 
5+ø