o
    ŀg'                  	   @  s  d Z ddlmZ ddlmZ ddlZddlZddlmZ ddl	Z	ddl
Z
ddlZddlZddlmZ ddlZddlmZ ddlmZ ed	Zed
ZG dd deeef Z	dd ddZd!ddZd!ddZeejddeeejejdZd!ddZG dd dZ dS )"z
Module for scope operations
    )annotations)ChainMapN)StringIO)TypeVar)	Timestamp)UndefinedVariableError_KT_VTc                   @  s$   e Zd ZdZddd	Zdd
dZdS )DeepChainMapz~
    Variant of ChainMap that allows direct updates to inner scopes.

    Only works when all passed mapping are mutable.
    keyr   valuer	   returnNonec                 C  s4   | j D ]}||v r|||<  d S q|| j d |< d S )Nr   )maps)selfr   r   mapping r   Q/var/www/html/myenv/lib/python3.10/site-packages/pandas/core/computation/scope.py__setitem__!   s   
zDeepChainMap.__setitem__c                 C  s(   | j D ]}||v r||=  dS qt|)z\
        Raises
        ------
        KeyError
            If `key` doesn't exist.
        N)r   KeyError)r   r   r   r   r   r   __delitem__(   s   
zDeepChainMap.__delitem__N)r   r   r   r	   r   r   )r   r   r   r   )__name__
__module____qualname____doc__r   r   r   r   r   r   r
      s    
r
   r   levelintr   Scopec                 C  s   t | d ||||dS )z.Ensure that we are grabbing the correct scope.   )global_dict
local_dict	resolverstarget)r   )r   r   r    r!   r"   r   r   r   ensure_scope6   s   r#   strc                 C  s0   z	t | }W t|S  ty   | }Y t|S w )z
    Replace a number with its hexadecimal representation. Used to tag
    temporary variables with their calling scope's id.
    )ord	TypeErrorhex)xhexinr   r   r   	_replacerC   s   
r*   c                 C  s$   t dt| }ddd |D S )z,Return the padded hexadecimal id of ``obj``.z@P c                 S  s   g | ]}t |qS r   )r*   ).0r(   r   r   r   
<listcomp>W   s    z_raw_hex_id.<locals>.<listcomp>)structpackidjoin)objpackedr   r   r   _raw_hex_idS   s   r4   TF)r   datetimeTrueFalselisttupleinfInfc                 C  s   t  }tj| |d | S )z
    Return a prettier version of obj.

    Parameters
    ----------
    obj : object
        Object to pretty print

    Returns
    -------
    str
        Pretty print object repr
    )stream)r   pprintgetvalue)r2   sior   r   r   _get_pretty_stringf   s   r@   c                   @  s   e Zd ZU dZg dZded< ded< ded< ded	< 	
d*d+ddZd,ddZed-ddZ	d.ddZ
d/d0ddZd1d d!Zd+d"d#Zd,d$d%Zed2d&d'Zed3d(d)Zd
S )4r   a  
    Object to hold scope, with a few bells to deal with some custom syntax
    and contexts added by pandas.

    Parameters
    ----------
    level : int
    global_dict : dict or None, optional, default None
    local_dict : dict or Scope or None, optional, default None
    resolvers : list-like or None, optional, default None
    target : object

    Attributes
    ----------
    level : int
    scope : DeepChainMap
    target : object
    temps : dict
    )r   scoper"   r!   tempsr   r   r
   rA   r!   dictrB   Nr   r   r   c           	      C  s   |d | _ tt | _|| _t|tr*| j|j |jd ur$|j| _| 	|j  t
| j }z0| j|d ur:|n|j }t|| _t|ts^| j|d urS|n|j }t|| _W ~n~w t|trp|t|jj7 }t| | _i | _d S )Nr   )r   r
   DEFAULT_GLOBALScopyrA   r"   
isinstancer   update_updatesys	_getframe	new_child	f_globalsf_localsr9   r!   r   rB   )	r   r   r   r    r!   r"   framescope_globalscope_localr   r   r   __init__   s2   








