
    cMh<C                        d dl mZ d dlmZmZ d dlZd dlmZmZm	Z	 d dl
Zd dlmZ d dlZd dlmZ erd dlmZ d dlmZmZ d.dZ	 	 	 	 d/d0dZd1dZd2dZ	 d3d4d!Z	 	 	 	 	 	 	 d5d6d-ZdS )7    )annotations)abcdefaultdictN)TYPE_CHECKINGAnyDefaultDictconvert_json_to_lines)	DataFrame)Iterable)IgnoreRaiseScalarsstrreturnc                h    | d         dk    s| d         dk    r| S | dd         } t          |           S )zJ
    Helper function that converts JSON lists to line delimited JSON.
    r   []   r	   )r   s    Y/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/pandas/io/json/_normalize.pyconvert_to_line_delimitsr       s=     Q43;;1R5C<<	!B$A ###     .prefixseplevelint	max_level
int | Nonec                H   d}t          | t                    r| g} d}g }| D ]}t          j        |          }|                                D ]\  }	}
t          |	t
                    st          |	          }	|dk    r|	}n||z   |	z   }t          |
t                    r|'||k    r!|dk    r|                    |	          }
|
||<   x|                    |	          }
|                    t          |
|||dz   |                     |	                    |           |r|d         S |S )a  
    A simplified json_normalize

    Converts a nested dict into a flat dict ("record"), unlike json_normalize,
    it does not attempt to extract a subset of the data.

    Parameters
    ----------
    ds : dict or list of dicts
    prefix: the prefix, optional, default: ""
    sep : str, default '.'
        Nested records will generate names separated by sep,
        e.g., for sep='.', { 'foo' : { 'bar' : 0 } } -> foo.bar
    level: int, optional, default: 0
        The number of levels in the json string.

    max_level: int, optional, default: None
        The max depth to normalize.

    Returns
    -------
    d - dict or list of dicts, matching `ds`

    Examples
    --------
    >>> nested_to_record(
    ...     dict(flat1=1, dict1=dict(c=1, d=2), nested=dict(e=dict(c=1, d=2), d=2))
    ... )
    {'flat1': 1, 'dict1.c': 1, 'dict1.d': 2, 'nested.e.c': 1, 'nested.e.d': 2, 'nested.d': 2}
    FTr   Nr   )

isinstancedictcopydeepcopyitemsr   popupdatenested_to_recordappend)dsr   r   r   r    	singletonnew_dsdnew_dkvnewkeys               r   r*   r*   -   sP   X I"d T	F  a  GGII 	Q 	QDAqa%% FFzz#) a&& %%9*<*<A::		!A$%E&M		!ALL)!VS%!)YOOPPPPe ayMr   datar   
key_stringnormalized_dictdict[str, Any]	separatorc                    t          | t                    rM|                                 D ]7\  }}| | | }|s|                    |          }t	          ||||           8n| ||<   |S )a3  
    Main recursive function
    Designed for the most basic use case of pd.json_normalize(data)
    intended as a performance improvement, see #15621

    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
    r4   r5   r6   r8   )r#   r$   r'   removeprefix_normalise_json)r4   r5   r6   r8   keyvaluenew_keys          r   r<   r<   ~   s    . $ +**,, 	 	JC#5Y555G :!..y99" /#	    	 '+
#r   c                    d |                                  D             }t          d |                                  D             di |          }i ||S )aw  
    Order the top level keys and then recursively go to depth

    Parameters
    ----------
    data : dict or list of dicts
    separator : str, default '.'
        Nested records will generate names separated by sep,
        e.g., for sep='.', { 'foo' : { 'bar' : 0 } } -> foo.bar

    Returns
    -------
    dict or list of dicts, matching `normalised_json_object`
    c                D    i | ]\  }}t          |t                    ||S  r#   r$   .0r1   r2   s      r   
