
    jT                    X   d dl mZ d dlZd dlZd dlmZ d dlmZmZm	Z	 d dl
mZ d dlmZ d dlmZ d dlmZ er>d dlmZ d d	lmZ d d
lmZ d dlmZ d dlmZ  e	d          Z ed          Zedef         Zd-dZd.dZ d/dZ!d0dZ"d1dZ# G d  d!          Z$ e$            Z%d2d)Z&d3d,Z'dS )4    )annotationsN)wraps)TYPE_CHECKINGAnyTypeVar)	functions)wrap_s)dtype_to_ffiname)Callable)	ParamSpec)Series)PySeries)PolarsDataTypeTP.clstype[T]returnc                   t          | dd          }t          |          }t          |           D ]}|                    d          s|dk    rt          | |          }t	          |          rat          |          }|j        j        d|j        j                 }|||f|v r-t          |          rt          | |t          |                     | S )a  
    Series/NameSpace class decorator that sets up expression dispatch.

    * Applied to the Series class, and/or any Series 'NameSpace' classes.
    * Walks the class attributes, looking for methods that have empty function
      bodies, with signatures compatible with an existing Expr function.
    * IFF both conditions are met, the empty method is decorated with @call_expr.
    	_accessorN_plot)getattr_expr_lookupdir
startswithcallable_undecorated__code__co_varnamesco_argcount_is_empty_methodsetattr	call_expr)r   	namespaceexpr_lookupnameattrargss         X/home/longshao/multi-rider-rag/.venv/lib/python3.11/site-packages/polars/series/utils.pyexpr_dispatchr+      s     [$//Iy))KC 8 8 $$	8 3%%D~~ 8#D)) }01L4=3L1LM tT*k99>Nt>T>T9Cy777J    r%   
str | None,set[tuple[str | None, str, tuple[str, ...]]]c                   t          j                    }d|_        | t          ||           }t	                      }t          |          D ]}|                    d          sv	 t          ||          }n# t          $ r Y 5w xY wt          |          rEt          |          }|j
        j        d|j
        j                 }|                    | ||f           |S )zACreate lookup of potential Expr methods (in the given namespace).Nr   )plExpr_pyexprr   setr   r   AttributeErrorr   r   r   r    r!   add)r%   exprlookupr'   mr)   s         r*   r   r   =   s     799DDL tY''UUFD		 4 4s## 
	4D$''!   {{ 4 !OOz-.F
0F.FG

ItT2333Ms   "A33
B ?B functionCallable[P, T]c                T    t          | d          r| j        } t          | d          | S )z1Return the given function without any decorators.__wrapped__)hasattrr<   )r9   s    r*   r   r   W   s5    
(M
*
* (' (M
*
* (Or,   funcSeriesMethodc                     t                     d	 fd            }t          |dt          j                              |S )
z7Dispatch Series method to an expression implementation.selfr   r)   kwargsr   r   c                L   t          | j                  }t          j        |j                  }t          | dd           x}t          ||          }t          |j                  }|                                                     ||i |          	                                S )Nr   )
r	   _sFcolr'   r   __name__to_frame
select_seq	to_series)rA   r)   rB   sr6   r%   fr>   s          r*   wrapperzcall_expr.<locals>.wrappera   s    47OOuQV}} {D999IF4++DD$-((zz||&&qq$'9&'9'9::DDFFFr,   __signature__)rA   r   r)   r   rB   r   r   r   )r   r#   inspect	signature)r>   rM   s   ` r*   r$   r$   ^   s]     4[[G G G G G [G G_g&7&=&=>>>Nr,   boolc                    | j         }|j        t          v oFt          |j                  dk    r|j        d         du pt
          j        j        dk    o
|j        dk    S )z
    Confirm that the given function has no implementation.

    Definitions of empty:

    - only has a docstring (body is empty)
    - has no docstring and just contains 'pass' (or equivalent)
          NN)r   co_code_EMPTY_BYTECODElen	co_constssysflagsoptimize)r>   fcs     r*   r"   r"   p   s`     
BJ/) 	R\		a		;BLOt$; 	AI!#?(?r,   c                      e Zd Zd	dZd
dZdS )_EmptyBytecodeHelperr   Nonec                P    dd}dd}|j         j        |j         j        f| _        d S )Nr   r`   c                     dS ) N rd   r,   r*   _empty_with_docstringz<_EmptyBytecodeHelper.__init__.<locals>._empty_with_docstring   s      r,   c                     d S rU   rd   rd   r,   r*   _empty_without_docstringz?_EmptyBytecodeHelper.__init__.<locals>._empty_without_docstring   s    Dr,   r   r`   )r   rV   empty_bytecode)rA   re   rg   s      r*   __init__z_EmptyBytecodeHelper.__init__   sN    	 	 	 		 	 	 	 "*2$-5
r,   itembytesrQ   c                    || j         v S rU   )ri   )rA   rk   s     r*   __contains__z!_EmptyBytecodeHelper.__contains__   s    t***r,   Nrh   )rk   rl   r   rQ   )rG   
__module____qualname__rj   rn   rd   r,   r*   r_   r_      s<        
 
 
 
+ + + + + +r,   r_   r'   strdtyper   objr   Callable[..., Any] | Nonec                n    t          |          }|                     d|          }t          ||d          S )az  
    Dynamically obtain the proper FFI function/ method.

    Parameters
    ----------
    name
        function or method name where dtype is replaced by <>
        for example
            "call_foo_<>"
    dtype
        polars dtype.
    obj
        Object to find the method for.

    Returns
    -------
    callable or None
        FFI function, or None if not found.
    z<>N)r
   replacer   )r'   rr   rs   ffi_namefnames        r*   get_ffi_funcry      s6    ,  &&HLLx((E3t$$$r,   Callable[..., Any]r   c                    ddl m} 	  |d            |             } |d           |S # t          $ r  |d            w xY w)Nr   )check_lengthFT)polars._plrr|   	Exception)r>   r|   results      r*   _with_no_check_lengthr      s{    ((((((UT
 	    Ts	    * A)r   r   r   r   )r%   r-   r   r.   )r9   r:   r   r:   )r>   r?   r   r?   )r>   r?   r   rQ   )r'   rq   rr   r   rs   r   r   rt   )r>   rz   r   r   )(
__future__r   rO   rZ   	functoolsr   typingr   r   r   polars._reexport	_reexportr0   polarsr   rE   polars._utils.wrapr	   polars.datatypesr
   collections.abcr   r   r   r}   r   polars._typingr   r   r   r?   r+   r   r   r$   r"   r_   rW   ry   r   rd   r,   r*   <module>r      s   " " " " " "  



       . . . . . . . . . .       ! ! ! ! ! ! % % % % % % - - - - - - 
)((((((      $$$$$$------A	#ACK(L       F   4      $   "+ + + + + + + +$ '&((% % % %6     r,   