o
    i!                     @  s  d dl mZ d dlZd dlmZmZ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e d dlmZ W d   n1 sNw   Y  erkd dlmZ d d	lmZ d d
lmZmZm Z  dddddddTddZ!dUddZ"ddddVd%d&Z#ed'dd(dWd/d0Z$ed'd1dXd4d0Z$d5d5d(dYd6d0Z$d5d1dZd8d9Z%ddd:d[d=d>Z&d\dAdBZ'd]dDdEZ(ddFd^dIdJZ)d_dNdOZ*d`dPdQZ+dadRdSZ,dS )b    )annotationsN)
CollectionIterableMapping)TYPE_CHECKINGAnyLiteraloverload)	functions)qualified_type_name)ComputeError)Expr)PyExpr)ColumnNameOrSelectorIntoExprPolarsDataTypeF)
str_as_litlist_as_series	structifydtyperequire_selectorinputr   r   boolr   r   r   PolarsDataType | Noner   returnr   c                C  s   t | tjr| }|rt|}|jS t | tr |s t| }|jS |r.dt| d}t|t | t	rB|rBtj
t| |d}|jS tj
| |d}|jS )a	  
    Parse a single input into an expression.

    Parameters
    ----------
    input
        The input to be parsed as an expression.
    str_as_lit
        Interpret string input as a string literal. If set to `False` (default),
        strings are parsed as column names.
    list_as_series
        Interpret list input as a Series literal. If set to `False` (default),
        lists are parsed as list literals.
    structify
        Convert multi-column expressions to a single struct expression.
    dtype
        If the input is expected to resolve to a literal with a known dtype, pass
        this to the `lit` constructor.
    require_selector
        Require that the input is a valid selector (eg: column name or selector).

    Returns
    -------
    PyExpr
    cannot turn  into selector)r   )
isinstanceplr   _structify_expressionstrFcolr   	TypeErrorlistZlitSeries_pyexpr)r   r   r   r   r   r   exprmsg r)   O/home/app/Keep/.python/lib/python3.10/site-packages/polars/_utils/parse/expr.pyparse_into_expression   s    "

r+   r'   r   c                 C  sV   | j  }|j  r)z| j  }W n ty    t| } Y | S w t||} | S N)metaZundo_aliasesZhas_multiple_outputsZoutput_namer   r!   structalias)r'   Zunaliased_exprZ	expr_namer)   r)   r*   r   I   s   

r   )__structify__require_selectorsinputsIntoExpr | Iterable[IntoExpr]r0   r1   named_inputslist[PyExpr]c                 O  s,   t ||| d}|rt|| d}|| |S )av  
    Parse multiple inputs into a list of expressions.

    Parameters
    ----------
    *inputs
        Inputs to be parsed as expressions, specified as positional arguments.
    **named_inputs
        Additional inputs to be parsed as expressions, specified as keyword arguments.
        The expressions will be renamed to the keyword used.
    __structify
        Convert multi-column expressions to a single struct expression.
    __require_selectors
        Require that all inputs are valid selectors (eg: column names or selector
        expressions), disallowing literals.

    Returns
    -------
    list of PyExpr
    require_selectorsr   r   )_parse_positional_inputs_parse_named_inputsextend)r0   r1   r2   r4   exprsZnamed_exprsr)   r)   r*   parse_into_list_of_expressionsU   s   
r=   .)strictraise_if_not_selectorir   r>   r?   Literal[False]pl.Selectorc                C     d S r,   r)   r@   r>   r?   r)   r)   r*   parse_into_selectorz      rE   r>   Literal[True]pl.Selector | Nonec                C  rC   r,   r)   rD   r)   r)   r*   rE      rF   Tc                C  sd   t | trtjj| g|ddS t | tjr| S t | tjr"| j S |r0dt| d}t	|d S )NTnamesr>   Zexpand_patternsr   r   )
r   r    r   Selector_by_namer   r-   Zas_selectorr   r#   )r@   r>   r?   r(   r)   r)   r*   rE      s   

