B
    O7X"                 @   s   d dl Z d dlZd dlmZmZmZmZmZmZ dZ	dd Z
dd Zdd	 Zefd
dZefddZefddZefddZefddZefddZefddZdd ZefddZdefddZd!dd ZdS )"    N)mapzip	iteritemsiterkeys
itervaluesreduce)merge
merge_withvalmapkeymapitemmap	valfilter	keyfilter
itemfilterassocdissocassoc_in	update_inget_inc             C   s.   | dt}|r*td| j| d |S )Nfactoryz.{0}() got an unexpected keyword argument '{1}'r   )popdict	TypeErrorformat__name__popitem)fkwargsr    r   .lib/python3.7/site-packages/toolz/dicttoolz.py_get_factory   s
    r    c              O   sN   t | dkr"t| d ts"| d } tt|}| }x| D ]}|| q8W |S )z Merge a collection of dictionaries

    >>> merge({1: 'one'}, {2: 'two'})
    {1: 'one', 2: 'two'}

    Later dictionaries have precedence

    >>> merge({1: 2, 3: 4}, {3: 3, 4: 4})
    {1: 2, 3: 3, 4: 4}

    See Also:
        merge_with
       r   )len
isinstancer   r    r   update)dictsr   r   rvdr   r   r   r      s    

r   c             O   s   t |dkr"t|d ts"|d }tt|}| }xD|D ]<}x6t|D ]*\}}||krb|g||< qF|| | qFW q8W t| ||S )a   Merge dictionaries and apply function to combined values

    A key may occur in more than one dict, and all values mapped from the key
    will be passed to the function as a list, such as func([val1, val2, ...]).

    >>> merge_with(sum, {1: 1, 2: 2}, {1: 10, 2: 20})
    {1: 11, 2: 22}

    >>> merge_with(first, {1: 1, 2: 2}, {2: 20, 3: 30})  # doctest: +SKIP
    {1: 1, 2: 2, 3: 30}

    See Also:
        merge
    r!   r   )r"   r#   r   r    r	   r   appendr
   )funcr%   r   r   resultr'   kvr   r   r   r	   +   s    

r	   c             C   s(   | }| tt|t| t| |S )z Apply function to values of dictionary

    >>> bills = {"Alice": [20, 15, 30], "Bob": [10, 35]}
    >>> valmap(sum, bills)  # doctest: +SKIP
    {'Alice': 65, 'Bob': 45}

    See Also:
        keymap
        itemmap
    )r$   r   r   r   r   )r)   r'   r   r&   r   r   r   r
   H   s    r
   c             C   s(   | }| tt| t|t| |S )z Apply function to keys of dictionary

    >>> bills = {"Alice": [20, 15, 30], "Bob": [10, 35]}
    >>> keymap(str.lower, bills)  # doctest: +SKIP
    {'alice': [20, 15, 30], 'bob': [10, 35]}

    See Also:
        valmap
        itemmap
    )r$   r   r   r   r   )r)   r'   r   r&   r   r   r   r   X   s    r   c             C   s   | }| t| t| |S )z Apply function to items of dictionary

    >>> accountids = {"Alice": 10, "Bob": 20}
    >>> itemmap(reversed, accountids)  # doctest: +SKIP
    {10: "Alice", 20: "Bob"}

    See Also:
        keymap
        valmap
    )r$   r   r   )r)   r'   r   r&   r   r   r   r   h   s    r   c             C   s0   | }x$t |D ]\}}| |r|||< qW |S )z Filter items in dictionary by value

    >>> iseven = lambda x: x % 2 == 0
    >>> d = {1: 2, 2: 3, 3: 4, 4: 5}
    >>> valfilter(iseven, d)
    {1: 2, 3: 4}

    See Also:
        keyfilter
        itemfilter
        valmap
    )r   )	predicater'   r   r&   r+   r,   r   r   r   r   x   s
    r   c             C   s0   | }x$t |D ]\}}| |r|||< qW |S )z Filter items in dictionary by key

    >>> iseven = lambda x: x % 2 == 0
    >>> d = {1: 2, 2: 3, 3: 4, 4: 5}
    >>> keyfilter(iseven, d)
    {2: 3, 4: 5}

    See Also:
        valfilter
        itemfilter
        keymap
    )r   )r-   r'   r   r&   r+   r,   r   r   r   r      s
    r   c             C   s4   | }x(t |D ]}| |r|\}}|||< qW |S )a   Filter items in dictionary by item

    >>> def isvalid(item):
    ...     k, v = item
    ...     return k % 2 == 0 and v < 4

    >>> d = {1: 2, 2: 3, 3: 4, 4: 5}
    >>> itemfilter(isvalid, d)
    {2: 3}

    See Also:
        keyfilter
        valfilter
        itemmap
    )r   )r-   r'   r   r&   itemr+   r,   r   r   r   r      s    r   c             C   s   | }|||< t | ||dS )z Return a new dict with new key value pair

    New dict has d[key] set to value. Does not modify the initial dictionary.

    >>> assoc({'x': 1}, 'x', 2)
    {'x': 2}
    >>> assoc({'x': 1}, 'y', 3)   # doctest: +SKIP
    {'x': 1, 'y': 3}
    )r   )r   )r'   keyvaluer   d2r   r   r   r      s    