<dictcomp>z+_normalise_json_ordered.<locals>.<dictcomp>   s-    JJJ$!QjD6I6IJAJJJr   c                D    i | ]\  }}t          |t                    ||S rB   rC   rD   s      r   rF   z+_normalise_json_ordered.<locals>.<dictcomp>   s-    CCCtq!z!T/B/BCaCCCr   r   r:   )r'   r<   )r4   r8   	top_dict_nested_dict_s       r   _normalise_json_orderedrJ      sh     KJ$**,,JJJI"CCtzz||CCC	  L )i(<((r   r,   dict | list[dict]dict | list[dict] | Anyc                    i }t          | t                    rt          |           }n%t          | t                    rfd| D             }|S |S )a  
    A optimized basic json_normalize

    Converts a nested dict into a flat dict ("record"), unlike
    json_normalize and nested_to_record it doesn't do anything clever.
    But for the most basic use cases it enhances performance.
    E.g. pd.json_normalize(data)

    Parameters
    ----------
    ds : dict or list of dicts
    sep : str, default '.'
        Nested records will generate names separated by sep,
        e.g., for sep='.', { 'foo' : { 'bar' : 0 } } -> foo.bar

    Returns
    -------
    frame : DataFrame
    d - dict or list of dicts, matching `normalised_json_object`

    Examples
    --------
    >>> _simple_json_normalize(
    ...     {
    ...         "flat1": 1,
    ...         "dict1": {"c": 1, "d": 2},
    ...         "nested": {"e": {"c": 1, "d": 2}, "d": 2},
    ...     }
    ... )
    {'flat1': 1, 'dict1.c': 1, 'dict1.d': 2, 'nested.e.c': 1, 'nested.e.d': 2, 'nested.d': 2}

    )r4   r8   c                2    g | ]}t          |           S )r   )_simple_json_normalize)rE   rowr   s     r   
<listcomp>z*_simple_json_normalize.<locals>.<listcomp>   s'    SSS 6s D D DSSSr   )r#   r$   rJ   list)r,   r   normalised_json_objectnormalised_json_lists    `  r   rP   rP      sm    V  "d $!8bC!P!P!P	B		 $SSSSPRSSS##!!r   raiserecord_pathstr | list | Nonemeta"str | list[str | list[str]] | Nonemeta_prefix
str | Nonerecord_prefixerrorsr   r   c                   	 dd fd
d!fdt          | t                    r| st                      S t          | t                    r| g} nFt          | t          j                  r%t          | t                    st          |           } nt          |&|$|" t          t          |                     S |:t          d | D                       rt          |           } t          |           S t          |t                    s|g}|g }nt          |t                    s|g}d |D             g g t          t                    fdD             d"d#f
d | |i d           t                    }|                    fd          }                                D ]\  }	}
|||	z   }	|	|v rt          d|	 d          t          j        |
t"                    }|j        dk    rCt          j        t)          |
          ft"                    }t+          |
          D ]
