o
    ;Di                     @   s  d dl mZmZmZ d dl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lmZmZmZmZ d dlZeeZejZejZejZdd	 Zee_G d
d deZdaddZdd Zee_G dd deZ daddZ!dd Z"e"e_"G dd deZ#dd Z$dd Z%e%e_%G dd deZ&dd Z'dbd d!Z(e(e_(G d"d# d#eZ)d$d% Z*dad&d'Z+e+e_+G d(d) d)eZ,d*d+ Z-d,d- Z.e.e_.G d.d/ d/eZ/d0d1 Z0dcd3d4Z1e1e_1dcd5d6Z2e2e_2G d7d8 d8eZ3d9d: Z4d;d< Z5e5e_5G d=d> d>eZ6d?d@ Z7dAdB Z8e8e_8G dCdD dDeZ9dEdF Z:e:e_:G dGdH dHeZ;dIdJ Z<dddMdNZ=e=e_=G dOdP dPeZ>dQdR Z?dedSdTZ@e@e_@G dUdV dVeZAdWdX ZBG dYdZ dZeCZDd[d\ ZEeEe_EG d]d^ d^eZFd_d` ZGdS )f    )absolute_importprint_functiondivision)islicechain)deque)count)izipizip_longestnextstring_types	text_type)	asindices	rowgetterRecordTableNc                 O   s8   t |dkrt|d ttfr|d }t| |fi |S )a,  
    Choose and/or re-order fields. E.g.::

        >>> import petl as etl
        >>> table1 = [['foo', 'bar', 'baz'],
        ...           ['A', 1, 2.7],
        ...           ['B', 2, 3.4],
        ...           ['B', 3, 7.8],
        ...           ['D', 42, 9.0],
        ...           ['E', 12]]
        >>> table2 = etl.cut(table1, 'foo', 'baz')
        >>> table2
        +-----+------+
        | foo | baz  |
        +=====+======+
        | 'A' |  2.7 |
        +-----+------+
        | 'B' |  3.4 |
        +-----+------+
        | 'B' |  7.8 |
        +-----+------+
        | 'D' |  9.0 |
        +-----+------+
        | 'E' | None |
        +-----+------+

        >>> # fields can also be specified by index, starting from zero
        ... table3 = etl.cut(table1, 0, 2)
        >>> table3
        +-----+------+
        | foo | baz  |
        +=====+======+
        | 'A' |  2.7 |
        +-----+------+
        | 'B' |  3.4 |
        +-----+------+
        | 'B' |  7.8 |
        +-----+------+
        | 'D' |  9.0 |
        +-----+------+
        | 'E' | None |
        +-----+------+

        >>> # field names and indices can be mixed
        ... table4 = etl.cut(table1, 'bar', 0)
        >>> table4
        +-----+-----+
        | bar | foo |
        +=====+=====+
        |   1 | 'A' |
        +-----+-----+
        |   2 | 'B' |
        +-----+-----+
        |   3 | 'B' |
        +-----+-----+
        |  42 | 'D' |
        +-----+-----+
        |  12 | 'E' |
        +-----+-----+

        >>> # select a range of fields
        ... table5 = etl.cut(table1, *range(0, 2))
        >>> table5
        +-----+-----+
        | foo | bar |
        +=====+=====+
        | 'A' |   1 |
        +-----+-----+
        | 'B' |   2 |
        +-----+-----+
        | 'B' |   3 |
        +-----+-----+
        | 'D' |  42 |
        +-----+-----+
        | 'E' |  12 |
        +-----+-----+

    Note that any short rows will be padded with `None` values (or whatever is
    provided via the `missing` keyword argument).

    See also :func:`petl.transform.basics.cutout`.

       r   )len
