o
    ;Dih                     @   sp   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dZee_G dd deZd	d
 Zdd ZdS )    )absolute_importprint_functiondivisionN)	text_type)Table	asindicesRecordc                 C   s   t | ||dS )a  
    Validate a `table` against a set of `constraints` and/or an expected
    `header`, e.g.::

        >>> import petl as etl
        >>> # define some validation constraints
        ... header = ('foo', 'bar', 'baz')
        >>> constraints = [
        ...     dict(name='foo_int', field='foo', test=int),
        ...     dict(name='bar_date', field='bar', test=etl.dateparser('%Y-%m-%d')),
        ...     dict(name='baz_enum', field='baz', assertion=lambda v: v in ['Y', 'N']),
        ...     dict(name='not_none', assertion=lambda row: None not in row),
        ...     dict(name='qux_int', field='qux', test=int, optional=True),
        ... ]
        >>> # now validate a table
        ... table = (('foo', 'bar', 'bazzz'),
        ...          (1, '2000-01-01', 'Y'),
        ...          ('x', '2010-10-10', 'N'),
        ...          (2, '2000/01/01', 'Y'),
        ...          (3, '2015-12-12', 'x'),
        ...          (4, None, 'N'),
        ...          ('y', '1999-99-99', 'z'),
        ...          (6, '2000-01-01'),
        ...          (7, '2001-02-02', 'N', True))
        >>> problems = etl.validate(table, constraints=constraints, header=header)
        >>> problems.lookall()
        +--------------+-----+-------+--------------+------------------+
        | name         | row | field | value        | error            |
        +==============+=====+=======+==============+==================+
        | '__header__' |   0 | None  | None         | 'AssertionError' |
        +--------------+-----+-------+--------------+------------------+
        | 'foo_int'    |   2 | 'foo' | 'x'          | 'ValueError'     |
        +--------------+-----+-------+--------------+------------------+
        | 'bar_date'   |   3 | 'bar' | '2000/01/01' | 'ValueError'     |
        +--------------+-----+-------+--------------+------------------+
        | 'baz_enum'   |   4 | 'baz' | 'x'          | 'AssertionError' |
        +--------------+-----+-------+--------------+------------------+
        | 'bar_date'   |   5 | 'bar' | None         | 'AttributeError' |
        +--------------+-----+-------+--------------+------------------+
        | 'not_none'   |   5 | None  | None         | 'AssertionError' |
        +--------------+-----+-------+--------------+------------------+
        | 'foo_int'    |   6 | 'foo' | 'y'          | 'ValueError'     |
        +--------------+-----+-------+--------------+------------------+
        | 'bar_date'   |   6 | 'bar' | '1999-99-99' | 'ValueError'     |
        +--------------+-----+-------+--------------+------------------+
        | 'baz_enum'   |   6 | 'baz' | 'z'          | 'AssertionError' |
        +--------------+-----+-------+--------------+------------------+
        | '__len__'    |   7 | None  |            2 | 'AssertionError' |
        +--------------+-----+-------+--------------+------------------+
        | 'baz_enum'   |   7 | 'baz' | None         | 'AssertionError' |
        +--------------+-----+-------+--------------+------------------+
        | '__len__'    |   8 | None  |            4 | 'AssertionError' |
        +--------------+-----+-------+--------------+------------------+

    Returns a table of validation problems.

    )constraintsheader)ProblemsViewtabler	   r
    r   R/var/www/Datamplify/venv/lib/python3.10/site-packages/petl/transform/validation.pyvalidate   s   ;r   c                   @   s   e Zd Zdd Zdd ZdS )r   c                 C   s   || _ || _|| _d S Nr   )selfr   r	   r
   r   r   r   __init__O   s   
zProblemsView.__init__c                 C   s   t | j| j| jS r   )iterproblemsr   r	   r
   )r   r   r   r   __iter__T   s   zProblemsView.__iter__N)__name__
__module____qualname__r   r   r   r   r   r   r   M   s    r   c                    s,   | pg }dd |D } fdd|D }|S )a  
    This method renders local constraints such that return value is:
      * a list, not None
      * a list of dicts
      * a list of non-optional constraints or optional with defined field

    .. note:: We use a new variable 'local_constraints' because the constraints
              parameter may be a mutable collection, and we do not wish to
              cause side-effects by modifying it locally
    c                 S   s   g | ]	}t d i |qS )r   )dict.0cr   r   r   
<listcomp>d   s    z)normalize_constraints.<locals>.<listcomp>c                    s(   g | ]}| d  v s| ds|qS )fieldoptional)getr   fldsr   r   r   e   s    r   )r	   r"   local_constraintsr   r!   r   normalize_constraintsX   s   
r$   c                 c   s   d}|V  t | }zt|}W n ty   g }Y nw |d u r(ttt|}n6ttt|}ttt|}z||ks=J W n ty[ }	 zddd d t|	jfV  W Y d }	~	nd }	~	ww |}t	||}
|
D ]}d|vrd|v rt
||d }tj| }||d< qet|}t|D ]\}}t|}d }zt|}||ksJ W n ty }	 zd|d d |t|	jfV  W Y d }	~	nd }	~	ww t||}|
D ]}|dd }|dd }|d	d }|d
d }|ddd }z||}W n  ty }	 z||d |d t|	jfV  W Y d }	~	qd }	~	ww |r|nd }|d urAz|| W n  ty@ }	 z||d ||t|	jfV  W Y d }	~	nd }	~	ww |d urpz	||sNJ W q tyo }	 z||d ||t|	jfV  W Y d }	~	qd }	~	ww qqd S )N)namerowr   valueerror
__header__r   getterr   __len__   r%   	assertiontestc                 S   s   | S r   r   )xr   r   r   <lambda>   s    ziterproblems.<locals>.<lambda>)iternextStopIterationlistmapr   	Exceptiontyper   r$   r   operator
itemgetterlen	enumeratetupler   r    )r   r	   expected_headerouthdritactual_headerr"   expected_fldsactual_fldser#   
constraintindicesr*   expected_lenir&   lr%   r   r-   r.   targetr'   r   r   r   r   m   s   "

&
&
&
&r   )NN)
__future__r   r   r   r8   petl.compatr   petl.util.baser   r   r   r   r   r$   r   r   r   r   r   <module>   s   
>