\  }}
|
||<   |                              ||	<   |S )$a  
    Normalize semi-structured JSON data into a flat table.

    Parameters
    ----------
    data : dict or list of dicts
        Unserialized JSON objects.
    record_path : str or list of str, default None
        Path in each object to list of records. If not passed, data will be
        assumed to be an array of records.
    meta : list of paths (str or list of str), default None
        Fields to use as metadata for each record in resulting table.
    meta_prefix : str, default None
        If True, prefix records with dotted (?) path, e.g. foo.bar.field if
        meta is ['foo', 'bar'].
    record_prefix : str, default None
        If True, prefix records with dotted (?) path, e.g. foo.bar.field if
        path to records is ['foo', 'bar'].
    errors : {'raise', 'ignore'}, default 'raise'
        Configures error handling.

        * 'ignore' : will ignore KeyError if keys listed in meta are not
          always present.
        * 'raise' : will raise KeyError if keys listed in meta are not
          always present.
    sep : str, default '.'
        Nested records will generate names separated by sep.
        e.g., for sep='.', {'foo': {'bar': 0}} -> foo.bar.
    max_level : int, default None
        Max number of levels(depth of dict) to normalize.
        if None, normalizes all levels.

    Returns
    -------
    frame : DataFrame
    Normalize semi-structured JSON data into a flat table.

    Examples
    --------
    >>> data = [
    ...     {"id": 1, "name": {"first": "Coleen", "last": "Volk"}},
    ...     {"name": {"given": "Mark", "family": "Regner"}},
    ...     {"id": 2, "name": "Faye Raker"},
    ... ]
    >>> pd.json_normalize(data)
        id name.first name.last name.given name.family        name
    0  1.0     Coleen      Volk        NaN         NaN         NaN
    1  NaN        NaN       NaN       Mark      Regner         NaN
    2  2.0        NaN       NaN        NaN         NaN  Faye Raker

    >>> data = [
    ...     {
    ...         "id": 1,
    ...         "name": "Cole Volk",
    ...         "fitness": {"height": 130, "weight": 60},
    ...     },
    ...     {"name": "Mark Reg", "fitness": {"height": 130, "weight": 60}},
    ...     {
    ...         "id": 2,
    ...         "name": "Faye Raker",
    ...         "fitness": {"height": 130, "weight": 60},
    ...     },
    ... ]
    >>> pd.json_normalize(data, max_level=0)
        id        name                        fitness
    0  1.0   Cole Volk  {'height': 130, 'weight': 60}
    1  NaN    Mark Reg  {'height': 130, 'weight': 60}
    2  2.0  Faye Raker  {'height': 130, 'weight': 60}

    Normalizes nested data up to level 1.

    >>> data = [
    ...     {
    ...         "id": 1,
    ...         "name": "Cole Volk",
    ...         "fitness": {"height": 130, "weight": 60},
    ...     },
    ...     {"name": "Mark Reg", "fitness": {"height": 130, "weight": 60}},
    ...     {
    ...         "id": 2,
    ...         "name": "Faye Raker",
    ...         "fitness": {"height": 130, "weight": 60},
    ...     },
    ... ]
    >>> pd.json_normalize(data, max_level=1)
        id        name  fitness.height  fitness.weight
    0  1.0   Cole Volk             130              60
    1  NaN    Mark Reg             130              60
    2  2.0  Faye Raker             130              60

    >>> data = [
    ...     {
    ...         "state": "Florida",
    ...         "shortname": "FL",
    ...         "info": {"governor": "Rick Scott"},
    ...         "counties": [
    ...             {"name": "Dade", "population": 12345},
    ...             {"name": "Broward", "population": 40000},
    ...             {"name": "Palm Beach", "population": 60000},
    ...         ],
    ...     },
    ...     {
    ...         "state": "Ohio",
    ...         "shortname": "OH",
    ...         "info": {"governor": "John Kasich"},
    ...         "counties": [
    ...             {"name": "Summit", "population": 1234},
    ...             {"name": "Cuyahoga", "population": 1337},
    ...         ],
    ...     },
    ... ]
    >>> result = pd.json_normalize(
    ...     data, "counties", ["state", "shortname", ["info", "governor"]]
    ... )
    >>> result
             name  population    state shortname info.governor
    0        Dade       12345   Florida    FL    Rick Scott
    1     Broward       40000   Florida    FL    Rick Scott
    2  Palm Beach       60000   Florida    FL    Rick Scott
    3      Summit        1234   Ohio       OH    John Kasich
    4    Cuyahoga        1337   Ohio       OH    John Kasich

    >>> data = {"A": [1, 2]}
    >>> pd.json_normalize(data, "A", record_prefix="Prefix.")
        Prefix.0
    0          1
    1          2

    Returns normalized data with columns prefixed with the given string.
    Fjsr7   spec
list | strextract_recordboolr   Scalar | Iterablec                4   | }	 t          |t                    r|D ]}|t          |          ||         }n||         }nV# t          $ rI}|rt          d| d          |dk    rt          j        cY d}~S t          d| d| d          |d}~ww xY w|S )zInternal function to pull fieldNzKey zS not found. If specifying a record_path, all elements of data should have the path.ignorez) not found. To replace missing values of z% with np.nan, pass in errors='ignore')r#   rS   KeyErrornpnan)r`   ra   rc   resultfielder^   s         r   _pull_fieldz#json_normalize.<locals>._pull_field  s    	$%% &! + +E~&uoo-#E]FF+
   	 	 	 21 2 2 2   !!v71 7 7q 7 7 7  	 s#   <A 
B'B3B9BBrS   c                     | |d          }t          |t                    s/t          j        |          rg }nt	          |  d| d| d          |S )z
        Internal function to pull field for records, and similar to
        _pull_field, but require to return list. And will raise error
        if has non iterable value.
        T)rc   z has non list value z
 for path z. Must be list or null.)r#   rS   pdisnull	TypeError)r`   ra   rk   rn   s      r   _pull_recordsz%json_normalize.<locals>._pull_records  s     Rd;;; &$'' 	y    , ,v , , , , ,   r   NrO   c              3  R   K   | ]"}d  |                                 D             V  #dS )c                8    g | ]}t          |t                    S rB   rC   )rE   xs     r   rR   z,json_normalize.<locals>.<genexpr>.<listcomp>  s"    888
