o
    ;Di5                     @   s0  d dl mZmZm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mZmZmZ d dlmZ d&d	d
Zee_G dd deZdd Zdd Zdd Zd'ddZee_G dd deZdd Zd'ddZee_G dd deZdd Z 		d(d d!Z!e!e_!G d"d# d#eZ"d$d% Z#dS ))    )absolute_importprint_functiondivisionN)OrderedDict)nextstring_types	text_type)ArgumentError)Tableexpr
rowgroupbyRecord)sortTc                 C   s   t | ||||dS )a	  
    Transform a table, mapping fields arbitrarily between input and output.
    E.g.::

        >>> import petl as etl
        >>> from collections import OrderedDict
        >>> table1 = [['id', 'sex', 'age', 'height', 'weight'],
        ...           [1, 'male', 16, 1.45, 62.0],
        ...           [2, 'female', 19, 1.34, 55.4],
        ...           [3, 'female', 17, 1.78, 74.4],
        ...           [4, 'male', 21, 1.33, 45.2],
        ...           [5, '-', 25, 1.65, 51.9]]
        >>> mappings = OrderedDict()
        >>> # rename a field
        ... mappings['subject_id'] = 'id'
        >>> # translate a field
        ... mappings['gender'] = 'sex', {'male': 'M', 'female': 'F'}
        >>> # apply a calculation to a field
        ... mappings['age_months'] = 'age', lambda v: v * 12
        >>> # apply a calculation to a combination of fields
        ... mappings['bmi'] = lambda rec: rec['weight'] / rec['height']**2
        >>> # transform and inspect the output
        ... table2 = etl.fieldmap(table1, mappings)
        >>> table2
        +------------+--------+------------+--------------------+
        | subject_id | gender | age_months | bmi                |
        +============+========+============+====================+
        |          1 | 'M'    |        192 |  29.48870392390012 |
        +------------+--------+------------+--------------------+
        |          2 | 'F'    |        228 |   30.8531967030519 |
        +------------+--------+------------+--------------------+
        |          3 | 'F'    |        204 | 23.481883600555488 |
        +------------+--------+------------+--------------------+
        |          4 | 'M'    |        252 |  25.55260331279326 |
        +------------+--------+------------+--------------------+
        |          5 | '-'    |        300 |   19.0633608815427 |
        +------------+--------+------------+--------------------+

    Note also that the mapping value can be an expression string, which will be
    converted to a lambda function via :func:`petl.util.base.expr`.

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

    The `failonerror` and `errorvalue` keyword arguments are documented
    under :func:`petl.config.failonerror`
    )mappingsfailonerror
errorvaluetrusted)FieldMapView)tabler   r   r   r    r   L/var/www/Datamplify/venv/lib/python3.10/site-packages/petl/transform/maps.pyfieldmap   s   1r   c                   @   s*   e Zd Z		d	ddZdd Zdd ZdS )
r   NTc                 C   sB   || _ |d u rt | _n|| _|d u rtjn|| _|| _|| _d S N)sourcer   r   configr   r   r   )selfr   r   r   r   r   r   r   r   __init__I   s   

zFieldMapView.__init__c                 C   s   || j |< d S r   )r   )r   keyvaluer   r   r   __setitem__U   s   zFieldMapView.__setitem__c                 C   s   t | j| j| j| j| jS r   )iterfieldmapr   r   r   r   r   r   r   r   r   __iter__X   s   zFieldMapView.__iter__NNNT)__name__
__module____qualname__r   r   r"   r   r   r   r   r   G   s    
r   c                 #   s   t | }zt|}W n
 ty   Y d S w ttt| | }t|V  t }|	 D ]s\}	}
|
|v r=t
|
||	< q-t|
trP|
t|k rPt
|
||	< q-t|
tr^t|
|d||	< q-t|
rg|
||	< q-t|
ttfrt|
dkr|
d }|
d }t|rt||||	< q-t|trt||||	< q-tdtd|	|
f  fdd|D }|D ]<}t }|D ]/}	z||	 |}W n ty } z|d	kr|}n|r||}W Y d }~nd }~ww || qt|V  qd S )
N)r      r      zexpected callable or dictzinvalid mapping %r: %rc                 3       | ]}t | V  qd S r   r   .0rowfldsr   r   	<genexpr>~       ziterfieldmap.<locals>.<genexpr>inline)iterr   StopIterationlistmapr   keystupledictitemsoperator
itemgetter
isinstanceintlenr   r   callable
composefuncomposedictr	   	Exceptionappend)r   r   r   r   r   ithdrouthdrmapfunsoutfldmsrcfldfmr-   outrowvaler   r.   r   r    ]   s\   



