o
    ;DiB                     @   s&  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mZ d dlmZmZmZmZ d dlmZ dd Zee_d	d
 Zee_dd Zee_dd Zee_dd Zee_d$ddZee_G dd deZdd Zdd Zdd Zdd Zee_dd Z e e_ d d! Z!e!e_!d"d# Z"e"e_"dS )%    )absolute_importprint_functiondivision)nextinteger_typesstring_types	text_typeN)ArgumentErrorFieldSelectionError)Tableexpr
fieldnamesRecord)	numparserc                 O   s   d}t |dkr	n>t |dkr|d }n3t |dkrGt }|d }t |dkr,|d }n|dd }t|ttfrC|D ]}|||< q;n|||< t| |fi |S )a   Transform values under one or more fields via arbitrary functions, method
    invocations or dictionary translations. E.g.::

        >>> import petl as etl
        >>> table1 = [['foo', 'bar', 'baz'],
        ...           ['A', '2.4', 12],
        ...           ['B', '5.7', 34],
        ...           ['C', '1.2', 56]]
        >>> # using a built-in function:
        ... table2 = etl.convert(table1, 'bar', float)
        >>> table2
        +-----+-----+-----+
        | foo | bar | baz |
        +=====+=====+=====+
        | 'A' | 2.4 |  12 |
        +-----+-----+-----+
        | 'B' | 5.7 |  34 |
        +-----+-----+-----+
        | 'C' | 1.2 |  56 |
        +-----+-----+-----+

        >>> # using a lambda function::
        ... table3 = etl.convert(table1, 'baz', lambda v: v*2)
        >>> table3
        +-----+-------+-----+
        | foo | bar   | baz |
        +=====+=======+=====+
        | 'A' | '2.4' |  24 |
        +-----+-------+-----+
        | 'B' | '5.7' |  68 |
        +-----+-------+-----+
        | 'C' | '1.2' | 112 |
        +-----+-------+-----+

        >>> # a method of the data value can also be invoked by passing
        ... # the method name
        ... table4 = etl.convert(table1, 'foo', 'lower')
        >>> table4
        +-----+-------+-----+
        | foo | bar   | baz |
        +=====+=======+=====+
        | 'a' | '2.4' |  12 |
        +-----+-------+-----+
        | 'b' | '5.7' |  34 |
        +-----+-------+-----+
        | 'c' | '1.2' |  56 |
        +-----+-------+-----+

        >>> # arguments to the method invocation can also be given
        ... table5 = etl.convert(table1, 'foo', 'replace', 'A', 'AA')
        >>> table5
        +------+-------+-----+
        | foo  | bar   | baz |
        +======+=======+=====+
        | 'AA' | '2.4' |  12 |
        +------+-------+-----+
        | 'B'  | '5.7' |  34 |
        +------+-------+-----+
        | 'C'  | '1.2' |  56 |
        +------+-------+-----+

        >>> # values can also be translated via a dictionary
        ... table7 = etl.convert(table1, 'foo', {'A': 'Z', 'B': 'Y'})
        >>> table7
        +-----+-------+-----+
        | foo | bar   | baz |
        +=====+=======+=====+
        | 'Z' | '2.4' |  12 |
        +-----+-------+-----+
        | 'Y' | '5.7' |  34 |
        +-----+-------+-----+
        | 'C' | '1.2' |  56 |
        +-----+-------+-----+

        >>> # the same conversion can be applied to multiple fields
        ... table8 = etl.convert(table1, ('foo', 'bar', 'baz'), str)
        >>> table8
        +-----+-------+------+
        | foo | bar   | baz  |
        +=====+=======+======+
        | 'A' | '2.4' | '12' |
        +-----+-------+------+
        | 'B' | '5.7' | '34' |
        +-----+-------+------+
        | 'C' | '1.2' | '56' |
        +-----+-------+------+

        >>> # multiple conversions can be specified at the same time
        ... table9 = etl.convert(table1, {'foo': 'lower',
        ...                               'bar': float,
        ...                               'baz': lambda v: v * 2})
        >>> table9
        +-----+-----+-----+
        | foo | bar | baz |
        +=====+=====+=====+
        | 'a' | 2.4 |  24 |
        +-----+-----+-----+
        | 'b' | 5.7 |  68 |
        +-----+-----+-----+
        | 'c' | 1.2 | 112 |
        +-----+-----+-----+

        >>> # ...or alternatively via a list
        ... table10 = etl.convert(table1, ['lower', float, lambda v: v*2])
        >>> table10
        +-----+-----+-----+
        | foo | bar | baz |
        +=====+=====+=====+
        | 'a' | 2.4 |  24 |
        +-----+-----+-----+
        | 'b' | 5.7 |  68 |
        +-----+-----+-----+
        | 'c' | 1.2 | 112 |
        +-----+-----+-----+

        >>> # conversion can be conditional
        ... table11 = etl.convert(table1, 'baz', lambda v: v * 2,
        ...                       where=lambda r: r.foo == 'B')
        >>> table11
        +-----+-------+-----+
        | foo | bar   | baz |
        +=====+=======+=====+
        | 'A' | '2.4' |  12 |
        +-----+-------+-----+
        | 'B' | '5.7' |  68 |
        +-----+-------+-----+
        | 'C' | '1.2' |  56 |
        +-----+-------+-----+

        >>> # conversion can access other values from the same row
        ... table12 = etl.convert(table1, 'baz',
        ...                       lambda v, row: v * float(row.bar),
        ...                       pass_row=True)
        >>> table12
        +-----+-------+--------------------+
        | foo | bar   | baz                |
        +=====+=======+====================+
        | 'A' | '2.4' | 28.799999999999997 |
        +-----+-------+--------------------+
        | 'B' | '5.7' |              193.8 |
        +-----+-------+--------------------+
        | 'C' | '1.2' |               67.2 |
        +-----+-------+--------------------+
        >>> # conversion can use a custom function
        >>> def my_func(val, row):
        ...     return float(row.bar) + row.baz
        ... 
        >>> table13 = etl.convert(table1, 'foo', my_func, pass_row=True)
        >>> table13
        +------+-------+-----+
        | foo  | bar   | baz |
        +======+=======+=====+
        | 14.4 | '2.4' |  12 |
        +------+-------+-----+
        | 39.7 | '5.7' |  34 |
        +------+-------+-----+
        | 57.2 | '1.2' |  56 |
        +------+-------+-----+

    Note that either field names or indexes can be given.

    The ``where`` keyword argument can be given with a callable or expression
    which is evaluated on each row and which should return True if the
    conversion should be applied on that row, else False.

    The ``pass_row`` keyword argument can be given, which if True will mean
    that both the value and the containing row will be passed as
    arguments to the conversion function (so, i.e., the conversion function
    should accept two arguments).

    When multiple fields are converted in a single call, the conversions
    are independent of each other. Each conversion sees the original row::

        >>> # multiple conversions do not affect each other
        ... table13 = etl.convert(table1, {
        ...                           "foo": lambda foo, row: row.bar,
        ...                           "bar": lambda bar, row: row.foo,
        ...                       }, pass_row=True)
        >>> table13
        +-------+-----+-----+
        | foo   | bar | baz |
        +=======+=====+=====+
        | '2.4' | 'A' |  12 |
        +-------+-----+-----+
        | '5.7' | 'B' |  34 |
        +-------+-----+-----+
        | '1.2' | 'C' |  56 |
        +-------+-----+-----+

    Also accepts `failonerror` and `errorvalue` keyword arguments,
    documented under :func:`petl.config.failonerror`

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

    Nr         )lendict
