o
    Chb                     @  sN  d Z ddlmZ ddlZddlmZmZ ddl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mZ ddlm  mZ dd	lmZmZmZmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z& dd
l'm(Z(m)Z) ddl*m+Z+ ddl,m-Z- dDddZ.dDddZ/dDddZ0dd Z1dd Z2e2e0e/e.e(fdEddZ3dd Z4e4e5Z6e4e7Z8e9d d! d"d! e:eD D Z;e;fd#d$Z<e9d%d! e;D Z=e<ej>Z?e<ej@ZAe<ejBZCe<ejDZEe<ejFZGe<ejHZIe<ejJZKe<ejLZMe<ejNZOe<ejPZQe<ejRZSe<ejZTe<ejUZVe9g d&ZWe9g d'ZXeAe?B eQB eSB eTB eVB eEB eXB eW ZYe=eY eWB ZZeYeZ@ Z[d(e[ Z\e[r8J e\dFd+d,Z]ed-Z^dGd1d2Z_d3d4 Z`e e%d5Zad6d7 Zbe_eYebeaG d8d9 d9ejcZde9g d:Zee9ee Zfe_eYeeB eGe9g d;B  G d<d= d=edZge_eYeeB e9d>gB G d?d@ d@edZhG dAdB dBZiehegdCZjdS )Hz
:func:`~pandas.eval` parsers.
    )annotationsN)partialreduce	iskeyword)CallableClassVarTypeVar)UndefinedVariableError)is_string_dtype)ARITH_OPS_SYMSBOOL_OPS_SYMSCMP_OPS_SYMS	LOCAL_TAGMATHOPS
REDUCTIONSUNARY_OPS_SYMSBinOpConstantFuncNodeOpTermUnaryOpis_term)clean_backtick_quoted_tokstokenize_string)Scope)printingtoktuple[int, str]returnc                 C  s   | \}}||dkrdfS |fS )a`  
    Rewrite the assignment operator for PyTables expressions that use ``=``
    as a substitute for ``==``.

    Parameters
    ----------
    tok : tuple of int, str
        ints correspond to the all caps constants in the tokenize module

    Returns
    -------
    tuple of int, str
        Either the input or token or the replacement values
    === r   toknumtokvalr#   r#   n/var/www/html/myvaluetrips/my_value_trip_new/venv/lib/python3.10/site-packages/pandas/core/computation/expr.py_rewrite_assign3   s   r(   c                 C  sF   | \}}|t jkr|dkrt jdfS |dkrt jdfS ||fS ||fS )aj  
    Replace ``&`` with ``and`` and ``|`` with ``or`` so that bitwise
    precedence is changed to boolean precedence.

    Parameters
    ----------
    tok : tuple of int, str
        ints correspond to the all caps constants in the tokenize module

    Returns
    -------
    tuple of int, str
        Either the input or token or the replacement values
    &and|or)tokenizeOPNAMEr$   r#   r#   r'   _replace_booleansF   s   


r0   c                 C  s,   | \}}|t jkr|dkrt jtfS ||fS )a(  
    Replace local variables with a syntactically valid name.

    Parameters
    ----------
    tok : tuple of int, str
        ints correspond to the all caps constants in the tokenize module

    Returns
    -------
    tuple of int, str
        Either the input or token or the replacement values

    Notes
    -----
    This is somewhat of a hack in that we rewrite a string such as ``'@a'`` as
    ``'__pd_eval_local_a'`` by telling the tokenizer that ``__pd_eval_local_``
    is a ``tokenize.OP`` and to replace the ``'@'`` symbol with it.
    @)r-   r.   r   r$   r#   r#   r'   _replace_locals_   s   
r2   c                   s    fddS )z
    Compose 2 callables.
    c                    s    | i |S Nr#   )argskwargsfgr#   r'   <lambda>}   s    z_compose2.<locals>.<lambda>r#   r6   r#   r6   r'   	_compose2y      r:   c                  G  s   t | dks