isinstancelisttupleCutViewtableargskwargs r   N/var/www/Datamplify/venv/lib/python3.10/site-packages/petl/transform/basics.pycut   s   Vr   c                   @      e Zd ZdddZdd ZdS )r   Nc                 C      || _ || _|| _d S Nsourcespecmissingselfr#   r$   r%   r   r   r   __init__w      
zCutView.__init__c                 C      t | j| j| jS r!   )itercutr#   r$   r%   r'   r   r   r   __iter__|      zCutView.__iter__r!   __name__
__module____qualname__r(   r-   r   r   r   r   r   u       
r   c              	   #   s    t | }t|}zt|}W n ty   g }Y nw t||}t| }||V  |D ]z|V  W q+ tyJ   t fdd|D V  Y q+w d S )Nc                 3   (    | ]}|t k r| n V  qd S r!   r   .0ir%   rowr   r   	<genexpr>      & zitercut.<locals>.<genexpr>)iterr   r   StopIterationr   r   
IndexErrorr#   r$   r%   ithdrindices	transformr   r9   r   r+      s$   

r+   c                 O   s   t | |fi |S )a  
    Remove fields. E.g.::

        >>> import petl as etl
        >>> table1 = [['foo', 'bar', 'baz'],
        ...           ['A', 1, 2.7],
        ...           ['B', 2, 3.4],
        ...           ['B', 3, 7.8],
        ...           ['D', 42, 9.0],
        ...           ['E', 12]]
        >>> table2 = etl.cutout(table1, 'bar')
        >>> table2
        +-----+------+
        | foo | baz  |
        +=====+======+
        | 'A' |  2.7 |
        +-----+------+
        | 'B' |  3.4 |
        +-----+------+
        | 'B' |  7.8 |
        +-----+------+
        | 'D' |  9.0 |
        +-----+------+
        | 'E' | None |
        +-----+------+

    See also :func:`petl.transform.basics.cut`.

    )
CutOutViewr   r   r   r   cutout   s   rF   c                   @   r   )rE   Nc                 C   r    r!   r"   r&   r   r   r   r(      r)   zCutOutView.__init__c                 C   r*   r!   )
itercutoutr#   r$   r%   r,   r   r   r   r-      r.   zCutOutView.__iter__r!   r/   r   r   r   r   rE      r3   rE   c              	   #   s    t | }t|}zt|}W n ty   g }Y nw t||  fddtt|D }t| }||V  |D ]z|V  W q8 tyW   tfdd|D V  Y q8w d S )Nc                    s   g | ]}| vr|qS r   r   r6   )
indicesoutr   r   
<listcomp>   s    zitercutout.<locals>.<listcomp>c                 3   r4   r!   r5   r6   r9   r   r   r;      r<   zitercutout.<locals>.<genexpr>)	r=   r   r   r>   r   ranger   r   r?   r@   r   )rH   r%   r:   r   rG      s&   