isinstancelisttupleFieldConvertView)tableargskwargs
convertersfieldconvf r   S/var/www/Datamplify/venv/lib/python3.10/site-packages/petl/transform/conversions.pyconvert   s$    G


r!   c                 O   s   t | t| g|R i |S )aT  
    Convenience function to convert all fields in the table using a common
    function or mapping. See also :func:`convert`.

    The ``where`` keyword argument can be given with a callable or expression
    which is evaluated on each row and which should return True if the
    conversion should be applied on that row, else False.

    )r!   r   )r   r   r   r   r   r    
convertall   s   r"   c                 K   s   t | |||ifi |S )aO  
    Convenience function to replace all occurrences of `a` with `b` under the
    given field. See also :func:`convert`.

    The ``where`` keyword argument can be given with a callable or expression
    which is evaluated on each row and which should return True if the
    conversion should be applied on that row, else False.

    r!   )r   r   abr   r   r   r    replace   s   r&   c                 K   s   t | ||ifi |S )aK  
    Convenience function to replace all instances of `a` with `b` under all
    fields. See also :func:`convertall`.

    The ``where`` keyword argument can be given with a callable or expression
    which is evaluated on each row and which should return True if the
    conversion should be applied on that row, else False.

    r"   )r   r$   r%   r   r   r   r    
