o
    i\                     @  s&  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rdd 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d4ddZd5ddZ d6ddZ!d7d d!Z"d8d#d$Z#G d%d& d&Z$e$ Z%d9d.d/Z&d:d2d3Z'dS );    )annotationsN)wraps)TYPE_CHECKINGAnyTypeVar)	functions)wrap_s)dtype_to_ffiname)Callable)	ParamSpec)Series)PySeries)PolarsDataTypeTP.clstype[T]returnc                 C  s   t | dd}t|}t| D ]5}|dsC|dkrCt | |}t|rCt|}|jjd|jj }|||f|v rCt	|rCt
| |t| q| 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_Zplot)getattr_expr_lookupdir
startswithcallable_undecorated__code__co_varnamesco_argcount_is_empty_methodsetattr	call_expr)r   	namespaceZexpr_lookupnameattrargs r&   J/home/app/Keep/.python/lib/python3.10/site-packages/polars/series/utils.pyexpr_dispatch   s   

r(   r"   
str | None,set[tuple[str | None, str, tuple[str, ...]]]c              	   C  s   t  }d|_| durt|| }t }t|D ]2}|dsIzt||}W n	 ty.   Y qw t|rIt	|}|j
jd|j
j }|| ||f q|S )zACreate lookup of potential Expr methods (in the given namespace).Nr   )plExprZ_pyexprr   setr   r   AttributeErrorr   r   r   r   r   add)r"   exprlookupr#   mr%   r&   r&   r'   r   =   s$   

r   functionCallable[P, T]c                 C  s   t | dr| j} t | ds| S )z1Return the given function without any decorators.__wrapped__)hasattrr5   )r3   r&   r&   r'   r   W   s   

r   funcSeriesMethodc                   s,   t  d fd	d
}t|dt  |S )z7Dispatch Series method to an expression implementation.selfr   r%   P.argskwargsP.kwargsr   r   c                   s\   t | j}t|j}t| dd  }d urt||}t| j}| ||i |	 S )Nr   )
r   Z_sFcolr#   r   __name__Zto_frameZ
select_seqZ	to_series)r9   r%   r;   sr0   r"   fr7   r&   r'   wrappera   s   

zcall_expr.<locals>.wrapper__signature__N)r9   r   r%   r:   r;   r<   r   r   )r   r    inspect	signature)r7   rC   r&   rB   r'   r!   ^   s   
r!   boolc                 C  sB   | j }|jtv o t|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)r7   Zfcr&   r&   r'   r   p   s
   	
r   c                   @  s    e Zd ZdddZddd	Zd
S )_EmptyBytecodeHelperr   Nonec                 C  s*   ddd}ddd}|j j|j jf| _d S )Nr   rS   c                   S  s   dS ) Nr&   r&   r&   r&   r'   _empty_with_docstring   s    z<_EmptyBytecodeHelper.__init__.<locals>._empty_with_docstringc                   S  s   d S rJ   r&   r&   r&   r&   r'   _empty_without_docstring   s   z?_EmptyBytecodeHelper.__init__.<locals>._empty_without_docstringr   rS   )r   rK   empty_bytecode)r9   rU   rV   r&   r&   r'   __init__   s
   


z_EmptyBytecodeHelper.__init__itembytesrG   c                 C  s
   || j v S rJ   )rX   )r9   rZ   r&   r&   r'   __contains__   s   
z!_EmptyBytecodeHelper.__contains__NrW   )rZ   r[   r   rG   )r?   
__module____qualname__rY   r\   r&   r&   r&   r'   rR      s    
rR   r#   strdtyper   objr   Callable[..., Any] | Nonec                 C  s    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#   r`   ra   Zffi_namefnamer&   r&   r'   get_ffi_func   s   re   Callable[..., Any]r   c                 C  sB   ddl m} z|d |  }|d W |S  ty    |d  w )Nr   )check_lengthFT)polars._plrrg   	Exception)r7   rg   resultr&   r&   r'   _with_no_check_length   s   
rk   )r   r   r   r   )r"   r)   r   r*   )r3   r4   r   r4   )r7   r8   r   r8   )r7   r8   r   rG   )r#   r_   r`   r   ra   r   r   rb   )r7   rf   r   r   )(
__future__r   rE   rO   	functoolsr   typingr   r   r   Zpolars._reexportZ	_reexportr+   Zpolarsr   r=   Zpolars._utils.wrapr   Zpolars.datatypesr	   collections.abcr
   r   r   rh   r   Zpolars._typingr   r   r   r8   r(   r   r   r!   r   rR   rL   re   rk   r&   r&   r&   r'   <module>   s6    

#