rG   c                  O      t | fi |S )a?  
    Concatenate tables. E.g.::

        >>> import petl as etl
        >>> table1 = [['foo', 'bar'],
        ...           [1, 'A'],
        ...           [2, 'B']]
        >>> table2 = [['bar', 'baz'],
        ...           ['C', True],
        ...           ['D', False]]
        >>> table3 = etl.cat(table1, table2)
        >>> table3
        +------+-----+-------+
        | foo  | bar | baz   |
        +======+=====+=======+
        |    1 | 'A' | None  |
        +------+-----+-------+
        |    2 | 'B' | None  |
        +------+-----+-------+
        | None | 'C' | True  |
        +------+-----+-------+
        | None | 'D' | False |
        +------+-----+-------+

        >>> # can also be used to square up a single table with uneven rows
        ... table4 = [['foo', 'bar', 'baz'],
        ...           ['A', 1, 2],
        ...           ['B', '2', '3.4'],
        ...           [u'B', u'3', u'7.8', True],
        ...           ['D', 'xyz', 9.0],
        ...           ['E', None]]
        >>> table5 = etl.cat(table4)
        >>> table5
        +-----+-------+-------+
        | foo | bar   | baz   |
        +=====+=======+=======+
        | 'A' |     1 |     2 |
        +-----+-------+-------+
        | 'B' | '2'   | '3.4' |
        +-----+-------+-------+
        | 'B' | '3'   | '7.8' |
        +-----+-------+-------+
        | 'D' | 'xyz' |   9.0 |
        +-----+-------+-------+
        | 'E' | None  | None  |
        +-----+-------+-------+

        >>> # use the header keyword argument to specify a fixed set of fields
        ... table6 = [['bar', 'foo'],
        ...           ['A', 1],
        ...           ['B', 2]]
        >>> table7 = etl.cat(table6, header=['A', 'foo', 'B', 'bar', 'C'])
        >>> table7
        +------+-----+------+-----+------+
        | A    | foo | B    | bar | C    |
        +======+=====+======+=====+======+
        | None |   1 | None | 'A' | None |
        +------+-----+------+-----+------+
        | None |   2 | None | 'B' | None |
        +------+-----+------+-----+------+

        >>> # using the header keyword argument with two input tables
        ... table8 = [['bar', 'foo'],
        ...           ['A', 1],
        ...           ['B', 2]]
        >>> table9 = [['bar', 'baz'],
        ...           ['C', True],
        ...           ['D', False]]
        >>> table10 = etl.cat(table8, table9, header=['A', 'foo', 'B', 'bar', 'C'])
        >>> table10
        +------+------+------+-----+------+
        | A    | foo  | B    | bar | C    |
        +======+======+======+=====+======+
        | None |    1 | None | 'A' | None |
        +------+------+------+-----+------+
        | None |    2 | None | 'B' | None |
        +------+------+------+-----+------+
        | None | None | None | 'C' | None |
        +------+------+------+-----+------+
        | None | None | None | 'D' | None |
        +------+------+------+-----+------+

    Note that the tables do not need to share exactly the same fields, any
    missing fields will be padded with `None` or whatever is provided via the
    `missing` keyword argument.

    Note that this function can be used with a single table argument, in which
    case it has the effect of ensuring all data rows are the same length as
    the header row, truncating any long rows and padding any short rows with
    the value of the `missing` keyword argument.

    By default, the fields for the output table will be determined as the
    union of all fields found in the input tables. Use the `header` keyword
    argument to override this behaviour and specify a fixed set of fields for
    the output table.

    )CatViewtablesr   r   r   r   cat   s   crO   c                   @   r   )rL   Nc                 C   r    r!   )sourcesr%   header)r'   rP   r%   rQ   r   r   r   r(   R  r)   zCatView.__init__c                 C   r*   r!   )itercatrP   r%   rQ   r,   r   r   r   r-   W  r.   zCatView.__iter__NNr/   r   r   r   r   rL   P  r3   rL   c                 c   s"   dd | D }g }|D ]}z| tt| W q ty'   | g  Y qw |d u rJt|d }|dd  D ]}|D ]}||vrG| | q<q8n|}t|V  t||D ]8\}}|D ]1}	t }
|D ]$}|}z	|	|| }W n tyy   Y n	 ty   Y nw |
 | qct|
V  q\qVd S )Nc                 S      g | ]}t |qS r   r=   r7   tr   r   r   rI   \      zitercat.<locals>.<listcomp>r   r   )	appendr   r   r>   r   zipindexr?   
ValueError)rP   r%   rQ   itshdrsrA   outhdrrB   hr:   outrowvalr   r   r   rR   [  sH   

rR   c                  O   rK   )a  Concatenate tables, without trying to match headers. E.g.::

        >>> import petl as etl
        >>> table1 = [['foo', 'bar'],
        ...           [1, 'A'],
        ...           [2, 'B']]
        >>> table2 = [['bar', 'baz'],
        ...           ['C', True],
        ...           ['D', False]]
        >>> table3 = etl.stack(table1, table2)
        >>> table3
        +-----+-------+
        | foo | bar   |
        +=====+=======+
        |   1 | 'A'   |
        +-----+-------+
        |   2 | 'B'   |
        +-----+-------+
        | 'C' | True  |
        +-----+-------+
        | 'D' | False |
        +-----+-------+

        >>> # can also be used to square up a single table with uneven rows
        ... table4 = [['foo', 'bar', 'baz'],
        ...           ['A', 1, 2],
        ...           ['B', '2', '3.4'],
        ...           [u'B', u'3', u'7.8', True],
        ...           ['D', 'xyz', 9.0],
        ...           ['E', None]]
        >>> table5 = etl.stack(table4)
        >>> table5
        +-----+-------+-------+
        | foo | bar   | baz   |
        +=====+=======+=======+
        | 'A' |     1 |     2 |
        +-----+-------+-------+
        | 'B' | '2'   | '3.4' |
        +-----+-------+-------+
        | 'B' | '3'   | '7.8' |
        +-----+-------+-------+
        | 'D' | 'xyz' |   9.0 |
        +-----+-------+-------+
        | 'E' | None  | None  |
        +-----+-------+-------+

    Similar to :func:`petl.transform.basics.cat` except that no attempt is
    made to align fields from different tables. Data rows are simply emitted
    in order, trimmed or padded to the length of the header row from the
    first table.

    .. versionadded:: 1.1.0

    )	StackViewrM   r   r   r   stack  s   8rd   c                   @   s   e Zd ZdddZdd ZdS )rc   NTc                 C      || _ || _|| _|| _d S r!   )rP   r%   trimpad)r'   rP   r%   rf   rg   r   r   r   r(        