r    c                        fdd}|S )Nc                    s    |  S r   r   )recfrK   r   r   g   s   zcomposefun.<locals>.gr   )rS   rK   rT   r   rR   r   rA      s   rA   c                    rP   )Nc                    s   |  }| v r | S |S r   r   )rQ   kdrK   r   r   rT      s   zcomposedict.<locals>.gr   )rW   rK   rT   r   rV   r   rB      s   rB   c                 C      t | |||dS )a  
    Transform rows via an arbitrary function. E.g.::

        >>> import petl as etl
        >>> table1 = [['id', 'sex', 'age', 'height', 'weight'],
        ...           [1, 'male', 16, 1.45, 62.0],
        ...           [2, 'female', 19, 1.34, 55.4],
        ...           [3, 'female', 17, 1.78, 74.4],
        ...           [4, 'male', 21, 1.33, 45.2],
        ...           [5, '-', 25, 1.65, 51.9]]
        >>> def rowmapper(row):
        ...     transmf = {'male': 'M', 'female': 'F'}
        ...     return [row[0],
        ...             transmf[row['sex']] if row['sex'] in transmf else None,
        ...             row.age * 12,
        ...             row.height / row.weight ** 2]
        ...
        >>> table2 = etl.rowmap(table1, rowmapper,
        ...                     header=['subject_id', 'gender', 'age_months',
        ...                             'bmi'])
        >>> table2
        +------------+--------+------------+-----------------------+
        | subject_id | gender | age_months | bmi                   |
        +============+========+============+=======================+
        |          1 | 'M'    |        192 | 0.0003772112382934443 |
        +------------+--------+------------+-----------------------+
        |          2 | 'F'    |        228 | 0.0004366015456998006 |
        +------------+--------+------------+-----------------------+
        |          3 | 'F'    |        204 | 0.0003215689675106949 |
        +------------+--------+------------+-----------------------+
        |          4 | 'M'    |        252 | 0.0006509906805544679 |
        +------------+--------+------------+-----------------------+
        |          5 | None   |        300 | 0.0006125608384287258 |
        +------------+--------+------------+-----------------------+

    The `rowmapper` function should accept a single row and return a single
    row (list or tuple).

    The `failonerror` keyword argument is documented under
    :func:`petl.config.failonerror`
    r   )