J dtt| S )z&
    Compose 2 or more callables.
       z.At least 2 callables must be passed to compose)lenr   r:   )funcsr#   r#   r'   _compose   s   
r?   sourcestrc                   s,   t  sJ dt fddt| D S )a  
    Compose a collection of tokenization functions.

    Parameters
    ----------
    source : str
        A Python source code string
    f : callable
        This takes a tuple of (toknum, tokval) as its argument and returns a
        tuple with the same structure but possibly different elements. Defaults
        to the composition of ``_rewrite_assign``, ``_replace_booleans``, and
        ``_replace_locals``.

    Returns
    -------
    str
        Valid Python source code

    Notes
    -----
    The `f` parameter can be any callable that takes *and* returns input of the
    form ``(toknum, tokval)``, where ``toknum`` is one of the constants from
    the ``tokenize`` module and ``tokval`` is a string.
    zf must be callablec                 3  s    | ]} |V  qd S r3   r#   .0xr7   r#   r'   	<genexpr>       z_preparse.<locals>.<genexpr>)callabler-   
untokenizer   r@   r7   r#   rE   r'   	_preparse   s   rK   c                   s    fddS )zO
    Factory for a type checking function of type ``t`` or tuple of types.
    c                   s   t | j S r3   )
isinstancevalue)rD   tr#   r'   r9      s    z_is_type.<locals>.<lambda>r#   rN   r#   rN   r'   _is_type   s   rP   c                 c  s*    | ]}t |trt|tjr|V  qd S r3   )rL   type
issubclassastASTrC   noder#   r#   r'   rF      s    

rF   c                 c  s    | ]}t t|V  qd S r3   )getattrrS   )rC   namer#   r#   r'   rF      s    c                   s    fdd|D }t |S )zE
    Filter out AST nodes that are subclasses of ``superclass``.
    c                 3  s     | ]}t | r|jV  qd S r3   )rR   __name__rU   
superclassr#   r'   rF      s    z _filter_nodes.<locals>.<genexpr>)	frozenset)r[   	all_nodes
node_namesr#   rZ   r'   _filter_nodes   s   r_   c                 c      | ]}|j V  qd S r3   )rY   rB   r#   r#   r'   rF          )AssignModuleExpr)YieldGeneratorExpIfExpDictCompSetCompReprLambdaSetrT   IsIsNotz$cannot both support and not support 	node_nameCallable[..., None]c                       fdd}|S )zV
    Return a function that raises a NotImplementedError with a passed node name.
    c                   s   t d  d)N'z' nodes are not implemented)NotImplementedError)selfr4   r5   ro   r#   r'   r7        z _node_not_implemented.<locals>.fr#   )ro   r7   r#   ru   r'   _node_not_implemented   s   rw   _Tnodesset[str]Callable[[type[_T]], type[_T]]c                   s   d fdd}|S )z
    Decorator to disallow certain nodes from parsing. Raises a
    NotImplementedError instead.

    Returns
    -------
    callable
    clstype[_T]r    c                   sB   d| _  D ]}t|}d| }|  j |f7  _ t| || q| S )Nr#   visit_)unsupported_nodesrw   setattr)r|   rV   
new_methodrX   ry   r#   r'   
disallowed  s   
zdisallow.<locals>.disallowedN)r|   r}   r    r}   r#   )ry   r   r#   r   r'   disallow  s   
r   c                   s    fdd}|S )zw
    Return a function to create an op class with its symbol already passed.

    Returns
    -------
    callable
    c                   s   t  g|R i |S )z
        Return a partial function with an Op subclass with an operator already passed.

        Returns
        -------
        callable
        )r   )rt   rV   r4   r5   op_class	op_symbolr#   r'   r7   0  s   z_op_maker.<locals>.fr#   )r   r   r7   r#   r   r'   	_op_maker'  s   	