zStackView.__init__c                 C      t | j| j| j| jS r!   )	iterstackrP   r%   rf   rg   r,   r   r   r   r-        zStackView.__iter__)NTTr/   r   r   r   r   rc         
rc   c              	   c   s    dd | D }g }|D ]}z	| t| W q ty%   | g  Y qw |d }t|}t|V  |D ])}|D ]$}	t|	}
|rG|
d | }
|rZt|
|k rZ|
|f|t|
  7 }
|
V  q9q5d S )Nc                 S   rT   r   rU   rV   r   r   r   rI     rX   ziterstack.<locals>.<listcomp>r   )rY   r   r>   r   r   )rP   r%   rf   rg   r]   r^   rA   rB   nr:   ra   r   r   r   rj     s,   
rj   c                 C      t | ||||dS )a  
    Add a field with a fixed or calculated value. E.g.::

        >>> import petl as etl
        >>> table1 = [['foo', 'bar'],
        ...           ['M', 12],
        ...           ['F', 34],
        ...           ['-', 56]]
        >>> # using a fixed value
        ... table2 = etl.addfield(table1, 'baz', 42)
        >>> table2
        +-----+-----+-----+
        | foo | bar | baz |
        +=====+=====+=====+
        | 'M' |  12 |  42 |
        +-----+-----+-----+
        | 'F' |  34 |  42 |
        +-----+-----+-----+
        | '-' |  56 |  42 |
        +-----+-----+-----+

        >>> # calculating the value
        ... table2 = etl.addfield(table1, 'baz', lambda rec: rec['bar'] * 2)
        >>> table2
        +-----+-----+-----+
        | foo | bar | baz |
        +=====+=====+=====+
        | 'M' |  12 |  24 |
        +-----+-----+-----+
        | 'F' |  34 |  68 |
        +-----+-----+-----+
        | '-' |  56 | 112 |
        +-----+-----+-----+

    Use the `index` parameter to control the position of the inserted field.

    )valuer[   r%   )AddFieldView)r   fieldro   r[   r%   r   r   r   addfield  s   
'rr   c                   @   r   )rp   Nc                 C   s$   t ||d| _|| _|| _|| _d S Nr%   )rd   r#   rq   ro   r[   )r'   r#   rq   ro   r[   r%   r   r   r   r(     s   
zAddFieldView.__init__c                 C   ri   r!   )iteraddfieldr#   rq   ro   r[   r,   r   r   r   r-     rk   zAddFieldView.__iter__NNNr/   r   r   r   r   rp         
rp   c           
      #   s    t | }zt|}W n ty   g }Y nw ttt| |d u r&t|}t|}||| t|V  t	|r\ fdd|D }|D ]}t|}||}	|||	 t|V  qDd S |D ]}t|}||| t|V  q^d S )Nc                 3       | ]}t | V  qd S r!   r   r7   r:   fldsr   r   r;   2      ziteraddfield.<locals>.<genexpr>)
r=   r   r>   r   mapr   r   insertr   callable)
r#   rq   ro   r[   rA   rB   r_   r:   ra   vr   r{   r   ru     s4   
ru   c                 C   s   t | ||dS )a  
    Add fields with fixed or calculated values. E.g.::

        >>> import petl as etl
        >>> table1 = [['foo', 'bar'],
        ...           ['M', 12],
        ...           ['F', 34],
        ...           ['-', 56]]
        >>> # using a fixed value or a calculation
        ... table2 = etl.addfields(table1,
        ...                        [('baz', 42),
        ...                         ('luhrmann', lambda rec: rec['bar'] * 2)])
        >>> table2
        +-----+-----+-----+----------+
        | foo | bar | baz | luhrmann |
        +=====+=====+=====+==========+
        | 'M' |  12 |  42 |       24 |
        +-----+-----+-----+----------+
        | 'F' |  34 |  42 |       68 |
        +-----+-----+-----+----------+
        | '-' |  56 |  42 |      112 |
        +-----+-----+-----+----------+

        >>> # you can specify an index as a 3rd item in each tuple -- indicies
        ... # are evaluated in order.
        ... table2 = etl.addfields(table1,
        ...                        [('baz', 42, 0),
        ...                         ('luhrmann', lambda rec: rec['bar'] * 2, 0)])
        >>> table2
        +----------+-----+-----+-----+
        | luhrmann | baz | foo | bar |
        +==========+=====+=====+=====+
        |       24 |  42 | 'M' |  12 |
        +----------+-----+-----+-----+
        |       68 |  42 | 'F' |  34 |
        +----------+-----+-----+-----+
        |      112 |  42 | '-' |  56 |
        +----------+-----+-----+-----+

    rt   )AddFieldsView)r   
