o
    ;Di?                     @   sH  d dl mZmZmZ d dl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 d dlmZm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)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divisionprint_functionN)next	text_type)stack)keys_from_args)Table	asindicesiterpeek	rowgetter)lookup	lookuponeTc	           	   
   C   ,   t | ||||\}}t| |||||||dS )a/  Alternative implementation of :func:`petl.transform.joins.join`, where
    the join is executed by constructing an in-memory lookup for the right
    hand table, then iterating over rows from the left hand table.
    
    May be faster and/or more resource efficient where the right table is small
    and the left table is large.
    
    By default data from right hand table is cached to improve performance
    (only available when `key` is given).

    Left and right tables with different key fields can be handled via the
    `lkey` and `rkey` arguments.

    .. versionchanged:: 1.7.16
        To ensure correct results for tables with uneven rows, tables will be
        squared up and rows will be filled with the value if `missing` keyword
        argument before joining to ensure correct results.

    )lkeyrkeycachelprefixrprefixmissing)r   HashJoinView)	leftrightkeyr   r   r   r   r   r    r   Q/var/www/Datamplify/venv/lib/python3.10/site-packages/petl/transform/hashjoins.pyhashjoin   s   r   c                   @   s"   e Zd Z		dddZdd ZdS )r   TNc	           	      C   sD   t ||d| _t ||d| _|| _|| _|| _d | _|| _|| _d S N)r   )	r   r   r   r   r   r   rlookupr   r   )	selfr   r   r   r   r   r   r   r   r   r   r   __init__,   s   
zHashJoinView.__init__c                 C   sB   | j r| jd u rt| j| j| _t| j| j| j| j| j| j| j	S N)
r   r   r   r   r   iterhashjoinr   r   r   r   r   r   r   r   __iter__7   s
   zHashJoinView.__iter__)TNNN__name__
__module____qualname__r    r$   r   r   r   r   r   *   s
    
r   c                 #   s   t | }t |}t|}	t|}
t|	|}t|
|tj| }fddtt|
D }t|  d u r:t|	}n	 fdd|	D }d u rO|	|
 n|	fdd|
D  t
|V  fdd}|D ]}||}||v r|| }|||D ]}|V  q}qjd S )Nc                       g | ]}| vr|qS r   r   .0irkindr   r   
<listcomp>O       z iterhashjoin.<locals>.<listcomp>c                       g | ]
}t  t | qS r   r   r+   fr   r   r   r/   V       c                    r1   r   r2   r3   r   r   r   r/   [       c                 3   0    |D ]}t | }| | t|V  qd S r!   listextendtuple_lrow_rrowsrrow_outrowrgetvr   r   joinrows_      ziterhashjoin.<locals>.joinrowsiterr   r
   operator
itemgetterrangelenr   r;   r<   r=   )r   r   r   r   r   r   r   litritlhdrrhdrlkindlgetkrvindouthdrrE   lrowkrrowsoutrowr   r   rD   r.   r   r   r"   >   s8   





r"   c	           	   
   C   r   )a  Alternative implementation of :func:`petl.transform.joins.leftjoin`,
    where the join is executed by constructing an in-memory lookup for the
    right hand table, then iterating over rows from the left hand table.
    
    May be faster and/or more resource efficient where the right table is small
    and the left table is large.
    
    By default data from right hand table is cached to improve performance
    (only available when `key` is given).

    Left and right tables with different key fields can be handled via the
    `lkey` and `rkey` arguments.

    .. versionchanged:: 1.7.16
        To ensure correct results for tables with uneven rows, tables will be
        squared up before joining to ensure correct results.

    r   r   r   r   )r   HashLeftJoinView	r   r   r   r   r   r   r   r   r   r   r   r   hashleftjoino      r]   c                   @   "   e Zd Z		dddZdd ZdS )r[   NTc	           	      C   J   t ||d| _t ||d| _|| _|| _|| _|| _d | _|| _|| _	d S r   )
