o
    ;D©iø,  ã                   @   s¼   d dl mZmZmZ d dl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„ Zddd	„Zee
_ddd„Zee
_ddd„Zee
_ddd„Zee
_ddd„Zee
_ddd„Zee
_dS )é    )Úabsolute_importÚprint_functionÚdivisionN)Ú	text_type)ÚDuplicateKeyError)ÚTableÚ	asindicesÚasdictÚRecordÚ	rowgetterc           	      C   s    t | ƒ}zt|ƒ}W n ty   g }Y nw t||ƒ}t|ƒdks%J dƒ‚tj|Ž }|d u r7ttt|ƒƒŽ }nt||ƒ}t|ƒdksFJ dƒ‚tj|Ž }|||fS )Nr   úno key selectedzno value selected)	ÚiterÚnextÚStopIterationr   ÚlenÚoperatorÚ
itemgetterr   Úrange)	ÚtableÚkeyÚvalueÚitÚhdrÚ
keyindicesÚgetkeyÚgetvalueÚvalueindices© r   úJ/var/www/Datamplify/venv/lib/python3.10/site-packages/petl/util/lookups.pyÚ_setup_lookup   s   ÿ




r   c                 C   sl   |du rt ƒ }t| ||ƒ\}}}|D ]!}||ƒ}||ƒ}	||v r.|| }
|
 |	¡ |
||< q|	g||< q|S )a¾  
    Load a dictionary with data from the given table. E.g.::

        >>> import petl as etl
        >>> table1 = [['foo', 'bar'],
        ...           ['a', 1],
        ...           ['b', 2],
        ...           ['b', 3]]
        >>> lkp = etl.lookup(table1, 'foo', 'bar')
        >>> lkp['a']
        [1]
        >>> lkp['b']
        [2, 3]
        >>> # if no value argument is given, defaults to the whole
        ... # row (as a tuple)
        ... lkp = etl.lookup(table1, 'foo')
        >>> lkp['a']
        [('a', 1)]
        >>> lkp['b']
        [('b', 2), ('b', 3)]
        >>> # compound keys are supported
        ... table2 = [['foo', 'bar', 'baz'],
        ...           ['a', 1, True],
        ...           ['b', 2, False],
        ...           ['b', 3, True],
        ...           ['b', 3, False]]
        >>> lkp = etl.lookup(table2, ('foo', 'bar'), 'baz')
        >>> lkp[('a', 1)]
        [True]
        >>> lkp[('b', 2)]
        [False]
        >>> lkp[('b', 3)]
        [True, False]
        >>> # data can be loaded into an existing dictionary-like
        ... # object, including persistent dictionaries created via the
        ... # shelve module
        ... import shelve
        >>> lkp = shelve.open('example1.dat', flag='n')
        >>> lkp = etl.lookup(table1, 'foo', 'bar', lkp)
        >>> lkp.close()
        >>> lkp = shelve.open('example1.dat', flag='r')
        >>> lkp['a']
        [1]
        >>> lkp['b']
        [2, 3]

    N)Údictr   Úappend)r   r   r   Ú
dictionaryr   r   r   ÚrowÚkÚvÚlr   r   r   Úlookup&   s   1