field_defsr%   r   r   r   	addfields?  s   *r   c                   @   r   )r   Nc                 C   s   t ||d| _|| _d S rs   )rd   r#   r   )r'   r#   r   r%   r   r   r   r(   q  s   
zAddFieldsView.__init__c                 C      t | j| jS r!   )iteraddfieldsr#   r   r,   r   r   r   r-   w     zAddFieldsView.__iter__r!   r/   r   r   r   r   r   o  rl   r   c                 c   s    t | }zt|}W n ty   g }Y nw ttt|}t|}g }|D ]#}t|dkr7|\}}	t|}
n|\}}	}
||
| ||	|
f q&t	|V  |D ],}t|}|D ]\}	}
t
|	rqt||}|	|}||
| qY||
|	 qYt	|V  qQd S )N   )r=   r   r>   r   r~   r   r   r   rY   r   r   r   )r#   r   rA   rB   r|   r_   value_indexesfdefnamero   r[   r:   ra   r   r   r   r   r   {  s8   



r   c                 G   s   t | g|R  S )a  
    Choose a subsequence of data rows. E.g.::

        >>> import petl as etl
        >>> table1 = [['foo', 'bar'],
        ...           ['a', 1],
        ...           ['b', 2],
        ...           ['c', 5],
        ...           ['d', 7],
        ...           ['f', 42]]
        >>> table2 = etl.rowslice(table1, 2)
        >>> table2
        +-----+-----+
        | foo | bar |
        +=====+=====+
        | 'a' |   1 |
        +-----+-----+
        | 'b' |   2 |
        +-----+-----+

        >>> table3 = etl.rowslice(table1, 1, 4)
        >>> table3
        +-----+-----+
        | foo | bar |
        +=====+=====+
        | 'b' |   2 |
        +-----+-----+
        | 'c' |   5 |
        +-----+-----+
        | 'd' |   7 |
        +-----+-----+

        >>> table4 = etl.rowslice(table1, 0, 5, 2)
        >>> table4
        +-----+-----+
        | foo | bar |
        +=====+=====+
        | 'a' |   1 |
        +-----+-----+
        | 'c' |   5 |
        +-----+-----+
        | 'f' |  42 |
        +-----+-----+

    Positional arguments are used to slice the data rows. The `sliceargs` are
    passed through to :func:`itertools.islice`.

    See also :func:`petl.transform.basics.head`,
    :func:`petl.transform.basics.tail`.

    )RowSliceView)r   	sliceargsr   r   r   rowslice  s   5r   c                   @      e Zd Zdd Zdd ZdS )r   c                 G   s   || _ |s
d| _d S || _d S )Nr!   )r#   r   )r'   r#   r   r   r   r   r(     s   

