o
    ;DiM=                     @   s  d dl mZmZmZ d dl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mZmZmZmZ dd Zee_G d	d
 d
eZG dd deZdd Zdd ZdHddZee_dHddZee_dHddZee_ee_dHddZee_ee_dHddZee_ee_ dHddZ!e!e_!e!e_"dHddZ#e#e_#e#e_$dHd d!Z%e%e_%e%e_&dHd"d#Z'e'e_'dHd$d%Z(e(e_(dHd&d'Z)e)e_)dHd(d)Z*e*e_*dHd*d+Z+e+e_+dHd,d-Z,e,e_,dHd.d/Z-e-e_-dHd0d1Z.e.e_.dHd2d3Z/e/e_/dHd4d5Z0e0e_0dHd6d7Z1e1e_1e1e_2dHd8d9Z3e3e_3e3e_4dHd:d;Z5e5e_5e5e_6dHd<d=Z7e7e_7e7e_8d>d? Z9e9e_9G d@dA dAeZ:dBdC Z;dDdE Z<e<e_<dFdG Z=e=e_=dS )I    )absolute_importprint_functiondivisionN)nextstring_typescallable	text_type
Comparable)ArgumentError)	asindicesexprTablevaluesRecordc                 O   s   | dd}| dd}| dd}t|dkrtdt|d	krB|d }t|tr2t||d
}nt|s:J dt| |||dS |d }|d	 }t|sRJ dt| ||||dS )a  
    Select rows meeting a condition. E.g.::

        >>> import petl as etl
        >>> table1 = [['foo', 'bar', 'baz'],
        ...           ['a', 4, 9.3],
        ...           ['a', 2, 88.2],
        ...           ['b', 1, 23.3],
        ...           ['c', 8, 42.0],
        ...           ['d', 7, 100.9],
        ...           ['c', 2]]
        >>> # the second positional argument can be a function accepting
        ... # a row
        ... table2 = etl.select(table1,
        ...                     lambda rec: rec.foo == 'a' and rec.baz > 88.1)
        >>> table2
        +-----+-----+------+
        | foo | bar | baz  |
        +=====+=====+======+
        | 'a' |   2 | 88.2 |
        +-----+-----+------+

        >>> # the second positional argument can also be an expression
        ... # string, which will be converted to a function using petl.expr()
        ... table3 = etl.select(table1, "{foo} == 'a' and {baz} > 88.1")
        >>> table3
        +-----+-----+------+
        | foo | bar | baz  |
        +=====+=====+======+
        | 'a' |   2 | 88.2 |
        +-----+-----+------+

        >>> # the condition can also be applied to a single field
        ... table4 = etl.select(table1, 'foo', lambda v: v == 'a')
        >>> table4
        +-----+-----+------+
        | foo | bar | baz  |
        +=====+=====+======+
        | 'a' |   4 |  9.3 |
        +-----+-----+------+
        | 'a' |   2 | 88.2 |
        +-----+-----+------+

    The complement of the selection can be returned (i.e., the query can be
    inverted) by providing `complement=True` as a keyword argument.

    The ``trusted`` keyword argument can be used to specify whether the
    expression is trusted. See `:func:`petl.util.base.expr` for details.

    missingN
complementFtrustedTr   zmissing positional argument   )r   z*second argument must be string or callable)r   r   zthird argument must be callable)r   r   )	getlenr   
isinstancer   r   r   RowSelectViewFieldSelectView)tableargskwargsr   r   r   wherefield r   O/var/www/Datamplify/venv/lib/python3.10/site-packages/petl/transform/selects.pyselect   s&   4

r!   c                   @   s   e Zd ZdddZdd ZdS )r   NFc                 C   s   || _ || _|| _|| _d S N)sourcer   r   r   )selfr#   r   r   r   r   r   r    __init__\   s   
zRowSelectView.__init__c                 C   s   t | j| j| j| jS r"   )iterrowselectr#   r   r   r   r$   r   r   r    __iter__b   s   zRowSelectView.__iter__)NF__name__
__module____qualname__r%   r(   r   r   r   r    r   Z   s    
r   c                   @   s   e Zd ZdddZdd ZdS )r   FNc                 C   s"   || _ || _|| _|| _|| _d S r"   )r#   r   r   r   r   )r$   r#   r   r   r   r   r   r   r    r%   i   s
   
zFieldSelectView.__init__c                 C   s   t | j| j| j| j| jS r"   )iterfieldselectr#   r   r   r   r   r'   r   r   r    r(   p   s   zFieldSelectView.__iter__)FNr)   r   r   r   r    r   g   s    
r   c              	   c   s    t | }zt|}t|V  W n ty   g }Y nw t||}tj| }|D ]!}	z||	}
W n ty;   |}
Y nw t||
|krIt|	V  q(d S r"   )	iterr   tupleStopIterationr   operator
itemgetter
IndexErrorbool)r#   r   r   r   r   ithdrindicesgetvrowvr   r   r    r-   u   s(   


r-   c                 #   s    t | }zt|}W n
 ty   Y d S w ttt| t|V   fdd|D }|D ]}t|||kr=t|V  q.d S )Nc                 3   s    | ]
}t | d V  qdS ))r   Nr   .0r9   fldsr   r   r    	<genexpr>   s    z iterrowselect.<locals>.<genexpr>)r.   r   r0   listmapr   r/   r4   )r#   r   r   r   r5   r6   r9   r   r>   r    r&      s   

r&   Fc                    s    fdd}t | ||dS )zSelect rows of length `n`.c                    s   t |  kS r"   )r   )r9   nr   r    <lambda>   s    zrowlenselect.<locals>.<lambda>r   r!   )r   rD   r   r   r   rC   r    rowlenselect   s   rH   c                    s   t | | fdd|dS )zfSelect rows where the function `op` applied to the given field and
    the given value returns `True`.c                    s
    | S r"   r   r:   opvaluer   r    rE         
 zselectop.<locals>.<lambda>rF   rG   )r   r   rL   rK   r   r   rJ   r    selectop   s   rN   c                 C      t | ||tj|dS )z9Select rows where the given field equals the given value.rF   )rN   r1   eqr   r   rL   r   r   r   r    selecteq      rR   c                 C   rO   )zASelect rows where the given field does not equal the given value.rF   )rN   r1   nerQ   r   r   r    selectne   rS   rU   c                 C      t |}t| ||tj|dS )z?Select rows where the given field is less than the given value.rF   )r
   rN   r1   ltrQ   r   r   r    selectlt      rX   c                 C   rV   )zOSelect rows where the given field is less than or equal to the given
    value.rF   )r
   rN   r1   lerQ   r   r   r    selectle      r[   c                 C   rV   )zBSelect rows where the given field is greater than the given value.rF   )r
   rN   r1   gtrQ   r   r   r    selectgt   rY   r^   c                 C   rV   )zRSelect rows where the given field is greater than or equal to the given
    value.rF   )r
   rN   r1   gerQ   r   r   r    selectge   r\   r`   c                 C   rO   )z;Select rows where the given field contains the given value.rF   )rN   r1   containsrQ   r   r   r    selectcontains      rb   c                       t | | fdd|dS )zASelect rows where the given field is a member of the given value.c                    s   |  v S r"   r   rI   rL   r   r    rE          zselectin.<locals>.<lambda>rF   rG   rQ   r   re   r    selectin      rg   c                    rd   )zESelect rows where the given field is not a member of the given value.c                    s   |  vS r"   r   rI   re   r   r    rE     rf   zselectnotin.<locals>.<lambda>rF   rG   rQ   r   re   r    selectnotin   rh   ri   c                 C   rO   )z7Select rows where the given field `is` the given value.rF   )rN   r1   is_rQ   r   r   r    selectis
  rS   rk   c                 C   rO   )z;Select rows where the given field `is not` the given value.rF   )rN   r1   is_notrQ   r   r   r    selectisnot  rS   rm   c                 C   s   t | ||t|dS )zCSelect rows where the given field is an instance of the given type.rF   )rN   r   rQ   r   r   r    selectisinstance  s   rn   c                    *   t t   t| | fdd|dS )z^Select rows where the given field is greater than or equal to `minv` and
    less than `maxv`.c                    s   |   ko	 k S   S r"   r   rI   maxvminvr   r    rE   +      z%selectrangeopenleft.<locals>.<lambda>rF   r
   r!   r   r   rr   rq   r   r   rp   r    selectrangeopenleft%  
   rv   c                    ro   )z^Select rows where the given field is greater than `minv` and
    less than or equal to `maxv`.c                    s   |   k o	 kS   S r"   r   rI   rp   r   r    rE   8  rs   z&selectrangeopenright.<locals>.<lambda>rF   rt   ru   r   rp   r    selectrangeopenright2  rw   rx   c                    ro   )zjSelect rows where the given field is greater than or equal to `minv` and
    less than or equal to `maxv`.c                    s   |   ko	 kS   S r"   r   rI   rp   r   r    rE   E  rs   z!selectrangeopen.<locals>.<lambda>rF   rt   ru   r   rp   r    selectrangeopen?  rw   ry   c                    ro   )zRSelect rows where the given field is greater than `minv` and
    less than `maxv`.c                    s   t |   k o k S   S r"   r	   rI   rp   r   r    rE   R  s    z#selectrangeclosed.<locals>.<lambda>rF   rt   ru   r   rp   r    selectrangeclosedL  rw   rz   c                 C      t | |dd |dS )z3Select rows where the given field evaluates `True`.c                 S   s   t | S r"   r4   rI   r   r   r    rE   \  rf   zselecttrue.<locals>.<lambda>rF   rG   r   r   r   r   r   r    
