U
    /e                     @  s   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l
mZmZmZmZmZ e	dZG dd deeef eeeef Zdd	d
ddZdS )    )annotations)CallableIterableIteratorMutableMapping)GenericTypeVar)KTVTZictBasecloseflushWTc                      s   e Zd ZU dZded< ded< ded< dddd fd	d
ZdddddZddddddZdddddZdddddZ	dddddZ
ddd d!Zd"dd#d$Zd%dd&d'ZeZddd(d)Zddd*d+Z  ZS ),Funca  Buffer a MutableMapping with a pair of input/output functions

    Parameters
    ----------
    dump: callable
        Function to call on value as we set it into the mapping
    load: callable
        Function to call on value as we pull it from the mapping
    d: MutableMapping

    Examples
    --------
    >>> def double(x):
    ...     return x * 2

    >>> def halve(x):
    ...     return x / 2

    >>> d = {}
    >>> f = Func(double, halve, d)
    >>> f['x'] = 10
    >>> d
    {'x': 20}
    >>> f['x']
    10.0
    zCallable[[VT], WT]dumpzCallable[[WT], VT]loadzMutableMapping[KT, WT]d)r   r   r   c                   s    t    || _|| _|| _d S N)super__init__r   r   r   )selfr   r   r   	__class__ -/tmp/pip-unpacked-wheel-z3s6s24u/zict/func.pyr   +   s    
zFunc.__init__r	   r
   )keyreturnc                 C  s   |  | j| S r   )r   r   r   r   r   r   r   __getitem__6   s    zFunc.__getitem__None)r   valuer   c                 C  s   |  || j|< d S r   )r   r   )r   r   r    r   r   r   __setitem__9   s    zFunc.__setitem__objectboolc                 C  s
   || j kS r   r   r   r   r   r   __contains__<   s    zFunc.__contains__c                 C  s   | j |= d S r   r$   r   r   r   r   __delitem__?   s    zFunc.__delitem__zIterable[tuple[KT, VT]])itemsr   c                   s"    fdd|D } j | d S )Nc                 3  s    | ]\}}|  |fV  qd S r   )r   ).0kvr   r   r   	<genexpr>C   s     z"Func._do_update.<locals>.<genexpr>)r   update)r   r'   itr   r+   r   
_do_updateB   s    zFunc._do_updatezIterator[KT])r   c                 C  s
   t | jS r   )iterr   r+   r   r   r   __iter__F   s    zFunc.__iter__intc                 C  s
   t | jS r   )lenr   r+   r   r   r   __len__I   s    zFunc.__len__strc                 C  s&   dt | j dt | j d| j dS )Nz<Func: z<-> >)funcnamer   r   r   r+   r   r   r   __str__L   s    zFunc.__str__c                 C  s   t | j d S r   )r   r   r+   r   r   r   r   Q   s    z
Func.flushc                 C  s   t | j d S r   )r   r   r+   r   r   r   r   T   s    z
Func.close)__name__
__module____qualname____doc____annotations__r   r   r!   r%   r&   r/   r1   r4   r9   __repr__r   r   __classcell__r   r   r   r   r      s    
r   r   r5   )funcr   c                 C  s<   t | dr| j} q z| jW S  tk
r6   t|  Y S X dS )zGet the name of a function.rA   N)hasattrrA   r:   	Exceptionr5   )rA   r   r   r   r8   X   s    
r8   N)
__future__r   collections.abcr   r   r   r   typingr   r   Zzict.commonr	   r
   r   r   r   r   r   r8   r   r   r   r   <module>   s   $M