zRowSliceView.__init__c                 C   r   r!   )iterrowslicer#   r   r,   r   r   r   r-     r   zRowSliceView.__iter__Nr/   r   r   r   r   r     s    r   c                 c   sV    t | }z	tt|V  W n
 ty   Y d S w t|g|R  D ]}t|V  q!d S r!   )r=   r   r   r>   r   )r#   r   rA   r:   r   r   r   r     s   r      c                 C   
   t | |S )a  
    Select the first `n` data rows. E.g.::

        >>> import petl as etl
        >>> table1 = [['foo', 'bar'],
        ...           ['a', 1],
        ...           ['b', 2],
        ...           ['c', 5],
        ...           ['d', 7],
        ...           ['f', 42],
        ...           ['f', 3],
        ...           ['h', 90]]
        >>> table2 = etl.head(table1, 4)
        >>> table2
        +-----+-----+
        | foo | bar |
        +=====+=====+
        | 'a' |   1 |
        +-----+-----+
        | 'b' |   2 |
        +-----+-----+
        | 'c' |   5 |
        +-----+-----+
        | 'd' |   7 |
        +-----+-----+

    See also :func:`petl.transform.basics.tail`,
    :func:`petl.transform.basics.rowslice`.

    )r   r   rm   r   r   r   head  s   
 r   c                 C   r   )aF  
    Select the last `n` data rows. E.g.::

        >>> import petl as etl
        >>> table1 = [['foo', 'bar'],
        ...           ['a', 1],
        ...           ['b', 2],
        ...           ['c', 5],
        ...           ['d', 7],
        ...           ['f', 42],
        ...           ['f', 3],
        ...           ['h', 90],
        ...           ['k', 12],
        ...           ['l', 77],
        ...           ['q', 2]]
        >>> table2 = etl.tail(table1, 4)
        >>> table2
        +-----+-----+
        | foo | bar |
        +=====+=====+
        | 'h' |  90 |
        +-----+-----+
        | 'k' |  12 |
        +-----+-----+
        | 'l' |  77 |
        +-----+-----+
        | 'q' |   2 |
        +-----+-----+

    See also :func:`petl.transform.basics.head`,
    :func:`petl.transform.basics.rowslice`.

    )TailViewr   r   r   r   tail  s   
#r   c                   @   r   )r   c                 C      || _ || _d S r!   )r#   rm   )r'   r#   rm   r   r   r   r(   I     
zTailView.__init__c                 C   r   r!   )itertailr#   rm   r,   r   r   r   r-   M  r   zTailView.__iter__Nr/   r   r   r   r   r   G      r   c                 c   sx    t | }z	tt|V  W n
 ty   Y d S w t }|D ]}|| t||kr/|  q|D ]}t|V  q2d S r!   )r=   r   r   r>   r   rY   r   popleft)r#   rm   rA   cacher:   r   r   r   r   Q  s    
