
    ˀh#                    "   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y)    )annotationsN)IterableMappingSequence)TYPE_CHECKINGAny)unstable)	DataFrame)N_INFER_DEFAULT)JSONEncoder)Schemac           	         |dkD  rSi }t        | t              rt        | |||      }|S t        | t              r| D cg c]  }t	        ||||       }}|S |S | S c c}w )Nr   )data	separator	max_levelencoderr   r   r   )
isinstancedict_normalize_json_orderedlist_simple_json_normalize)r   r   r   r   normalized_json_objectrownormalized_json_lists          t/var/www/html/wine-match-dev/backend/winematch-backend/venv/lib/python3.12/site-packages/polars/convert/normalize.pyr   r      s     1}!#dD!%<##	&"" &% d#  $  '''#	$  $ ('%%$s   Ac           	         t        | t              rV|dkD  rD|r| | nd}|dz
  }| j                         D ]   \  }}	|r| | n|}
t        |	|
||||       " |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   
key_stringnormalized_dictr   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?*i[1RH(1}"jjl 	
U08XJse,c&$3'.#	 	 +2$-OJ'""&*
#    c                    i i }}| j                         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   sr    2 B+D

 1aKNDG	 G dgr*   .T)r   r   schemastrictinfer_schema_lengthr   c               ^   |d}|dz  }t        | t              rt        |       dk(  rt        |      S t        | t              r| g} n9t        | 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}" │
    └──────┴────────────┴───────────────────────────────┘
    l        r   r   )r2   z expected list or dict of objectsr   )r2   r3   r4   )r   r   lenr
   r   r   strr   
ValueErrorjsondumpsr   )r   r   r   r2   r3   r4   r   msgs           r   json_normalizer<      s    p 	NI$!c$i1n''	D'	"v	D(	#JtS,ADz0o**		
 /
 
r*   )
r   /dict[Any, Any] | Sequence[dict[Any, Any] | Any]r   r7   r   intr   r   returnz+dict[Any, Any] | list[dict[Any, Any]] | Any)r   r   r!   r7   r"   dict[str, Any]r   r7   r   r>   r   r   r?   r@   )
r   r@   r   r7   r   r>   r   r   r?   r@   )r   r=   r   r7   r   
int | Noner2   zSchema | Noner3   boolr4   rA   r   zJSONEncoder | Noner?   r
   )
__future__r   r9   collections.abcr   r   r   typingr   r   polars._utils.unstabler	   polars.dataframer
   polars.datatypes.constantsr   polars._typingr   polars.schemar   r   r$   r   r<    r*   r   <module>rL      sZ   #  7 7 % + & 6*$
9  	
 1@2
22 $2 	2
 2 2 2j(
(( ( 	(
 (V 
   &5"&r
9r r 	r
 r r $r  r r rr*   