
    q-Ph#                        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Zd(dZd)dZ e            dddde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                    dk    rTi }t          | t                    rt          |           }n't          | t                    rfd| D             }|S |S | S )Nr   )r   r   r   r   c                6    g | ]}t          |           S )r   r   r   )_simple_json_normalize).0rowr   r   r   s     X/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/polars/convert/normalize.py
<listcomp>z*_simple_json_normalize.<locals>.<listcomp>#   sF     $ $ $  '''#	  $ $ $    )
isinstancedict_normalize_json_orderedlist)r   r   r   r   normalized_json_objectnormalized_json_lists    ```  r   r   r      s     1}}!#dD!! 	(%<##	& & &"" d## 
	($ $ $ $ $ $  $ $ $  ('%%r   r   
key_stringnormalized_dictdict[str, Any]c           	         t          | t                    r_|dk    rI|r| | nd}|dz
  }|                                 D ]$\  }}	|r| | n|}
t          |	|
||||           %n ||           ||<   |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   key_rootnested_max_levelkeyvaluenew_keys              r   r.   r.   2   s    < $ +q==5?G*1i111RH(1}"jjll 	 	
U08AX,s,,,c&$3'.#    	 +2'$--OJ'""&*
#r   c                    i i }}|                                  D ]%\  }}t          |t                    r|||<    |||<   &t          |di |||          }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   top_nested_datakvnested_s	            r   r"   r"   g   s    2 B+D

  1a 	KNNDGG  G dgr   .T)r   r   schemastrictinfer_schema_lengthr   
int | Noner;   Schema | Noner<   boolr=   JSONEncoder | Noner
   c                  |d}|dz  }t          | t                    r#t          |           dk    rt          |          S t          | t                    r| g} nKt          | t
                    r%t          | t                    st          |           } nd}t          |          |t          j
        }t          t          | |||          |||          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;   r<   r=   )r    r   lenr
   r   r   r   r#   
ValueErrorjsondumpsr   )r   r   r   r;   r<   r=   r   msgs           r   json_normalizerH      s    p 	NI$!! c$ii1nn''''	D'	"	" v	D(	#	# JtS,A,A Dzz0oo*		
 	
 	
 /
 
 
 
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   r>   r;   r?   r<   r@   r=   r>   r   rA   r   r
   )
__future__r   rE   collections.abcr   r   r   typingr   r   polars._utils.unstabler	   polars.dataframer
   polars.datatypes.constantsr   polars._typingr   polars.schemar   r   r.   r"   rH    r   r   <module>rR      sO   # " " " " "  7 7 7 7 7 7 7 7 7 7 % % % % % % % % + + + + + + & & & & & & 6 6 6 6 6 6 %******$$$$$$   @2 2 2 2j( ( ( (V 
   &5"&r r r r r r r rr   