r   )binaryunaryc                   rq   )z9
    Decorator to add default implementation of ops.
    c                   sl      D ]/\}}t| | d}t| | d}|D ]}|| }|d ur2t||}t| d| | qq| S )N_ops_op_nodes_mapr~   )itemsrW   r   r   )r|   op_attr_namer   opsops_mapopop_nodemade_op
op_classesr#   r'   r7   E  s   
zadd_ops.<locals>.fr#   )r   r7   r#   r   r'   add_ops@  s   r   c                   @  s  e Zd ZU dZeZded< eZded< e	e
 e ZdZeeeeZeZdZeeeeZejejejejejejejejiZded< efdFddZdd Zdd Zdd Zdd Z dGddZ!dd Z"dd Z#		dHddZ$d d! Z%d"d# Z&dId%d&Z'dId'd(Z(dId)d*Z)dId+d,Z*dId-d.Z+dId/d0Z,e,Z-d1d2 Z.dId3d4Z/dJd6d7Z0d8d9 Z1d:d; Z2dKd<d=Z3d>d? Z4d@dA Z5dBdC Z6dDdE Z7dS )LBaseExprVisitorz
    Custom ast walker. Parsers of other engines should subclass this class
    if necessary.

    Parameters
    ----------
    env : Scope
    engine : str
    parser : str
    preparser : callable
    zClassVar[type[Term]]
const_type	term_type)GtLtGtELtEEqNotEqInNotInBitAndBitOrAndOrAddSubMultDivPowFloorDivMod)UAddUSubInvertNotztuple[str, ...]r   r    Nonec                 C  s"   || _ || _|| _|| _d | _d S r3   )envengineparser	preparserassignerrt   r   r   r   r   r#   r#   r'   __init__  s
   
zBaseExprVisitor.__init__c              
   K  s   t |tr1| |}z
tt|}W n ty0 } ztdd | D r*d|_	|d }~ww dt
|j }t| |}||fi |S )Nc                 s  s    | ]}t |V  qd S r3   r   rB   r#   r#   r'   rF     rG   z(BaseExprVisitor.visit.<locals>.<genexpr>z4Python keyword not valid identifier in numexpr queryr~   )rL   rA   r   rS   fix_missing_locationsparseSyntaxErroranysplitmsgrQ   rY   rW   )rt   rV   r5   cleanemethodvisitorr#   r#   r'   visit  s   


zBaseExprVisitor.visitc                 K  s2   t |jdkrtd|jd }| j|fi |S )Nr<   z#only a single expression is allowedr   )r=   bodyr   r   )rt   rV   r5   exprr#   r#   r'   visit_Module  s   
zBaseExprVisitor.visit_Modulec                 K  s   | j |jfi |S r3   r   rM   rt   rV   r5   r#   r#   r'   
visit_Expr  s   zBaseExprVisitor.visit_Exprc                 C  s   |j }t|}t|rVt|rV|| jv rVtt||f\}}tt||f\}}	|s.|s.|s.|	r4| j|  }|	rE| j|j	g}
| 
|
| j}|rV| j|j	g}
| 
|
| j}| |}||||fS r3   )r   rQ   r   rewrite_mapmap_is_list_is_strr   add_tmprM   r   r   )rt   rV   leftrightop_instanceop_type	left_list
right_listleft_str	right_strrX   r   r#   r#   r'   _rewrite_membership_op  s   
z&BaseExprVisitor._rewrite_membership_opNc                 C  sR   |d u r| j |jdd}|d u r| j |jdd}| |||\}}}}||||fS )Nr   )sider   )r   r   r   r   )rt   rV   r   r   r   r   r#   r#   r'   _maybe_transform_eq_ne  s   z&BaseExprVisitor._maybe_transform_eq_nec                 C  s   t t j}|jr't|dr'|js'|j|kr'| jt |j}| 	|| j}|jrHt|drH|jsH|j|krH| jt |j}| 	|| j}||fS )NrM   )
npdtypefloat32	is_scalarhasattrreturn_typer   r   rM   r   )rt   r   r   f32rX   r#   r#   r'   _maybe_downcast_constants  s(   