1d##888r   N)values)rE   ys     r   	<genexpr>z!json_normalize.<locals>.<genexpr>  s9      GGQ88QXXZZ888GGGGGGr   r   r    c                B    g | ]}t          |t                    r|n|gS rB   )r#   rS   )rE   ms     r   rR   z"json_normalize.<locals>.<listcomp>  s-    ===1*Q%%.QQA3===r   c                :    g | ]}                     |          S rB   )join)rE   valr   s     r   rR   z"json_normalize.<locals>.<listcomp>  s#    0003#000r   r   r   r   Nonec                  
 t          | t                    r| g} t          |          dk    rn| D ]i}t          	          D ]0\  }}|dz   t          |          k    r 
||d                   ||<   1 ||d                  |dd          ||dz              jd S | D ]} ||d                   }fd|D             }                    t          |                     t          	          D ]S\  }}|dz   t          |          k    r	||         }n 
|||d                    }|                             |           T                    |           d S )Nr   r   r   r   c                b    g | ]+}t          |t                    rt          |           n|,S )rz   )r#   r$   r*   )rE   rr    r   s     r   rR   z>json_normalize.<locals>._recursive_extract.<locals>.<listcomp>  sR         "!T**$QC9EEEE  r   )r#   r$   lenzipr+   extend)r4   path	seen_metar   objr   r=   recsmeta_val_metarn   rs   _recursive_extractlengthsr    	meta_keys	meta_valsrecordsr   s            r   r   z*json_normalize.<locals>._recursive_extract  s   dD!! 	6Dt99q== W W #E9 5 5 C CHCqyCHH,,)4S#b')B)B	#""3tAw<abb9ETUIVVVVVW W  % %$}S$q'22     "	   s4yy))) #E9 5 5 4 4HCqy3s88++#,S>#.;sCK#@#@cN))(3333t$$$$#% %r   r   c                     |  S )NrB   )rv   r]   s    r   <lambda>z json_normalize.<locals>.<lambda>
  s    M1F11F1F r   )columnszConflicting metadata name z, need distinguishing prefix )dtyper   )F)r`   r7   ra   rb   rc   rd   r   re   )r`   r7   ra   rb   r   rS   )r   )r   r   r   r   )r#   rS   r   r$   r   r   r   NotImplementedErrorrP   anyr*   r   renamer'   
ValueErrorri   arrayobjectndimemptyr   	enumeraterepeat)r4   rW   rY   r[   r]   r^   r   r    rk   r1   r2   rw   ir   rn   rs   r   r   r   r   r   s       ````     @@@@@@@@r   json_normalizer      sG   \ FK      :     ( $ 
"d 
"{{	D$		 "v	D#,	'	' "
40E0E " Dzz!! 	L!/#>>>???GG$GGGGG 	H $DcYGGGDT** $"m|d## v=====E GG(..I0000%000I% % % % % % % % % % % % % % % %< t["A6666wF 'F'F'F'FGG !! + +1"aA;;MQMMM  
 !6***;??Xs1vviv666F!!  1q		MM'**q		Mr   )r   r   r   r   )r   r   r   N)r   r   r   r   r   r   r    r!   )
r4   r   r5   r   r6   r7   r8   r   r   r7   )r4   r7   r8   r   r   r7   )r   )r,   rK   r   r   r   rL   )NNNNrV   r   N)r4   rK   rW   rX   rY   rZ   r[   r\   r]   r\   r^   r   r   r   r    r!   r   r   )
__future__r   collectionsr   r   r%   typingr   r   r   numpyri   pandas._libs.writersr
   pandasrp   r   collections.abcr   pandas._typingr   r   r   r*   r<   rJ   rP   r   rB   r   r   <module>r      s   # " " " " "                      6 6 6 6 6 6           ((((((       
$ 
$ 
$ 
$  N N N N Nb& & & &R) ) ) )6 2" 2" 2" 2" 2"n &*/3" $! k k k k k k kr   