
    AHjy                    L   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	m
Z
 d dlmZ d dlmZ d dlmZ d dlmZ er5d 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"ddZ#ddZ$ G d d      Z% e%       Z&	 	 	 	 	 	 	 	 ddZ'ddZ(y)     )annotationsN)wraps)TYPE_CHECKINGAnyTypeVarcast)	functions)wrap_s)dtype_to_ffiname)Callable)	ParamSpec)Series)PySeries)PolarsDataTypeTP.c                   t        | dd      }t        |      }t        |       D ]  }|j                  d      r|dk7  st        | |      }t	        |      s3t        dt        |            }|j                  j                  d|j                  j                   }|||f|v s}t        |      s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_plotzCallable[..., Series])getattr_expr_lookupdir
startswithcallabler   _undecorated__code__co_varnamesco_argcount_is_empty_methodsetattr	call_expr)cls	namespaceexpr_lookupnameattrargss         K/root/tools/cai/cai_env/lib/python3.12/site-packages/polars/series/utils.pyexpr_dispatchr*      s     [$/Iy)KC 8 $ 3%D~3\$5GH }}001L4==3L3LM tT*k9>Nt>TCy7%8& J    c                   t        j                         }d|_        | t        ||       }t	               }t        |      D ]y  }|j                  d      r	 t        ||      }t        |      s.t        |      }|j                  j                  d|j                  j                   }|j                  | ||f       { |S # t        $ r Y w xY w)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     779DDL tY'UFD	 4s#D$' { !Ozz--.F

0F0FG

ItT234 M " s   B==	C	C	c                P    t        | d      r| j                  } t        | d      r| S )z1Return the given function without any decorators.__wrapped__)hasattrr7   )functions    r)   r   r   W   s(    
(M
*'' (M
*Or+   c                p     t               d fd       }t        |dt        j                                |S )z7Dispatch Series method to an expression implementation.c                4   t        | j                        }t        j                  |j                        }t        | dd       x}t        ||      }t        |j                        }|j                         j                   ||i |      j                         S )Nr   )
r
   _sFcolr&   r   __name__to_frame
select_seq	to_series)selfr(   kwargssr3   r$   ffuncs          r)   wrapperzcall_expr.<locals>.wrappera   s{    477OuuQVV} {D99IF4+DD$--(zz|&&q$'9&'9:DDFFr+   __signature__)rC   r   r(   r   rD   r   returnr   )r   r!   inspect	signature)rG   rH   s   ` r)   r"   r"   ^   s;     4[G G G_g&7&7&=>Nr+   c                    | j                   }|j                  t        v xr[ t        |j                        dk(  xr |j                  d   du xs. t
        j                  j                  dk(  xr |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)rG   fcs     r)   r    r    p   sl     
BJJ/) 	R\\	a		;BLLOt$; 	AII!#?(?r+   c                      e Zd ZddZddZy)_EmptyBytecodeHelperc                v    dd}dd}|j                   j                  |j                   j                  f| _        y )Nc                      y) N r^   r+   r)   _empty_with_docstringz<_EmptyBytecodeHelper.__init__.<locals>._empty_with_docstring   s    r+   c                      y rP   r^   r^   r+   r)   _empty_without_docstringz?_EmptyBytecodeHelper.__init__.<locals>._empty_without_docstring   s    r+   rJ   None)r   rQ   empty_bytecode)rC   r_   ra   s      r)   __init__z_EmptyBytecodeHelper.__init__   s6    		 "**22$--55
r+   c                    || j                   v S rP   )rd   )rC   items     r)   __contains__z!_EmptyBytecodeHelper.__contains__   s    t****r+   Nrb   )rg   bytesrJ   bool)r?   
__module____qualname__re   rh   r^   r+   r)   rZ   rZ      s    
+r+   rZ   c                V    t        |      }| j                  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&   dtypeobjffi_namefnames        r)   get_ffi_funcrs      s-    ,  &HLLx(E3t$$r+   c                n    ddl m} 	  |d        |        } |d       |S # t        $ r
  |d        w xY w)Nr   )check_lengthFT)polars._plrru   	Exception)rG   ru   results      r)   _with_no_check_lengthry      sD    (UT
 	  Ts   ! 4)r#   type[T]rJ   rz   )r$   z
str | NonerJ   z,set[tuple[str | None, str, tuple[str, ...]]])r9   Callable[P, T]rJ   r{   )rG   SeriesMethodrJ   r|   )rG   r|   rJ   rj   )r&   strro   r   rp   r   rJ   zCallable[..., Any] | None)rG   zCallable[..., Any]rJ   r   ))
__future__r   rK   rU   	functoolsr   typingr   r   r   r   polars._reexport	_reexportr-   polarsr	   r=   polars._utils.wrapr
   polars.datatypesr   collections.abcr   r   r   rv   r   polars._typingr   r   r   r|   r*   r   r   r"   r    rZ   rR   rs   ry   r^   r+   r)   <module>r      s    "  
  4 4  ! % -( $-A#ACK(L F4$"+ +$ '(%
%$%+3%%6r+   