z)BaseExprVisitor._maybe_downcast_constantsc                 C  s   | | j| j| j| j|S r3   )evaluater   r   r   r   )rt   binopeval_in_pythonr#   r#   r'   _maybe_eval  s   zBaseExprVisitor._maybe_evalinznot inr"   z!=<>z<=z>=c                 C  s   |||}|j rtd|j d|j d|j d| jdkr6|jtv r)t|dds/t|ddr6| || jS |j|v rA| ||S | jdkrnt|dd t	ksft
t|dd sft|dd t	ksft
t|dd rn| ||| S |S )	Nz unsupported operand type(s) for z: 'z' and 'rr   pytablesis_datetimeFr   )has_invalid_return_type	TypeErrorr   rQ   r   r   rW   r   
binary_opsobjectr   )rt   r   r   lhsrhsr   maybe_eval_in_pythonresr#   r#   r'   _maybe_evaluate_binop  s6   
	






z%BaseExprVisitor._maybe_evaluate_binopc                 K  s2   |  |\}}}}| ||\}}| ||||S r3   )r   r   r   )rt   rV   r5   r   r   r   r   r#   r#   r'   visit_BinOp  s   zBaseExprVisitor.visit_BinOpc                 K  s    |  |j}|  |j}||S r3   )r   r   operand)rt   rV   r5   r   r   r#   r#   r'   visit_UnaryOp  s   zBaseExprVisitor.visit_UnaryOpr   c                 K  s   | j |j| jfi |S r3   )r   idr   r   r#   r#   r'   
visit_Name   s   zBaseExprVisitor.visit_Namec                 K     |  |j| jS r3   r   rM   r   r   r#   r#   r'   visit_NameConstant$  rv   z"BaseExprVisitor.visit_NameConstantc                 K  r   r3   r   r   r#   r#   r'   	visit_Num(  rv   zBaseExprVisitor.visit_Numc                 K  r   r3   r   r   r#   r#   r'   visit_Constant+  rv   zBaseExprVisitor.visit_Constantc                 K  s   | j |j}| || j S r3   )r   r   sr   rt   rV   r5   rX   r#   r#   r'   	visit_Str/  s   zBaseExprVisitor.visit_Strc                   s*    j  fdd|jD } | j S )Nc                      g | ]
}  | jqS r#   r   r   )rC   r   rt   r#   r'   
<listcomp>4      z.BaseExprVisitor.visit_List.<locals>.<listcomp>)r   r   eltsr   r  r#   r  r'   
visit_List3  s   zBaseExprVisitor.visit_Listc                 K  s   |  |jS )zdf.index[4]r   r   r#   r#   r'   visit_Index9  s   zBaseExprVisitor.visit_Indexc           
   	   K  s   ddl m} | |j}| |j}||| j| j| jd}z|j| }W n ty<   ||| j| j| jd}|| }Y nw | j	|}	| j
|	| jdS )Nr   )eval)
local_dictr   r   )r   )pandasr  r   rM   slicer   r   r   AttributeErrorr   r   )
rt   rV   r5   pd_evalrM   slobjresultvr   rX   r#   r#   r'   visit_Subscript=  s    zBaseExprVisitor.visit_Subscriptr  c                 K  sZ   |j }|dur| |j}|j}|dur| |j}|j}|dur'| |j}t|||S )zdf.index[slice(4,6)]N)lowerr   rM   upperstepr  )rt   rV   r5   r  r  r  r#   r#   r'   visit_SliceQ  s   zBaseExprVisitor.visit_Slicec                 K  s   t |jdkrtdt|jd tjstd| jjdu r"tdz| j	|jd fi |}W n t
y?   |jd j}Y nw t|d|| _| jdu rPtd| j	|jfi |S )	z
        support a single assignment node, like

        c = a + b

        set the assigner at the top level, must be a Name node which
        might or might not exist in the resolvers

        r<   z#can only assign a single expressionr   z5left hand side of an assignment must be a single nameNz%cannot assign without a target objectrX   z@left hand side of an assignment must be a single resolvable name)r=   targetsr   rL   rS   Namer   target