selecttrueY  rS   r~   c                 C   r{   )z4Select rows where the given field evaluates `False`.c                 S   s
   t |  S r"   r|   rI   r   r   r    rE   f  rM   zselectfalse.<locals>.<lambda>rF   rG   r}   r   r   r    selectfalsec  rc   r   c                 C   r{   )z,Select rows where the given field is `None`.c                 S   s   | d u S r"   r   rI   r   r   r    rE   q  rf   zselectnone.<locals>.<lambda>rF   rG   r}   r   r   r    
selectnonen  rS   r   c                 C   r{   )z0Select rows where the given field is not `None`.c                 S   s   | d uS r"   r   rI   r   r   r    rE   {  rf   zselectnotnone.<locals>.<lambda>rF   rG   r}   r   r   r    selectnotnonex  rc   r   c                 C   s
   t | |S )a  
    Select rows based on data in the current row and/or previous and
    next row. E.g.::

        >>> import petl as etl
        >>> table1 = [['foo', 'bar'],
        ...           ['A', 1],
        ...           ['B', 4],
        ...           ['C', 5],
        ...           ['D', 9]]
        >>> def query(prv, cur, nxt):
        ...     return ((prv is not None and (cur.bar - prv.bar) < 2)
        ...             or (nxt is not None and (nxt.bar - cur.bar) < 2))
        ...
        >>> table2 = etl.selectusingcontext(table1, query)
        >>> table2
        +-----+-----+
        | foo | bar |
        +=====+=====+
        | 'B' |   4 |
        +-----+-----+
        | 'C' |   5 |
        +-----+-----+

    The `query` function should accept three rows and return a boolean value.

    )SelectUsingContextViewr   queryr   r   r    selectusingcontext  s   