zScope.__init__r$   c                 C  s@   t t| j }t t| j }t| j d| d| dS )Nz(scope=z, resolvers=))r@   r8   rA   keysr!   typer   )r   
scope_keysres_keysr   r   r   __repr__   s   zScope.__repr__boolc                 C  s   t t| jS )z
        Return whether we have any extra scope.

        For example, DataFrames pass Their columns as resolvers during calls to
        ``DataFrame.eval()`` and ``DataFrame.query()``.

        Returns
        -------
        hr : bool
        )rX   lenr!   r   r   r   r   has_resolvers   s   zScope.has_resolversr   is_localc                 C  s   z|r	| j | W S | jr| j| W S |s| jrJ | j | W S  ty@   z| j| W  Y S  ty? } zt|||d}~ww w )a  
        Resolve a variable name in a possibly local context.

        Parameters
        ----------
        key : str
            A variable name
        is_local : bool
            Flag indicating whether the variable is local or not (prefixed with
            the '@' symbol)

        Returns
        -------
        value : object
            The value of a particular variable
        N)rA   r[   r!   r   rB   r   )r   r   r\   errr   r   r   resolve   s   zScope.resolveold_keynew_keyc                 C  sP   | j r| jj| jj }n| jj}|| j |D ]}||v r%|||<  dS qdS )a]  
        Replace a variable name, with a potentially new value.

        Parameters
        ----------
        old_key : str
            Current variable name to replace
        new_key : str
            New variable name to replace `old_key` with
        new_value : object
            Value to be replaced along with the possible renaming
        N)r[   r!   r   rA   appendrB   )r   r_   r`   	new_valuer   r   r   r   r   swapkey   s   zScope.swapkeyscopes	list[str]c                 C  sV   t ||}|D ] \}\}}}}}}zt|d| }t| j|| _W ~q~w dS )a  
        Get specifically scoped variables from a list of stack frames.

        Parameters
        ----------
        stack : list
            A list of stack frames as returned by ``inspect.stack()``
        scopes : sequence of strings
            A sequence containing valid stack frame attribute names that
            evaluate to a dictionary. For example, ('locals', 'globals')
        f_N)	itertoolsproductgetattrr
   rA   rK   )r   stackrd   	variablesrA   rN   _dr   r   r   	_get_vars  s   zScope._get_varsc              	   C  sJ   |d }t  }z| j|d| dgd W |dd= ~dS |dd= ~w )z
        Update the current scope by going back `level` levels.

        Parameters
        ----------
        level : int
        r   Nlocals)rd   )inspectrj   rn   )r   r   slrj   r   r   r   rH   &  s
   zScope._updatec                 C  sJ   t |j d| j dt|  }|| jvsJ || j|< || jv s#J |S )a#  
        Add a temporary variable to the scope.

        Parameters
        ----------
        value : object
            An arbitrary object to be assigned to a temporary variable.

        Returns
        -------
        str
            The name of the temporary variable created.
        rl   )rT   r   ntempsr4   rB   )r   r   namer   r   r   add_tmp:  s
    
zScope.add_tmpc                 C  s
   t | jS )z/The number of temporary variables in this scope)rY   rB   rZ   r   r   r   rr   R  s   
zScope.ntempsc                 C  s    | j g| jj | jj }t| S )z
        Return the full scope for use with passing to engines transparently
        as a mapping.

        Returns
        -------
        vars : DeepChainMap
            All variables in this scope.
        )rB   r!   r   rA   r
   )r   r   r   r   r   
full_scopeW  s   zScope.full_scopeNNr   N)r   r   r   r   r   r$   )r   rX   )r   r$   r\   rX   )N)r_   r$   r`   r$   r   r   )rd   re   r   r   )r   r   )r   r
   )r   r   r   r   	__slots____annotations__rQ   rW   propertyr[   r^   rc   rn   rH   rt   rr   ru   r   r   r   r   r   y   s*   
 
(
'


rv   )r   r   r   r   rw   )!r   
__future__r   collectionsr   r5   rp   ior   rg   r=   r.   rI   typingr   numpynppandas._libs.tslibsr   pandas.errorsr   r   r	   r
   r#   r*   r4   r8   r9   r:   rD   r@   r   r   r   r   r   <module>   s@    