r   r   r   r   r   r   r   r   r   r   	r   r   r   r   r   r   r   r   r   r   r   r   r          
zHashLeftJoinView.__init__c              	   C   sF   | j r| jd u rt| j| j| _t| j| j| j| j| j| j| j	| j
S r!   )r   r   r   r   r   iterhashleftjoinr   r   r   r   r   r#   r   r   r   r$         zHashLeftJoinView.__iter__NTNNr%   r   r   r   r   r[      
    
r[   c                 #   s4   t | }t |}	t|}
t|	}t|
|}t||tj| }fddtt|D }t|  d u r:t|
}n	 fdd|
D }d u rO|	| n|	fdd|D  t
|V  fdd}|D ]-}||}||v r|| }|||D ]}|V  q}qjt|}|	|gt|  t
|V  qjd S )Nc                    r)   r   r   r*   r-   r   r   r/      r0   z$iterhashleftjoin.<locals>.<listcomp>c                    r1   r   r2   r3   r5   r   r   r/      r6   c                    r1   r   r2   r3   r7   r   r   r/      r8   c                 3   r9   r!   r:   r>   rC   r   r   rE      rF   z"iterhashleftjoin.<locals>.joinrowsrG   )r   r   r   r   r   r   r   r   rM   rN   rO   rP   rQ   rR   rS   rT   rE   rU   rV   rW   rX   r   rY   r   rc      s>   