r   c                   @   s   e Zd Zdd Zdd ZdS )r   c                 C   s   || _ || _d S r"   r   )r$   r   r   r   r   r    r%     s   
zSelectUsingContextView.__init__c                 C   s   t | j| jS r"   )iterselectusingcontextr   r   r'   r   r   r    r(     s   zSelectUsingContextView.__iter__Nr)   r   r   r   r    r     s    r   c                 #   s    t | }ztt|}W n
 ty   Y d S w ttt| |V   fdd|D }d }t|}|D ]}||||r>|V  |}|}q3|||d rN|V  d S d S )Nc                 3   s    | ]}t | V  qd S r"   r;   r<   r?   r   r    r@     s    z)iterselectusingcontext.<locals>.<genexpr>)r.   r/   r   r0   rA   rB   r   )r   r   r5   r6   prvcurnxtr   r   r    r     s(   
r   c                 C   s.   t  }tt| |D ]
}t| ||||< q
|S )a6  
    Return a dictionary mapping field values to tables. E.g.::

        >>> import petl as etl
        >>> table1 = [['foo', 'bar', 'baz'],
        ...           ['a', 4, 9.3],
        ...           ['a', 2, 88.2],
        ...           ['b', 1, 23.3],
        ...           ['c', 8, 42.0],
        ...           ['d', 7, 100.9],
        ...           ['c', 2]]
        >>> foo = etl.facet(table1, 'foo')
        >>> sorted(foo.keys())
        ['a', 'b', 'c', 'd']
        >>> foo['a']
        +-----+-----+------+
        | foo | bar | baz  |
        +=====+=====+======+
        | 'a' |   4 |  9.3 |
        +-----+-----+------+
        | 'a' |   2 | 88.2 |
        +-----+-----+------+

        >>> foo['c']
        +-----+-----+------+
        | foo | bar | baz  |
        +=====+=====+======+
        | 'c' |   8 | 42.0 |
        +-----+-----+------+
        | 'c' |   2 |      |
        +-----+-----+------+

        >>> # works with compound keys too
        >>> table2 = [['foo', 'bar', 'baz'],
        ...           ['a', 1, True],
        ...           ['b', 2, False],
        ...           ['b', 3, True],
        ...           ['b', 3, False]]
        >>> foobar = etl.facet(table2, ('foo', 'bar'))

        >>> sorted(foobar.keys())
        [('a', 1), ('b', 2), ('b', 3)]

        >>> foobar[('b', 3)]
        +-----+-----+-------+
        | foo | bar | baz   |
        +=====+=====+=======+
        | 'b' |   3 | True  |
        +-----+-----+-------+
        | 'b' |   3 | False |
        +-----+-----+-------+

    See also :func:`petl.util.materialise.facetcolumns`.

    )dictsetr   rR   )r   keyfctr:   r   r   r    facet  s   9r   c                 O   sD   d|d< t | g|R i |}d|d< t | g|R i |}||fS )a>  Return two tables, the first containing selected rows, the second
    containing remaining rows. E.g.::

        >>> import petl as etl
        >>> table1 = [['foo', 'bar', 'baz'],
        ...           ['a', 4, 9.3],
        ...           ['a', 2, 88.2],
        ...           ['b', 1, 23.3],
        ...           ['c', 8, 42.0],
        ...           ['d', 7, 100.9],
        ...           ['c', 2]]
        >>> table2, table3 = etl.biselect(table1, lambda rec: rec.foo == 'a')
        >>> table2
        +-----+-----+------+
        | foo | bar | baz  |
        +=====+=====+======+
        | 'a' |   4 |  9.3 |
        +-----+-----+------+
        | 'a' |   2 | 88.2 |
        +-----+-----+------+
        >>> table3
        +-----+-----+-------+
        | foo | bar | baz   |
        +=====+=====+=======+
        | 'b' |   1 |  23.3 |
        +-----+-----+-------+
        | 'c' |   8 |  42.0 |
        +-----+-----+-------+
        | 'd' |   7 | 100.9 |
        +-----+-----+-------+
        | 'c' |   2 |       |
        +-----+-----+-------+

    .. versionadded:: 1.1.0

    Fr   TrG   )r   r   r   t1t2r   r   r    biselect  s
   'r   )F)>
__future__r   r   r   r1   petl.compatr   r   r   r   petl.comparisonr
   petl.errorsr   petl.util.baser   r   r   r   r   r!   r   r   r-   r&   rH   rN   rR   rP   rU   rT   rX   rW   r[   rZ   r^   r]   r`   r_   rb   rg   ri   rk   rm   rn   rv   rx   ry   rz   r~   truer   falser   noner   notnoner   r   r   r   r   r   r   r   r    <module>   s    J

























 
?
.