r'   Fc                 C   sb   |du rt ƒ }t| ||ƒ\}}}|D ]}||ƒ}	|r"|	|v r"t|	ƒ‚|	|vr.||ƒ}
|
||	< q|S )až  
    Load a dictionary with data from the given table, assuming there is
    at most one value for each key. E.g.::

        >>> import petl as etl
        >>> table1 = [['foo', 'bar'],
        ...           ['a', 1],
        ...           ['b', 2],
        ...           ['b', 3]]
        >>> # if the specified key is not unique and strict=False (default),
        ... # the first value wins
        ... lkp = etl.lookupone(table1, 'foo', 'bar')
        >>> lkp['a']
        1
        >>> lkp['b']
        2
        >>> # if the specified key is not unique and strict=True, will raise
        ... # DuplicateKeyError
        ... try:
        ...     lkp = etl.lookupone(table1, 'foo', strict=True)
        ... except etl.errors.DuplicateKeyError as e:
        ...     print(e)
        ...
        duplicate key: 'b'
        >>> # compound keys are supported
        ... table2 = [['foo', 'bar', 'baz'],
        ...           ['a', 1, True],
        ...           ['b', 2, False],
        ...           ['b', 3, True],
        ...           ['b', 3, False]]
        >>> lkp = etl.lookupone(table2, ('foo', 'bar'), 'baz')
        >>> lkp[('a', 1)]
        True
        >>> lkp[('b', 2)]
        False
        >>> lkp[('b', 3)]
        True
        >>> # data can be loaded into an existing dictionary-like
        ... # object, including persistent dictionaries created via the
        ... # shelve module
        ... import shelve
        >>> lkp = shelve.open('example2.dat', flag='n')
        >>> lkp = etl.lookupone(table1, 'foo', 'bar', lkp)
        >>> lkp.close()
        >>> lkp = shelve.open('example2.dat', flag='r')
        >>> lkp['a']
        1
        >>> lkp['b']
        2

    N)r    r   r   )r   r   r   r"   Ústrictr   r   r   r#   r$   r%   r   r   r   Ú	lookuponeo   s   5€r)   c                 C   s¾   |du rt ƒ }t| ƒ}zt|ƒ}W n ty   g }Y nw ttt|ƒƒ}t||ƒ}t|ƒdks3J dƒ‚t	j
|Ž }|D ]"}||ƒ}	t||ƒ}
|	|v rW||	 }| |
¡ |||	< q:|
g||	< q:|S )aÙ  
    Load a dictionary with data from the given table, mapping to dicts. E.g.::

        >>> import petl as etl
        >>> table1 = [['foo', 'bar'],
        ...           ['a', 1],
        ...           ['b', 2],
        ...           ['b', 3]]
        >>> lkp = etl.dictlookup(table1, 'foo')
        >>> lkp['a']
        [{'foo': 'a', 'bar': 1}]
        >>> lkp['b']
        [{'foo': 'b', 'bar': 2}, {'foo': 'b', 'bar': 3}]
        >>> # compound keys are supported
        ... table2 = [['foo', 'bar', 'baz'],
        ...           ['a', 1, True],
        ...           ['b', 2, False],
        ...           ['b', 3, True],
        ...           ['b', 3, False]]
        >>> lkp = etl.dictlookup(table2, ('foo', 'bar'))
        >>> lkp[('a', 1)]
        [{'foo': 'a', 'bar': 1, 'baz': True}]
        >>> lkp[('b', 2)]
        [{'foo': 'b', 'bar': 2, 'baz': False}]
        >>> lkp[('b', 3)]
        [{'foo': 'b', 'bar': 3, 'baz': True}, {'foo': 'b', 'bar': 3, 'baz': False}]
        >>> # data can be loaded into an existing dictionary-like
        ... # object, including persistent dictionaries created via the
        ... # shelve module
        ... import shelve
        >>> lok = shelve.open('example3.dat', flag='n')
        >>> lok = etl.dictlookup(table1, 'foo', lok)
        >>> lok.close()
        >>> lup = shelve.open('example3.dat', flag='r')
        >>> lup['a']
        [{'foo': 'a', 'bar': 1}]
        >>> lup['b']
        [{'foo': 'b', 'bar': 2}, {'foo': 'b', 'bar': 3}]

    Nr   r   )r    r   r   r   ÚlistÚmapr   r   r   r   r   r	   r!   ©r   r   r"   r   r   Úfldsr   r   r#   r$   Úrecr&   r   r   r   Ú
dictlookup¹   s*   *ÿ