ValueErrorr   r
   r   rW   r   rM   )rt   rV   r5   r   r#   r#   r'   visit_Assign_  s"   

zBaseExprVisitor.visit_Assignc           	      K  s   |j }|j}|j}t|tjr@| |j}zt||}| j	|}| 
|| jW S  ty?   t|tjr>|j|kr>| Y S  w tdt|j )NzInvalid Attribute context )attrrM   ctxrL   rS   Loadr   rW   r   r   r   r  r  r   r  rQ   rY   )	rt   rV   r5   r  rM   r  resolvedr  rX   r#   r#   r'   visit_Attribute}  s   
zBaseExprVisitor.visit_Attributec                   sp  t |jtjr|jjdkr |j}n.t |jtjstdz |j}W n t	yA   zt
|jj}W n ty>    w Y nw |d u rOtd|jj t|drW|j}t |t
rv fdd|jD }|jrrtd|j d|| S  fd	d|jD }|jD ]!}t |tjstd
|jj d|jr |j j||j< q j||i |} j| jdS )N__call__z"Only named functions are supportedzInvalid function call rM   c                   s   g | ]}  |qS r#   )r   rC   argr  r#   r'   r    s    z.BaseExprVisitor.visit_Call.<locals>.<listcomp>z
Function "z$" does not support keyword argumentsc                   r  r#   r  r$  r  r#   r'   r    r  z keyword error in function call 'rr   )rX   r   )rL   funcrS   	Attributer  r"  r  r   r   r
   r   r   r  r   rM   r4   keywordsrX   keywordr%  r   r   r   )rt   rV   r   r5   r   new_argskeyrX   r#   r  r'   
visit_Call  sP   


zBaseExprVisitor.visit_Callc                 C  s   |S r3   r#   )rt   r   r#   r#   r'   translate_In  s   zBaseExprVisitor.translate_Inc              	   K  s   |j }|j}t|dkr#| |d }tj||j|d d}| |S |j}g }t||D ]\}}	| tj	|	g|| |gd}
|	}|
|
 q-| tjt |dS )Nr<   r   )r   r   r   )comparatorsr   r   )r   values)r   r.  r=   r-  rS   r   r   r   zipCompareappendBoolOpr   )rt   rV   r5   r   compsr   r   r   r/  compnew_noder#   r#   r'   visit_Compare  s   
zBaseExprVisitor.visit_Comparec                 C  s   t |ttfr	|S | |S r3   )rL   r   r   r   )rt   bopr#   r#   r'   _try_visit_binop  s   
z BaseExprVisitor._try_visit_binopc                   s    fdd} j }t||S )Nc                   s<    | } |} ||\}}}}| j||S r3   )r9  r   r   r   )rD   yr   r   r   r   rV   rt   r#   r'   r     s   

z-BaseExprVisitor.visit_BoolOp.<locals>.visitor)r/  r   )rt   rV   r5   r   operandsr#   r;  r'   visit_BoolOp  s   
zBaseExprVisitor.visit_BoolOpr    r   )NN)r   r   )r    r   )r    r  r3   )8rY   
__module____qualname____doc__r   r   __annotations__r   r   r   r   r   r   binary_op_nodesdictr0  binary_op_nodes_mapr   	unary_opsunary_op_nodesunary_op_nodes_maprS   r   r   r   r   r   rK   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r	  visit_Tupler
  r  r  r  r"  r,  r-  r7  r9  r=  r#   r#   r#   r'   r   S  s\   
 

)








3r   )Dictr3  r   r   )r3  r'  r   r   Tuplec                      s2   e Zd Zeeeeeedfd fddZ	  Z
S )PandasExprVisitorrE   r    r   c                   s   t  |||| d S r3   superr   r   	__class__r#   r'   r     s   
zPandasExprVisitor.__init__r>  )rY   r?  r@  r   rK   r?   r2   r0   r   r   __classcell__r#   r#   rO  r'   rL    s    

