
    dB                        d dl mZ d dlmZmZ d dl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mZmZ d dlZd dlmZ d-dZ	 	 	 	 d.d/dZd0dZd1dZ	 d2d3d Z	 	 	 	 	 	 	 d4d5d,ZdS )6    )annotations)abcdefaultdictN)AnyDefaultDictIterableconvert_json_to_lines)IgnoreRaiseScalar)	DataFrame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    U/var/www/html/t/fyr/venv311/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                    rp|                                 D ]Z\  }}| | | }|s8t          j        dk     rddlm}  |||          }n|                    |          }t          ||||           [n| ||<   |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
    )   	   r   )removeprefixr3   r4   r5   r7   )r"   r#   r&   sysversion_infopandas.util._str_methodsr;   _normalise_json)r3   r4   r5   r7   keyvaluenew_keyr;   s           r   r@   r@   {   s    . $ +**,, 	 	JC#5Y555G >#f,,EEEEEE*l7I>>GG%229==G" /#	    	$ '+
#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#   .0r0   r1   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 rF   rG   rH   s      r   rJ   z+_normalise_json_ordered.<locals>.<dictcomp>   s-    CCCtq!z!T/B/BCaCCCr   r   r<   )r&   r@   )r3   r7   	top_dict_nested_dict_s       r   _normalise_json_orderedrN      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}

    )r3   r7   c                2    g | ]}t          |           S )r   )_simple_json_normalize)rI   rowr   s     r   
<listcomp>z*_simple_json_normalize.<locals>.<listcomp>   s'    SSS 6s D D DSSSr   )r"   r#   rN   list)r+   r   normalised_json_objectnormalised_json_lists    `  r   rT   rT      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 ]T\  }	}
|||	z   }	|	|v rt          d|	 d          t          j        |
t"                                                  ||	<   U|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jsr6   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"   rW   KeyErrornpnan)rd   re   rg   resultfielderb   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rW   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)rg   z has non list value z
 for path z. Must be list or null.)r"   rW   pdisnull	TypeError)rd   re   ro   rr   s      r   _pull_recordsz%json_normalize.<locals>._pull_records  s     Rd;;; &$'' 	y    , ,v , , , , ,   r   NrS   c              3  R   K   | ]"}d  |                                 D             V  #dS )c                8    g | ]}t          |t                    S rF   rG   )rI   xs     r   rV   z,json_normalize.<locals>.<genexpr>.<listcomp>  s"    888
1d##888r   N)values)rI   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 rF   )r"   rW   )rI   ms     r   rV   z"json_normalize.<locals>.<listcomp>  s-    ===1*Q%%.QQA3===r   c                :    g | ]}                     |          S rF   )join)rI   valr   s     r   rV   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 )r~   )r"   r#   r)   )rI   rr   r   s     r   rV   z>json_normalize.<locals>._recursive_extract.<locals>.<listcomp>  sR         "!T**$QC9EEEE  r   )r"   r#   lenzipr*   extend)r3   path	seen_metar   objr   rA   recsmeta_val_metarr   rw   _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 )NrF   )rz   ra   s    r   <lambda>z json_normalize.<locals>.<lambda>  s    M1F11F1F r   )columnszConflicting metadata name z, need distinguishing prefix )dtype)F)rd   r6   re   rf   rg   rh   r   ri   )rd   r6   re   rf   r   rW   )r   )r   r   r   r   )r"   rW   r   r#   r   r   r   NotImplementedErrorrT   anyr)   r   renamer&   
ValueErrorrm   arrayobjectrepeat)r3   r[   r]   r_   ra   rb   r   r   ro   r0   r1   r   rr   rw   r   r   r   r   r   s       ````   @@@@@@@@r   json_normalizer      s   \ 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   HQf---44W==q		Mr   )r   r   r   r   )r   r   r   N)r   r   r   r   r   r   r   r    )
r3   r   r4   r   r5   r6   r7   r   r   r6   )r3   r6   r7   r   r   r6   )r   )r+   rO   r   r   r   rP   )NNNNrZ   r   N)r3   rO   r[   r\   r]   r^   r_   r`   ra   r`   rb   r   r   r   r   r    r   r   )
__future__r   collectionsr   r   r$   r=   typingr   r   r   numpyrm   pandas._libs.writersr
   pandas._typingr   r   pandasrt   r   r   r)   r@   rN   rT   r   rF   r   r   <module>r      s   # " " " " "         



              6 6 6 6 6 6       
          
$ 
$ 
$ 
$  N N N N Nb+ + + +\) ) ) )6 2" 2" 2" 2" 2"n &*/3" $! a a a a a a ar   