r/   c                 C   s´   |du rt ƒ }t| ƒ}zt|ƒ}W n ty   g }Y nw ttt|ƒƒ}t||ƒ}t|ƒdks3J dƒ‚t	j
|Ž }|D ]}	||	ƒ}
|rJ|
|v rJt|
ƒ‚|
|vrWt||	ƒ}|||
< q:|S )aZ  
    Load a dictionary with data from the given table, mapping to dicts,
    assuming there is at most one row for each key. E.g.::

        >>> import petl as etl
        >>> table1 = [['foo', 'bar'],
        ...           ['a', 1],
        ...           ['b', 2],
        ...           ['b', 3]]
        >>> # if the specified key is not unique and strict=False (default),
        ... # the first value wins
        ... lkp = etl.dictlookupone(table1, 'foo')
        >>> lkp['a']
        {'foo': 'a', 'bar': 1}
        >>> lkp['b']
        {'foo': 'b', 'bar': 2}
        >>> # if the specified key is not unique and strict=True, will raise
        ... # DuplicateKeyError
        ... try:
        ...     lkp = etl.dictlookupone(table1, 'foo', strict=True)
        ... except etl.errors.DuplicateKeyError as e:
        ...     print(e)
        ...
        duplicate key: 'b'
        >>> # compound keys are supported
        ... table2 = [['foo', 'bar', 'baz'],
        ...           ['a', 1, True],
        ...           ['b', 2, False],
        ...           ['b', 3, True],
        ...           ['b', 3, False]]
        >>> lkp = etl.dictlookupone(table2, ('foo', 'bar'))
        >>> lkp[('a', 1)]
        {'foo': 'a', 'bar': 1, 'baz': True}
        >>> lkp[('b', 2)]
        {'foo': 'b', 'bar': 2, 'baz': False}
        >>> lkp[('b', 3)]
        {'foo': 'b', 'bar': 3, 'baz': True}
        >>> # data can be loaded into an existing dictionary-like
        ... # object, including persistent dictionaries created via the
        ... # shelve module
        ... import shelve
        >>> lkp = shelve.open('example4.dat', flag='n')
        >>> lkp = etl.dictlookupone(table1, 'foo', lkp)
        >>> lkp.close()
        >>> lkp = shelve.open('example4.dat', flag='r')
        >>> lkp['a']
        {'foo': 'a', 'bar': 1}
        >>> lkp['b']
        {'foo': 'b', 'bar': 2}

    Nr   r   )r    r   r   r   r*   r+   r   r   r   r   r   r   r	   ©r   r   r"   r(   r   r   r-   r   r   r#   r$   Údr   r   r   Údictlookuponeÿ   s*   5ÿ


€r2   c                 C   s¾   |du rt ƒ }t| ƒ}zt|ƒ}W n ty   g }Y nw ttt|ƒƒ}t||ƒ}t|ƒdks3J dƒ‚t	j
|Ž }|D ]"}||ƒ}	t||ƒ}
|	|v rW||	 }| |
¡ |||	< q:|
g||	< q:|S )zW
    Load a dictionary with data from the given table, mapping to record objects.

    Nr   r   )r    r   r   r   r*   r+   r   r   r   r   r   r
   r!   r,   r   r   r   ÚrecordlookupM  s*   ÿ




r3   c                 C   s´   |du rt ƒ }t| ƒ}zt|ƒ}W n ty   g }Y nw ttt|ƒƒ}t||ƒ}t|ƒdks3J dƒ‚t	j
|Ž }|D ]}	||	ƒ}
|rJ|
|v rJt|
ƒ‚|
|vrWt|	|ƒ}|||
< q:|S )z‹
    Load a dictionary with data from the given table, mapping to record objects,
    assuming there is at most one row for each key.

    Nr   r   )r    r   r   r   r*   r+   r   r   r   r   r   r   r
   r0   r   r   r   Úrecordlookuponeo  s*   ÿ


€r4   )NN)NNF)N)NF)Ú
__future__r   r   r   r   Úpetl.compatr   Úpetl.errorsr   Úpetl.util.baser   r   r	   r
   r   r   r'   r)   r/   r2   r3   r4   r   r   r   r   Ú<module>   s$    
F
G
C
K