rc   c	           	   
   C   r   )a  Alternative implementation of :func:`petl.transform.joins.rightjoin`,
    where the join is executed by constructing an in-memory lookup for the
    left hand table, then iterating over rows from the right hand table.
    
    May be faster and/or more resource efficient where the left table is small
    and the right table is large.
    
    By default data from right hand table is cached to improve performance
    (only available when `key` is given).

    Left and right tables with different key fields can be handled via the
    `lkey` and `rkey` arguments.

    .. versionchanged:: 1.7.16
        To ensure correct results for tables with uneven rows, tables will be
        squared up before joining to ensure correct results.

    rZ   )r   HashRightJoinViewr\   r   r   r   hashrightjoin   r^   rh   c                   @   r_   )rg   NTc	           	      C   r`   r   )
r   r   r   r   r   r   r   llookupr   r   ra   r   r   r   r       rb   zHashRightJoinView.__init__c              	   C   sF   | j r| jd u rt| j| j| _t| j| j| j| j| j| j| j	| j
S r!   )r   ri   r   r   r   iterhashrightjoinr   r   r   r   r   r#   r   r   r   r$     rd   zHashRightJoinView.__iter__re   r%   r   r   r   r   rg      rf   rg   c                 #   sT   t | }t |}	t|}
t|	}t|
|}t||tj }fddtt|D }t|  d u r:t|
}n	 fdd|
D }d u rO|	| n|	fdd|D  t
|V  fdd}|	D ]=}||}||v r|| }|||D ]}|V  q}qj|gt|
 }t|D ]
\}}|| ||< q|	| t
|V  qjd S )Nc                    r)   r   r   r*   r-   r   r   r/     r0   z%iterhashrightjoin.<locals>.<listcomp>c                    r1   r   r2   r3   r5   r   r   r/   %  r6   c                    r1   r   r2   r3   r7   r   r   r/   *  r6   c                 3   s0    |D ]}t |}| |  t|V  qd S r!   r:   )_rrow_lrowsrU   rB   rC   r   r   rE   /  rF   z#iterhashrightjoin.<locals>.joinrows)rH   r   r
   rI   rJ   rK   rL   r   r;   r<   r=   zip)r   r   r   r   r   ri   r   r   rM   rN   rO   rP   rQ   rgetkrS   rT   rE   rA   rV   lrowsrX   lirir   rY   r   rj     sF   





rj   c                 C   s"   t | ||||\}}t| |||S )a  Alternative implementation of :func:`petl.transform.joins.antijoin`,
    where the join is executed by constructing an in-memory set for all keys
    found in the right hand table, then iterating over rows from the left
    hand table.
    
    May be faster and/or more resource efficient where the right table is small
    and the left table is large.
    
    Left and right tables with different key fields can be handled via the
    `lkey` and `rkey` arguments.

    )r   HashAntiJoinView)r   r   r   r   r   r   r   r   hashantijoinH  s   rs   c                   @   s   e Zd Zdd Zdd ZdS )rr   c                 C   s   || _ || _|| _|| _d S r!   )r   r   r   r   )r   r   r   r   r   r   r   r   r    _  s   
zHashAntiJoinView.__init__c                 C   s   t | j| j| j| jS r!   )iterhashantijoinr   r   r   r   r#   r   r   r   r$   e  s   zHashAntiJoinView.__iter__Nr%   r   r   r   r   rr   ]  s    rr   c                 c   s    t | }t |}t|}t|}t|V  t||}t||}	tj| }
tj|	 }t }|D ]}||}|| q/|D ]}|
|}||vrLt|V  q=d S r!   )rH   r   r=   r
   rI   rJ   setadd)r   r   r   r   rM   rN   rO   rP   rQ   r.   rR   rn   rkeysrA   rkrU   lkr   r   r   rt   i  s(   





rt   c              	   C   s*   t | ||||\}}t| ||||||dS )aj  Alternative implementation of :func:`petl.transform.joins.lookupjoin`,
    where the join is executed by constructing an in-memory lookup for the
    right hand table, then iterating over rows from the left hand table.

    May be faster and/or more resource efficient where the right table is small
    and the left table is large.

    Left and right tables with different key fields can be handled via the
    `lkey` and `rkey` arguments.

    .. versionchanged:: 1.7.16
        To ensure correct results for tables with uneven rows, tables will be
        squared up before joining to ensure correct results.

    )r   r   r   )r   HashLookupJoinView)r   r   r   r   r   r   r   r   r   r   r   hashlookupjoin  s   r{   c                   @   s"   e Zd Z		dddZdd ZdS )rz   Nc                 C   s>   t ||d| _t ||d| _|| _|| _|| _|| _|| _d S r   )r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r      s   
zHashLookupJoinView.__init__c                 C   s"   t | j| j| j| j| j| j| jS r!   )iterhashlookupjoinr   r   r   r   r   r   r   r#   r   r   r   r$     s   zHashLookupJoinView.__iter__NNNr%   r   r   r   r   rz     s
    

rz   c                 #   s4   t | }t|}t|\}	}
t|
|dd}t||}t|	|tj| }fddtt|	D }t	|  d u r?t
|}n	 fdd|D }d u rT||	 n|fdd|	D  t|V  fdd}|D ](}||}||v r|| }|||V  qot
|}||gt|  t|V  qod S )	NF)strictc                    r)   r   r   r*   r-   r   r   r/     r0   z&iterhashlookupjoin.<locals>.<listcomp>c                    r1   r   r2   r3   r5   r   r   r/     r6   c                    r1   r   r2   r3   r7   r   r   r/     r6   c                    s   t | }| | t|S r!   r:   )r?   rk   rB   rC   r   r   rE     s   z$iterhashlookupjoin.<locals>.joinrows)rH   r   r   r   r
   rI   rJ   rK   rL   r   r;   r<   r=   )r   r   r   r   r   r   r   rM   rO   rP   rN   r   rQ   rR   rS   rT   rE   rU   rV   rA   rX   r   rY   r   r|     s>   





r|   )NNNTNNN)NNNNTNNr}   )NNNNNN)#
__future__r   r   r   rI   petl.compatr   r   petl.transform.basicsr   petl.transform.joinsr   petl.util.baser	   r
   r   r   petl.util.lookupsr   r   r   r   r"   r]   r[   rc   rh   rg   rj   rs   rr   rt   r{   rz   r|   r   r   r   r   <module>   sF    
1
7

<
