U
    JºcQ)  ã                   @   sà   d dl Z d dlZd dlmZ d dl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"d#„ Zd$d%„ Zd&d'„ ZdS ))é    N)Úreduce)ÚMapping)Ú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,{}() got an unexpected keyword argument '{}'r   )ÚpopÚdictÚ	TypeErrorÚformatÚ__name__Úpopitem)ÚfÚkwargsr   © r   úW/tmp/pip-unpacked-wheel-gikjz4vx/torch/fx/experimental/unification/unification_tools.pyÚ_get_factory
   s     
ÿr   c                  O   sJ   t | ƒdkr"t| d tƒs"| d } tt|ƒ}|ƒ }| D ]}| |¡ q6|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|ƒ}|ƒ }|D ]8}| ¡ D ]*\}}||kr^|g||< qB||  |¡ qBq6t| ||ƒ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   ÚitemsÚappendr   )Úfuncr!   r   r   Úresultr#   ÚkÚvr   r   r   r   *   s    
r   c                 C   s(   |ƒ }|  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    ÚzipÚkeysÚmapÚvalues©r&   r#   r   r"   r   r   r   r   G   s    r   c                 C   s(   |ƒ }|  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   W   s    r   c                 C   s   |ƒ }|  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   g   s    r   c                 C   s,   |ƒ }|  ¡ D ]\}}| |ƒr|||< q|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	   w   s
    
r	   c                 C   s,   |ƒ }|  ¡ D ]\}}| |ƒr|||< q|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/   r0   r   r   r   r
   ‹   s
    
r
   c                 C   s0   |ƒ }|  ¡ D ]}| |ƒr|\}}|||< q|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/   )r1   r#   r   r"   Úitemr(   r)   r   r   r   r   Ÿ   s    
r   c                 C   s   |ƒ }|  | ¡ |||< |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#   ÚkeyÚvaluer   Úd2r   r   r   r   ·   s    

r   c                 O   st   t t|ƒ}|ƒ }t|ƒt| ƒd k rH| | ¡ |D ]}||kr2||= q2n(t| ƒ}| |¡ |D ]}| | ||< q^|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}
    g333333ã?)r   r   r   r    ÚsetÚdifference_update)r#   r+   r   r   r5   r3   Ú	remainingr(   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©r4   r   r   Ú<lambda>ð   ó    zassoc_in.<locals>.<lambda>)r   )r#   r+   r4   r   r   r<   r   r   ä   s    r   c                 C   s”   t |ƒ}t|ƒ}|ƒ  }}| | ¡ |D ]@}	|| krN| | } |ƒ }
|
 | ¡ n
|ƒ  } }
|
 ||< }|	}q(|| kr„|| | ƒ||< n||ƒ||< |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}}}
    )ÚiterÚnextr    )r#   r+   r&   Údefaultr   Úksr(   r"   Úinnerr3   Zdtempr   r   r   r   ó   s     "


r   Fc              
   C   s:   zt tj| |ƒW S  tttfk
r4   |r,‚ | Y 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   )r+   ZcollrA   Z
no_defaultr   r   r   r   -  s    r   c                    sP   t ˆ tƒrBtˆ ƒdkr*ˆ d ‰ ‡ fdd„S ˆ r8tjˆ Ž S dd„ S n
t ˆ ¡S d S )Nr   r   c                    s
   | ˆ  fS r9   r   r:   ©Úindexr   r   r=   W  r>   zgetter.<locals>.<lambda>c                 S   s   dS )Nr   r   r:   r   r   r   r=   [  r>   )r   Úlistr   rD   Ú
itemgetterrH   r   rH   r   ÚgetterS  s    


rL   c                 C   s\   t | ƒst| ƒ} t dd„ ¡}|D ]}|| |ƒ |ƒ q"i }| ¡ D ]\}}|j||< qD|S )aV   Group a collection by a key function

    >>> names = ['Alice', 'Bob', 'Charlie', 'Dan', 'Edith', 'Frank']
    >>> groupby(len, names)  # doctest: +SKIP
    {3: ['Bob', 'Dan'], 5: ['Alice', 'Edith', 'Frank'], 7: ['Charlie']}

    >>> iseven = lambda x: x % 2 == 0
    >>> groupby(iseven, [1, 2, 3, 4, 5, 6, 7, 8])  # doctest: +SKIP
    {False: [1, 3, 5, 7], True: [2, 4, 6, 8]}

    Non-callable keys imply grouping on a member.

    >>> groupby('gender', [{'name': 'Alice', 'gender': 'F'},
    ...                    {'name': 'Bob', 'gender': 'M'},
    ...                    {'name': 'Charlie', 'gender': 'M'}]) # doctest:+SKIP
    {'F': [{'gender': 'F', 'name': 'Alice'}],
     'M': [{'gender': 'M', 'name': 'Bob'},
           {'gender': 'M', 'name': 'Charlie'}]}

    Not to be confused with ``itertools.groupby``

    See Also:
        countby
    c                   S   s   g j S r9   )r%   r   r   r   r   r=   z  r>   zgroupby.<locals>.<lambda>)ÚcallablerL   ÚcollectionsÚdefaultdictr$   Ú__self__)r3   Úseqr#   r2   r"   r(   r)   r   r   r   Úgroupby_  s    rR   c                 C   s   t t| ƒƒS )zC The first element in a sequence

    >>> first('ABC')
    'A'
    )r@   r?   )rQ   r   r   r   Úfirst‚  s    rS   )NF)rN   rD   Ú	functoolsr   Úcollections.abcr   Ú__all__r   r   r   r   r   r   r   r	   r
   r   r   r   r   r   r   rL   rR   rS   r   r   r   r   Ú<module>   s*   :
&#