rL  r   c                      s(   e Zd Zdddfd	 fddZ  ZS )
PythonExprVisitorNc                 C  s   | S r3   r#   rJ   r#   r#   r'   r9     s    zPythonExprVisitor.<lambda>r    r   c                   s   t  j||||d d S )N)r   rM  r   rO  r#   r'   r     s   zPythonExprVisitor.__init__r3   r>  )rY   r?  r@  r   rQ  r#   r#   rO  r'   rR    s    rR  c                   @  sz   e Zd ZU dZded< ded< ded< 					
ddddZedd Zdd Zd ddZ	d!ddZ
dd Zedd Zd	S )"rd   a  
    Object encapsulating an expression.

    Parameters
    ----------
    expr : str
    engine : str, optional, default 'numexpr'
    parser : str, optional, default 'pandas'
    env : Scope, optional, default None
    level : int, optional, default 2
    r   r   rA   r   r   numexprr  Nr   Scope | Nonelevelintr    r   c                 C  sL   || _ |pt|d d| _|| _|| _t| | j| j| j| _|  | _d S )Nr<   )rU  )	r   r   r   r   r   PARSERS_visitorr   terms)rt   r   r   r   r   rU  r#   r#   r'   r     s   zExpr.__init__c                 C  s   t | jdd S )Nr   )rW   rX  r  r#   r#   r'   r   +  s   zExpr.assignerc                 C  s   |  | jS r3   )rY  r   r  r#   r#   r'   r#  /     zExpr.__call__c                 C  s   t | jS r3   )r   pprint_thingrY  r  r#   r#   r'   __repr__2  rZ  zExpr.__repr__c                 C  s
   t | jS r3   )r=   r   r  r#   r#   r'   __len__5  s   
zExpr.__len__c                 C  s   | j | jS )z&
        Parse an expression.
        )rX  r   r   r  r#   r#   r'   r   8  r;   z
Expr.parsec                 C  s2   t | jrt| jjgS tdd t| jD S )z1
        Get the names in an expression.
        c                 s  r`   r3   )rX   )rC   termr#   r#   r'   rF   E  ra   zExpr.names.<locals>.<genexpr>)r   rY  r\   rX   comflattenr  r#   r#   r'   names>  s   
z
Expr.names)rS  r  Nr   )
r   rA   r   rA   r   rT  rU  rV  r    r   )r    rA   )r    rV  )rY   r?  r@  rA  rB  r   propertyr   r#  r\  r]  r   ra  r#   r#   r#   r'   rd     s$   
 


rd   )pythonr  )r   r   r    r   )r@   rA   r    rA   )ro   rA   r    rp   )ry   rz   r    r{   )krA  
__future__r   rS   	functoolsr   r   r)  r   r-   typingr   r   r	   numpyr   pandas.errorsr
   pandas.core.dtypes.commonr   pandas.core.commoncorecommonr_  pandas.core.computation.opsr   r   r   r   r   r   r   r   r   r   r   r   r   r   pandas.core.computation.parsingr   r   pandas.core.computation.scoper   pandas.io.formatsr   r(   r0   r2   r:   r?   rK   rP   listr   rA   r   r\   dir
_all_nodesr_   _all_node_namesmod
_mod_nodesstmt_stmt_nodesr   _expr_nodesexpr_context_expr_context_nodesboolop_boolop_nodesoperator_operator_nodesunaryop_unary_op_nodescmpop_cmp_op_nodescomprehension_comprehension_nodesexcepthandler_handler_nodes	arguments_arguments_nodes_keyword_nodesalias_alias_nodes_hacked_nodes_unsupported_expr_nodes_unsupported_nodes_base_supported_nodesintersection_msgrw   rx   r   r   _op_classesr   NodeVisitorr   _python_not_supported_numexpr_supported_callsrL  rR  rd   rW  r#   r#   r#   r'   <module>   s    @



"
















   =