RowMapView)r   	rowmapperheaderr   r   r   r   rowmap   s   +r]   c                   @      e Zd ZdddZdd ZdS )rZ   Nc                 C   0   || _ || _|| _|d u rtj| _d S || _d S r   )r   r[   r\   r   r   )r   r   r[   r\   r   r   r   r   r         zRowMapView.__init__c                 C      t | j| j| j| jS r   )
iterrowmapr   r[   r\   r   r!   r   r   r   r"         zRowMapView.__iter__r   r$   r%   r&   r   r"   r   r   r   r   rZ          
rZ   c           	      #   s    t | }zt|}W n
 ty   Y d S w ttt| t|V   fdd|D }|D ].}z||}t|V  W q- ty[ } z|dkrMt|gV  n|rQ|W Y d }~q-d }~ww d S )Nc                 3   r)   r   r*   r+   r.   r   r   r0      r1   ziterrowmap.<locals>.<genexpr>r2   r3   r   r4   r5   r6   r   r8   rC   )	r   r[   r\   r   rE   rF   r-   rM   rO   r   r.   r   rb      s,   
rb   c                 C   rX   )a
  
    Map each input row to any number of output rows via an arbitrary
    function. E.g.::

        >>> import petl as etl
        >>> table1 = [['id', 'sex', 'age', 'height', 'weight'],
        ...           [1, 'male', 16, 1.45, 62.0],
        ...           [2, 'female', 19, 1.34, 55.4],
        ...           [3, '-', 17, 1.78, 74.4],
        ...           [4, 'male', 21, 1.33]]
        >>> def rowgenerator(row):
        ...     transmf = {'male': 'M', 'female': 'F'}
        ...     yield [row[0], 'gender',
        ...            transmf[row['sex']] if row['sex'] in transmf else None]
        ...     yield [row[0], 'age_months', row.age * 12]
        ...     yield [row[0], 'bmi', row.height / row.weight ** 2]
        ...
        >>> table2 = etl.rowmapmany(table1, rowgenerator,
        ...                         header=['subject_id', 'variable', 'value'])
        >>> table2.lookall()
        +------------+--------------+-----------------------+
        | subject_id | variable     | value                 |
        +============+==============+=======================+
        |          1 | 'gender'     | 'M'                   |
        +------------+--------------+-----------------------+
        |          1 | 'age_months' |                   192 |
        +------------+--------------+-----------------------+
        |          1 | 'bmi'        | 0.0003772112382934443 |
        +------------+--------------+-----------------------+
        |          2 | 'gender'     | 'F'                   |
        +------------+--------------+-----------------------+
        |          2 | 'age_months' |                   228 |
        +------------+--------------+-----------------------+
        |          2 | 'bmi'        | 0.0004366015456998006 |
        +------------+--------------+-----------------------+
        |          3 | 'gender'     | None                  |
        +------------+--------------+-----------------------+
        |          3 | 'age_months' |                   204 |
        +------------+--------------+-----------------------+
        |          3 | 'bmi'        | 0.0003215689675106949 |
        +------------+--------------+-----------------------+
        |          4 | 'gender'     | 'M'                   |
        +------------+--------------+-----------------------+
        |          4 | 'age_months' |                   252 |
        +------------+--------------+-----------------------+

    The `rowgenerator` function should accept a single row and yield zero or
    more rows (lists or tuples).

    The `failonerror` keyword argument is documented under
    :func:`petl.config.failonerror`

    See also the :func:`petl.transform.reshape.melt` function.

    rY   )RowMapManyView)r   rowgeneratorr\   r   r   r   r   
rowmapmany   s   9ri   c                   @   r^   )rg   Nc                 C   r_   r   )r   rh   r\   r   r   )r   r   rh   r\   r   r   r   r   r   3  r`   zRowMapManyView.__init__c                 C   ra   r   )iterrowmapmanyr   rh   r\   r   r!   r   r   r   r"   :  rc   zRowMapManyView.__iter__r   rd   r   r   r   r   rg   1  re   rg   c           	      #   s    t | }zt|}W n
 ty   Y d S w ttt| t|V   fdd|D }|D ]2}z||D ]}t|V  q4W q- ty_ } z|dkrPt|gV  n|rT|	 W Y d }~q-d }~ww d S )Nc                 3   r)   r   r*   r+   r.   r   r   r0   G  r1   z!iterrowmapmany.<locals>.<genexpr>r2   rf   )	r   rh   r\   r   rE   rF   r-   rM   rO   r   r.   r   rj   ?  s0   
rj   Fc              
   C   s   t | |||||||dS )z
    Group rows under the given key then apply `mapper` to yield zero or more
    output rows for each input group of rows.

    )r\   	presorted
buffersizetempdircache)RowGroupMapView)r   r   mapperr\   rk   rl   rm   rn   r   r   r   rowgroupmapU  s   
rq   c                   @   s"   e Zd Z		dddZdd ZdS )	ro   NFTc	           	      C   s6   |r|| _ n
t|||||d| _ || _|| _|| _d S )N)rl   rm   rn   )r   r   r   r\   rp   )	r   r   r   rp   r\   rk   rl   rm   rn   r   r   r   r   g  s   
zRowGroupMapView.__init__c                 C   ra   r   )iterrowgroupmapr   r   rp   r\   r!   r   r   r   r"   r  s   zRowGroupMapView.__iter__NFNNTrd   r   r   r   r   ro   e  s
    
ro   c                 c   s:    t |V  t| |D ]\}}|||D ]}|V  qqd S r   )r8   r   )r   r   rp   r\   rowsr-   r   r   r   rr   v  s   
rr   r#   r   rs   )$
__future__r   r   r   r;   collectionsr   petl.compatr   r   r   petl.configr   petl.errorsr	   petl.util.baser
   r   r   r   petl.transform.sortsr   r   r   r    rA   rB   r]   rZ   rb   ri   rg   rj   rq   ro   rr   r   r   r   r   <module>   s8    
52

.
<