r   c             G   s*   t  | }x|D ]}||kr||= qW |S )aB   Return a new dict with the given key(s) removed.

    New dict has d[key] deleted for each supplied key.
    Does not modify the initial dictionary.

    >>> dissoc({'x': 1, 'y': 2}, 'y')
    {'x': 1}
    >>> dissoc({'x': 1, 'y': 2}, 'y', 'x')
    {}
    >>> dissoc({'x': 1}, 'y') # Ignores missing keys
    {'x': 1}
    )copy)r'   keysr1   r/   r   r   r   r      s
    


r   c                s   t | | fdd |S )a   Return a new dict with new, potentially nested, key value pair

    >>> purchase = {'name': 'Alice',
    ...             'order': {'items': ['Apple', 'Orange'],
    ...                       'costs': [0.50, 1.25]},
    ...             'credit card': '5555-1234-1234-1234'}
    >>> assoc_in(purchase, ['order', 'costs'], [0.25, 1.00]) # doctest: +SKIP
    {'credit card': '5555-1234-1234-1234',
     'name': 'Alice',
     'order': {'costs': [0.25, 1.00], 'items': ['Apple', 'Orange']}}
    c                s    S )Nr   )x)r0   r   r   <lambda>   s    zassoc_in.<locals>.<lambda>)r   )r'   r3   r0   r   r   )r0   r   r      s    r   c          	   C   s   t |dkst|d |dd  }}|rVt| |t|| krB| | n| |||||S || krj|| | n||}t| |||S dS )a	   Update value in a (potentially) nested dictionary

    inputs:
    d - dictionary on which to operate
    keys - list or tuple giving the location of the value to be changed in d
    func - function to operate on that value

    If keys == [k0,..,kX] and d[k0]..[kX] == v, update_in returns a copy of the
    original dictionary with v replaced by func(v), but does not mutate the
    original dictionary.

    If k0 is not a key in d, update_in creates nested dictionaries to the depth
    specified by the keys, with the innermost value set to func(default).

    >>> inc = lambda x: x + 1
    >>> update_in({'a': 0}, ['a'], inc)
    {'a': 1}

    >>> transaction = {'name': 'Alice',
    ...                'purchase': {'items': ['Apple', 'Orange'],
    ...                             'costs': [0.50, 1.25]},
    ...                'credit card': '5555-1234-1234-1234'}
    >>> update_in(transaction, ['purchase', 'costs'], sum) # doctest: +SKIP
    {'credit card': '5555-1234-1234-1234',
     'name': 'Alice',
     'purchase': {'costs': 1.75, 'items': ['Apple', 'Orange']}}

    >>> # updating a value when k0 is not in d
    >>> update_in({}, [1, 2, 3], str, default="bar")
    {1: {2: {3: 'bar'}}}
    >>> update_in({1: 'foo'}, [2, 3, 4], inc, 0)
    {1: 'foo', 2: {3: {4: 1}}}
    r   r!   N)r"   AssertionErrorr   r   )r'   r3   r)   defaultr   r+   ZksZ	innermostr   r   r   r      s    "
r   Fc          
   C   s4   yt tj| |S  tttfk
r.   |r* |S X dS )a4   Returns coll[i0][i1]...[iX] where [i0, i1, ..., iX]==keys.

    If coll[i0][i1]...[iX] cannot be found, returns ``default``, unless
    ``no_default`` is specified, then it raises KeyError or IndexError.

    ``get_in`` is a generalization of ``operator.getitem`` for nested data
    structures such as dictionaries and lists.

    >>> transaction = {'name': 'Alice',
    ...                'purchase': {'items': ['Apple', 'Orange'],
    ...                             'costs': [0.50, 1.25]},
    ...                'credit card': '5555-1234-1234-1234'}
    >>> get_in(['purchase', 'items', 0], transaction)
    'Apple'
    >>> get_in(['name'], transaction)
    'Alice'
    >>> get_in(['purchase', 'total'], transaction)
    >>> get_in(['purchase', 'items', 'apple'], transaction)
    >>> get_in(['purchase', 'items', 10], transaction)
    >>> get_in(['purchase', 'total'], transaction, 0)
    0
    >>> get_in(['y'], {}, no_default=True)
    Traceback (most recent call last):
        ...
    KeyError: 'y'

    See Also:
        itertoolz.get
        operator.getitem
    N)r   operatorgetitemKeyError
IndexErrorr   )r3   Zcollr7   Z
no_defaultr   r   r   r     s    r   )NF)r2   r8   Ztoolz.compatibilityr   r   r   r   r   r   __all__r    r   r	   r   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>   s"    -