7ColumnNameOrSelector | Collection[ColumnNameOrSelector]c                C  s   t | trCt | tsCdd | D }tjj||dd}t|t| kr$|S t|dkr4dd lm} |	 }| D ]
}|t
||dO }q6|S t
| |dS )Nc                 S  s   g | ]	}t |tr|qS r)   )r   r    ).0r@   r)   r)   r*   
<listcomp>   s    z,parse_list_into_selector.<locals>.<listcomp>TrJ   r   rG   )r   r   r    r   rL   rM   lenZpolars.selectors	selectorsemptyrE   )r2   r>   columnsselectorcsr@   r)   r)   r*   parse_list_into_selector   s    rW   r6   0tuple[IntoExpr, ...] | tuple[Iterable[IntoExpr]]r7   c                  s   t | } fdd|D S )Nc                   s   g | ]	}t | d qS ))r   r   )r+   )rO   er6   r)   r*   rP      s    z,_parse_positional_inputs.<locals>.<listcomp>)_parse_inputs_as_iterable)r2   r7   r   Zinputs_iterr)   r6   r*   r9      s   r9   &tuple[Any, ...] | tuple[Iterable[Any]]Iterable[Any]c                 C  sR   | sg S t | dkrt| d trd}t|t | dkr't| d r'| d S | S )N   r   u8  Cannot pass a dictionary as a single positional argument.
If you merely want the *keys*, use:
  • df.method(*your_dict.keys())
If you need the key value pairs, use one of:
  • unpack as keywords:    df.method(**your_dict)
  • build expressions:     df.method(expr.alias(k) for k, expr in your_dict.items()))rQ   r   r   r#   _is_iterable)r2   r(   r)   r)   r*   rZ      s   rZ   Any | Iterable[Any]c                 C  s   t | tot | tttjf S r,   )r   r   r    bytesr   r%   )r   r)   r)   r*   r^      s   r^   r8   dict[str, IntoExpr]Iterable[PyExpr]c                c  s,    |   D ]\}}t||d|V  qd S )Nr8   )itemsr+   r/   )r4   r   namer   r)   r)   r*   r:      s   r:   
predicatesconstraintsr   c                  O  s&   t | }|rt|}|| t|S )a  
    Parse predicates and constraints into a single expression.

    The result is an AND-reduction of all inputs.

    Parameters
    ----------
    *predicates
        Predicates to be parsed, specified as positional arguments.
    **constraints
        Constraints to be parsed, specified as keyword arguments.
        These will be converted to predicates of the form "keyword equals input value".

    Returns
    -------
    PyExpr
    )r9   _parse_constraintsr;   _combine_predicates)re   rf   Zall_predicatesZconstraint_predicatesr)   r)   r*   ,parse_predicates_constraints_into_expression   s
   
ri   c                 c  s,    |   D ]\}}t||jV  qd S r,   )rc   r!   r"   eqr&   )rf   rd   valuer)   r)   r*   rg     s   rg   c                 C  s.   | sd}t |t| dkr| d S t| S )Nz5at least one predicate or constraint must be providedr]   r   )r#   rQ   plrZall_horizontal)re   r(   r)   r)   r*   rh     s   
rh   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r'   r   r   r   )
r2   r3   r0   r   r1   r   r4   r   r   r5   )r@   r   r>   r   r?   rA   r   rB   )r@   r   r>   r   r?   rH   r   rI   )r@   r   r>   r   r?   r   r   rI   )r2   rN   r>   r   r   rB   )r2   rX   r7   r   r   r   r   r5   )r2   r[   r   r\   )r   r_   r   r   )r4   ra   r   r   r   rb   )re   r3   rf   r   r   r   )rf   ra   r   rb   )re   r5   r   r   )-
__future__r   
contextlibcollections.abcr   r   r   typingr   r   r   r	   Zpolars._reexportZ	_reexportr   Zpolarsr
   r!   Zpolars._utils.variousr   Zpolars.exceptionsr   suppressImportErrorZpolars._plrZ_plrrl   r   r   Zpolars._typingr   r   r   r+   r   r=   rE   rW   r9   rZ   r^   r:   ri   rg   rh   r)   r)   r)   r*   <module>   s^    
4%