r   c                 C   r   )a  
    Skip any row where the first value is a string and starts with
    `prefix`. E.g.::

        >>> import petl as etl
        >>> table1 = [['##aaa', 'bbb', 'ccc'],
        ...           ['##mmm',],
        ...           ['#foo', 'bar'],
        ...           ['##nnn', 1],
        ...           ['a', 1],
        ...           ['b', 2]]
        >>> table2 = etl.skipcomments(table1, '##')
        >>> table2
        +------+-----+
        | #foo | bar |
        +======+=====+
        | 'a'  |   1 |
        +------+-----+
        | 'b'  |   2 |
        +------+-----+

    Use the `prefix` parameter to determine which string to consider as
    indicating a comment.

    )SkipCommentsView)r   prefixr   r   r   skipcomments`  s   
r   c                   @   r   )r   c                 C   r   r!   r#   r   )r'   r#   r   r   r   r   r(     r   zSkipCommentsView.__init__c                 C   r   r!   )iterskipcommentsr#   r   r,   r   r   r   r-     r   zSkipCommentsView.__iter__Nr/   r   r   r   r   r     r   r   c                    s    fdd| D S )Nc                 3   s<    | ]}t |d krt|d  tr|d   s|V  qdS )r   N)r   r   r   
startswithrz   r   r   r   r;     s    z#iterskipcomments.<locals>.<genexpr>r   r   r   r   r   r     r.   r   c                 C      t | ||S )z*
    Move a field to a new position.

    )MoveFieldView)r   rq   r[   r   r   r   	movefield  s   r   c                   @   r   )r   Nc                 C   re   r!   )r   rq   r[   r%   )r'   r   rq   r[   r%   r   r   r   r(     rh   zMoveFieldView.__init__c              	   #   s    t j}zt|}W n ty   g }Y nw fdd|D }|jj t|V  tt	t
|}t||}t| }|D ] z| V  W q@ ty_   t fdd|D V  Y q@w d S )Nc                    s   g | ]	}| j kr|qS r   )rq   )r7   fr,   r   r   rI     s    z*MoveFieldView.__iter__.<locals>.<listcomp>c                 3   s*    | ]}|t  k r | njV  qd S r!   )r   r%   r6   r:   r'   r   r   r;     s    "z)MoveFieldView.__iter__.<locals>.<genexpr>)r=   r   r   r>   r   r[   rq   r   r   r~   strr   r   r?   )r'   rA   rB   r_   outfldsrC   rD   r   r   r   r-     s,   


zMoveFieldView.__iter__r!   r/   r   r   r   r   r     rl   r   c                  O   rK   )a  
    Join two or more tables by row order. E.g.::

        >>> import petl as etl
        >>> table1 = [['foo', 'bar'],
        ...           ['A', 9],
        ...           ['C', 2],
        ...           ['F', 1]]
        >>> table2 = [['foo', 'baz'],
        ...           ['B', 3],
        ...           ['D', 10]]
        >>> table3 = etl.annex(table1, table2)
        >>> table3
        +-----+-----+------+------+
        | foo | bar | foo  | baz  |
        +=====+=====+======+======+
        | 'A' |   9 | 'B'  |    3 |
        +-----+-----+------+------+
        | 'C' |   2 | 'D'  |   10 |
        +-----+-----+------+------+
        | 'F' |   1 | None | None |
        +-----+-----+------+------+

    See also :func:`petl.transform.joins.join`.

    )	AnnexViewrM   r   r   r   annex  s   r   c                   @   r   )r   Nc                 C   r   r!   )rN   r%   )r'   rN   r%   r   r   r   r(     r   zAnnexView.__init__c                 C   r   r!   )	iterannexrN   r%   r,   r   r   r   r-     r   zAnnexView.__iter__r!   r/   r   r   r   r   r     s    
r   c              	   c   s   dd | D }g }|D ]}z	| t| W q ty%   | g  Y qw tt| }|V  t| D ]M}t }t|D ]>\}}	t|| }
|	d u rT|gt||  }	n!t|	}||
k rkt|	}	|		|g|
|   n
||
kru|	d |
 }	|	|	 q<t|V  q3d S )Nc                 S   rT   r   rU   rV   r   r   r   rI     rX   ziterannex.<locals>.<listcomp>)
rY   r   r>   r   r   r
   r   	enumerater   extend)rN   r%   r]   r^   rA   r_   rowsra   r8   r:   lhlrr   r   r   r     s4   r   r   r:   c                 C   s   t | |||S )aK  
    Add a field of row numbers. E.g.::

        >>> import petl as etl
        >>> table1 = [['foo', 'bar'],
        ...           ['A', 9],
        ...           ['C', 2],
        ...           ['F', 1]]
        >>> table2 = etl.addrownumbers(table1)
        >>> table2
        +-----+-----+-----+
        | row | foo | bar |
        +=====+=====+=====+
        |   1 | 'A' |   9 |
        +-----+-----+-----+
        |   2 | 'C' |   2 |
        +-----+-----+-----+
        |   3 | 'F' |   1 |
        +-----+-----+-----+

    Parameters `start` and `step` control the numbering.

    )AddRowNumbersViewr   startsteprq   r   r   r   addrownumbers	  s   r   c                   @   s   e Zd ZdddZdd ZdS )	r   r   r:   c                 C   re   r!   r   )r'   r   r   r   rq   r   r   r   r(   *  rh   zAddRowNumbersView.__init__c                 C   ri   r!   )iteraddrownumbersr   r   r   rq   r,   r   r   r   r-   0  rk   zAddRowNumbersView.__iter__Nr   r   r:   r/   r   r   r   r   r   (  rl   r   c           
      c   s    t | }zt|}W n ty   g }Y nw |g}|| t|V  t|t||D ]\}}|g}	|	| t|	V  q,d S r!   )r=   r   r>   r   r   r	   r   )
r   r   r   rq   rA   rB   r_   r:   rm   ra   r   r   r   r   4  s   


r   c                 C   rn   )a@  
    Add a column of data to the table. E.g.::

        >>> import petl as etl
        >>> table1 = [['foo', 'bar'],
        ...           ['A', 1],
        ...           ['B', 2]]
        >>> col = [True, False]
        >>> table2 = etl.addcolumn(table1, 'baz', col)
        >>> table2
        +-----+-----+-------+
        | foo | bar | baz   |
        +=====+=====+=======+
        | 'A' |   1 | True  |
        +-----+-----+-------+
        | 'B' |   2 | False |
        +-----+-----+-------+

    Use the `index` parameter to control the position of the new column.

    )r[   r%   )AddColumnView)r   rq   colr[   r%   r   r   r   	addcolumnC  s   r   c                   @   r   )r   Nc                 C   s"   || _ || _|| _|| _|| _d S r!   )_table_field_col_index_missing)r'   r   rq   r   r[   r%   r   r   r   r(   b  s
   
zAddColumnView.__init__c                 C   s   t | j| j| j| j| jS r!   )iteraddcolumnr   r   r   r   r   r,   r   r   r   r-   i  s   zAddColumnView.__iter__rS   r/   r   r   r   r   r   `  rw   r   c                 c   s    t | }zt|}W n ty   g }Y nw |d u rt|}t|}||| t|V  t|||dD ]\}}	||krD|gt| }t|}
|
||	 t|
V  q5d S )N)	fillvalue)r=   r   r>   r   r   r   r   r
   )r   rq   r   r[   r%   rA   rB   r_   r:   rb   ra   r   r   r   r   n  s&   
r   c                   @   s   e Zd ZdS )TransformErrorN)r0   r1   r2   r   r   r   r   r     s    r   c                 C   r   )a  
    Like :func:`petl.transform.basics.addfield` but the `query` function is
    passed the previous, current and next rows, so values may be calculated
    based on data in adjacent rows. E.g.::

        >>> import petl as etl
        >>> table1 = [['foo', 'bar'],
        ...           ['A', 1],
        ...           ['B', 4],
        ...           ['C', 5],
        ...           ['D', 9]]
        >>> def upstream(prv, cur, nxt):
        ...     if prv is None:
        ...         return None
        ...     else:
        ...         return cur.bar - prv.bar
        ...
        >>> def downstream(prv, cur, nxt):
        ...     if nxt is None:
        ...         return None
        ...     else:
        ...         return nxt.bar - cur.bar
        ...
        >>> table2 = etl.addfieldusingcontext(table1, 'baz', upstream)
        >>> table3 = etl.addfieldusingcontext(table2, 'quux', downstream)
        >>> table3
        +-----+-----+------+------+
        | foo | bar | baz  | quux |
        +=====+=====+======+======+
        | 'A' |   1 | None |    3 |
        +-----+-----+------+------+
        | 'B' |   4 |    3 |    1 |
        +-----+-----+------+------+
        | 'C' |   5 |    1 |    4 |
        +-----+-----+------+------+
        | 'D' |   9 |    4 | None |
        +-----+-----+------+------+

    The `field` parameter is the name of the field to be added. The `query`
    parameter is a function operating on the current, previous and next rows
    and returning the value.

    )AddFieldUsingContextViewr   rq   queryr   r   r   addfieldusingcontext  s   -r   c                   @   r   )r   c                 C   r    r!   r   )r'   r   rq   r   r   r   r   r(     r)   z!AddFieldUsingContextView.__init__c                 C   r*   r!   )iteraddfieldusingcontextr   rq   r   r,   r   r   r   r-     r.   z!AddFieldUsingContextView.__iter__Nr/   r   r   r   r   r     s    r   c           	      #   s    t | }ztt|}W n ty   d}Y nw ttt| ||f V   |  fdd|D }d }zt|}W n
 tyF   Y d S w |D ]}||||}t||f V  tt||f  }|}qI|||d }t||f V  d S )Nr   c                 3   rx   r!   ry   rz   r{   r   r   r;     r}   z+iteraddfieldusingcontext.<locals>.<genexpr>)	r=   r   r   r>   r   r~   r   rY   r   )	r   rq   r   rA   rB   prvcurnxtr   r   r{   r   r     s0   
r   r!   rv   )r   r   rS   )H
__future__r   r   r   	itertoolsr   r   collectionsr   r   petl.compatr	   r
   r   r   r   petl.util.baser   r   r   r   logging	getLoggerr0   loggerwarninginfodebugr   r   r+   rF   rE   rG   rO   rL   rR   rd   rc   rj   rr   rp   ru   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   	Exceptionr   r   r   r   r   r   r   r   <module>   s    
\
"
f*;
+
 -+8

#
&

	$


0