replaceall  s   r(   c                    s   t | | fddfi |S )z
    Convenience function to convert a field to a fixed value. Accepts the
    ``where`` keyword argument. See also :func:`convert`.

    c                    s    S Nr   vvaluer   r    <lambda>'  s    zupdate.<locals>.<lambda>r#   )r   r   r-   r   r   r,   r    update   s   r/   Fc                 K   s   t | t|fi |S )ao  
    Convenience function to convert all field values to numbers where
    possible. E.g.::

        >>> import petl as etl
        >>> table1 = [['foo', 'bar', 'baz', 'quux'],
        ...           ['1', '3.0', '9+3j', 'aaa'],
        ...           ['2', '1.3', '7+2j', None]]
        >>> table2 = etl.convertnumbers(table1)
        >>> table2
        +-----+-----+--------+-------+
        | foo | bar | baz    | quux  |
        +=====+=====+========+=======+
        |   1 | 3.0 | (9+3j) | 'aaa' |
        +-----+-----+--------+-------+
        |   2 | 1.3 | (7+2j) | None  |
        +-----+-----+--------+-------+

    )r"   r   )r   strictr   r   r   r    convertnumbers-  s   r1   c                   @   s*   e Zd Z		d
ddZdd Zdd	 ZdS )r   NFTc                 C   s   || _ |d u rt | _n#t|tr|| _nt|ttfr)tdd t|D | _ntd| |d u r6tj	n|| _	|| _
|| _|| _|| _d S )Nc                 S   s   g | ]\}}||fqS r   r   .0ir+   r   r   r    
<listcomp>R  s    z-FieldConvertView.__init__.<locals>.<listcomp>zunexpected converters: %r)sourcer   r   r   r   r   	enumerater	   configfailonerror
errorvaluewherepass_rowtrusted)selfr6   r   r9   r:   r;   r<   r=   r   r   r    __init__J  s   


zFieldConvertView.__init__c                 C   s"   t | j| j| j| j| j| j| jS r)   )iterfieldconvertr6   r   r9   r:   r;   r<   r=   )r>   r   r   r    __iter__\  s   zFieldConvertView.__iter__c                 C   s   || j |< d S r)   )r   )r>   keyr-   r   r   r    __setitem__`     zFieldConvertView.__setitem__)NNNNFT)__name__
__module____qualname__r?   rA   rC   r   r   r   r    r   H  s    
r   c              	   #   s   t | }zt|}ttt|t|V  W n ty$   g  }Y nw t  | D ]w\}	}
t	|	t
sHz|	}	W n tyG   t|	w t	|	tsSJ d|	 t|
r\|
 |	< q,t	|
trht|
 |	< q,t	|
ttfrt	|
d tr|
d }|
dd  }t|g|R   |	< q,t	|
trt|
 |	< q,|
