a
    DBìjÛ'  ã                	   @  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œdd„Zddœdd„Zddœdd„ZeejddeeejejdœZddœdd„ZG dd„ dƒZ dS ) z
Module for scope operations
é    )Úannotations)ÚChainMapN)ÚStringIO)ÚTypeVar)Ú	Timestamp)ÚUndefinedVariableErrorÚ_KTÚ_VTc                   @  s2   e Zd ZdZddddœdd„Zdd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.
    r   r	   ÚNone)ÚkeyÚvalueÚreturnc                 C  s4   | j D ]}||v r|||<  d S q|| j d |< d S )Nr   )Úmaps)Úselfr   r   Úmapping© r   ú\/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/pandas/core/computation/scope.pyÚ__setitem__!   s
    
zDeepChainMap.__setitem__)r   r   c                 C  s,   | j D ]}||v r||=  dS qt|ƒ‚dS )z\
        Raises
        ------
        KeyError
            If `key` doesn't exist.
        N)r   ÚKeyError)r   r   r   r   r   r   Ú__delitem__(   s
    
zDeepChainMap.__delitem__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r
      s   r
   r   ÚintÚScope©Úlevelr   c                 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$   Ústr©r   c                 C  s,   zt | ƒ}W n ty"   | }Y n0 t|ƒS )z‚
    Replace a number with its hexadecimal representation. Used to tag
    temporary variables with their calling scope's id.
    )ÚordÚ	TypeErrorÚhex)ÚxZ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   ó    z_raw_hex_id.<locals>.<listcomp>)ÚstructÚpackÚidÚjoin)ÚobjÚpackedr   r   r   Ú_raw_hex_idS   s    r6   TF)r   ÚdatetimeÚTrueÚFalseÚlistÚtupleÚinfZ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)r4   Úsior   r   r   Ú_get_pretty_stringf   s    rA   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œdd„Zddœdd„Zeddœdd„ƒZ	dddœdd„Z
d*ddddœdd„Zdddœdd „Zdddœd!d"„Zddœd#d$„Zeddœd%d&„ƒZeddœd'd(„ƒZd
S )+r   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
   rB   r"   ÚdictrC   Nr   r   r   c           	      C  sô   |d | _ tt ¡ ƒ| _|| _t|tƒrT| j |j¡ |jd urH|j| _|  	|j ¡ t
 | j ¡}z`| j |d urt|n|j ¡ ¡}t|ƒ| _t|tƒs¼| j |d ur¦|n|j ¡ ¡}t|ƒ| _W ~n~0 t|tƒrà|t|jjƒ7 }t|Ž | _i | _d S )Nr   )r   r
   ÚDEFAULT_GLOBALSÚcopyrB   r#   Ú
isinstancer   ÚupdateÚ_updateÚsysÚ	_getframeÚ	new_childÚ	f_globalsÚf_localsr;   r"   r   rC   )	r   r   r    r!   r"   r#   ÚframeZscope_globalZscope_localr   r   r   Ú__init__”   s0    


ÿ

ÿ

zScope.__init__r%   r&   c                 C  s@   t t| j ¡ ƒƒ}t t| j ¡ ƒƒ}t| ƒj› d|› d|› d�S )Nz(scope=z, resolvers=ú))rA   r:   rB   Úkeysr"   Útyper   )r   Z
scope_keysZ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
        )rU   Úlenr"   ©r   r   r   r   Úhas_resolversÁ   s    zScope.has_resolvers)r   Úis_localc                 C  s–   z<|r| j | W S | jr$| j| W S |s.| jr2J ‚| j | W S  ty�   z| j| W  Y S  tyŠ } zt||ƒ|‚W Y d}~n
d}~0 0 Y n0 dS )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)rB   rX   r"   r   rC   r   )r   r   rY   Úerrr   r   r   ÚresolveÏ   s    zScope.resolve)Úold_keyÚnew_keyr   c                 C  sP   | j r| jj| jj }n| jj}| | j¡ |D ]}||v r0|||<  dS q0d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)rX   r"   r   rB   ÚappendrC   )r   r\   r]   Ú	new_valuer   r   r   r   r   Úswapkeyö   s    zScope.swapkeyz	list[str])Úscopesr   c                 C  sX   t  ||¡}|D ]B\}\}}}}}}z(t|d|› �ƒ}t| j |¡ƒ| _W ~q~0 q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')
        Zf_N)Ú	itertoolsÚproductÚgetattrr
   rB   rL   )r   Ústackra   Ú	variablesrB   rO   Ú_Údr   r   r   Ú	_get_vars  s    zScope._get_varsc              	   C  sL   |d }t  ¡ }z(| j|d|… dgd� W |dd…= ~n|dd…= ~0 dS )zƒ
        Update the current scope by going back `level` levels.

        Parameters
        ----------
        level : int
        r   NÚlocals)ra   )Úinspectre   ri   )r   r   Úslre   r   r   r   rI   &  s
    zScope._updatec                 C  sJ   t |ƒj› d| j› dt| ƒ› �}|| jvs.J ‚|| j|< || jv sFJ ‚|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.
        rg   )rS   r   Úntempsr6   rC   )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)rV   rC   rW   r   r   r   rm   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.
        )rC   r"   r   rB   r
   )r   r   r   r   r   Ú
full_scopeW  s    zScope.full_scope)NNr   N)N)r   r   r   r   Ú	__slots__Ú__annotations__rP   rT   ÚpropertyrX   r[   r`   ri   rI   ro   rm   rp   r   r   r   r   r   y   s(   
 ÿ(')NNr   N)!r   Ú
__future__r   Úcollectionsr   r7   rk   Úior   rb   r>   r0   rJ   Útypingr   ÚnumpyÚnpZpandas._libs.tslibsr   Zpandas.errorsr   r   r	   r
   r$   r+   r6   r:   r;   r<   rE   rA   r   r   r   r   r   Ú<module>   s>    ÿø