d u rq,td|	|
f  fdd|rfdd}nfd	d}t	|trt||d
}n|d urt|sJ d| |s|rfdd|D }|d u r|D ]}||V  qd S |D ]}||r||V  q|V  qd S )Nzexpected integer, found %rr   r   z2unexpected converter specification on field %r: %rc              
      sl   |  vr|S z |  |g|R  W S  t y5 } zdkr%|W  Y d }~S r)|W  Y d }~S d }~ww )Ninline)	Exception)r4   r+   r   e)converter_functionsr:   r9   r   r    transform_value  s   z)iterfieldconvert.<locals>.transform_valuec                    s   t  fddt D S )Nc                 3   s     | ]\}}|| V  qd S r)   r   r2   )_rowrL   r   r    	<genexpr>  s    :iterfieldconvert.<locals>.transform_row.<locals>.<genexpr>r   r7   rM   rL   rQ   r    transform_row  s   z'iterfieldconvert.<locals>.transform_rowc                    s   t  fddt| D S )Nc                 3   s    | ]
\}} ||V  qd S r)   r   r2   rR   r   r    rN     s    rO   rP   rQ   rR   r   r    rS     s   )r=   z0expected callable for "where" argument, found %rc                 3   s    | ]}t | V  qd S r)   )r   )r3   row)fldsr   r    rN     s    z#iterfieldconvert.<locals>.<genexpr>)iterr   r   mapr   r   StopIterationr   itemsr   r   index
ValueErrorr
   intcallabler   methodcallerdictconverterr	   r   )r6   r   r9   r:   r;   r<   r=   ithdrkcmethnmmethargsrS   rT   r   )rK   r:   r9   rU   rL   r    r@   d  sl   






r@   c                    s    fddS )Nc                    s   t |   S r)   )getattrr*   r   nmr   r    r.     s    zmethodcaller.<locals>.<lambda>r   )rh   r   r   rg   r    r^     rD   r^   c                    s    fdd}|S )Nc                    s0   z|  v r
 |  W S | W S  t y   |  Y S w r)   )	TypeErrorr*   dr   r    r     s   
zdictconverter.<locals>.convr   )rk   r   r   rj   r    r_     s   	r_   c                         fdd}t | ||fi |S )a<  
    Convenience function to format all values in the given `field` using the
    `fmt` format string.

    The ``where`` keyword argument can be given with a callable or expression
    which is evaluated on each row and which should return True if the
    conversion should be applied on that row, else False.

    c                    
     | S r)   formatr*   fmtr   r    r.        
 zformat.<locals>.<lambda>r#   r   r   rq   r   r   r   rp   r    ro        ro   c                        fdd}t | |fi |S )a5  
    Convenience function to format all values in all fields using the
    `fmt` format string.

    The ``where`` keyword argument can be given with a callable or expression
    which is evaluated on each row and which should return True if the
    conversion should be applied on that row, else False.

    c                    rm   r)   rn   r*   rp   r   r    r.     rr   zformatall.<locals>.<lambda>r'   r   rq   r   r   r   rp   r    	formatall     rw   c                    rl   )a:  
    Convenience function to interpolate all values in the given `field` using
    the `fmt` string.

    The ``where`` keyword argument can be given with a callable or expression
    which is evaluated on each row and which should return True if the
    conversion should be applied on that row, else False.

    c                        |  S r)   r   r*   rp   r   r    r.   
      zinterpolate.<locals>.<lambda>r#   rs   r   rp   r    interpolate  rt   r{   c                    ru   )a3  
    Convenience function to interpolate all values in all fields using
    the `fmt` string.

    The ``where`` keyword argument can be given with a callable or expression
    which is evaluated on each row and which should return True if the
    conversion should be applied on that row, else False.

    c                    ry   r)   r   r*   rp   r   r    r.     rz   z interpolateall.<locals>.<lambda>r'   rv   r   rp   r    interpolateall  rx   r|   )F)#
__future__r   r   r   petl.compatr   r   r   r   petl.configr8   petl.errorsr	   r
   petl.util.baser   r   r   r   petl.util.parsersr   r!   r"   r&   r(   r/   r1   r   r@   r^   r_   ro   rw   r{   r|   r   r   r   r    <module>   s>     ]

f
