B
    ¢X[\Ÿ  ã               @   s@   d dl mZmZmZ ddlmZmZ G dd„ deƒZdd„ ZdS )	é    )Úabsolute_importÚdivisionÚprint_functioné   )ÚZictBaseÚclosec               @   s„   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ ZeZdd„ Zdd„ Zd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 = dict()
    >>> f = Func(double, halve, d)
    >>> f['x'] = 10
    >>> d
    {'x': 20}
    >>> f['x']
    10.0
    c             C   s   || _ || _|| _d S )N)ÚdumpÚloadÚd)Úselfr	   r
   r   © r   ú(lib/python3.7/site-packages/zict/func.pyÚ__init__!   s    zFunc.__init__c             C   s   |   | j| ¡S )N)r
   r   )r   Úkeyr   r   r   Ú__getitem__&   s    zFunc.__getitem__c             C   s   |   |¡| j|< d S )N)r	   r   )r   r   Úvaluer   r   r   Ú__setitem__)   s    zFunc.__setitem__c             C   s
   || j kS )N)r   )r   r   r   r   r   Ú__contains__,   s    zFunc.__contains__c             C   s   | j |= d S )N)r   )r   r   r   r   r   Ú__delitem__/   s    zFunc.__delitem__c             C   s
   | j  ¡ S )N)r   Úkeys)r   r   r   r   r   2   s    z	Func.keysc             C   s   t | j| j ¡ ƒS )N)Úmapr
   r   Úvalues)r   r   r   r   r   5   s    zFunc.valuesc                s   ‡ fdd„ˆ j  ¡ D ƒS )Nc             3   s    | ]\}}|ˆ   |¡fV  qd S )N)r
   )Ú.0ÚkÚv)r   r   r   ú	<genexpr>9   s    zFunc.items.<locals>.<genexpr>)r   Úitems)r   r   )r   r   r   8   s    z
Func.itemsc                s   ˆ j  ‡ fdd„|D ƒ¡ d S )Nc             3   s    | ]\}}|ˆ   |¡fV  qd S )N)r	   )r   r   r   )r   r   r   r   <   s    z"Func._do_update.<locals>.<genexpr>)r   Úupdate)r   r   r   )r   r   Ú
_do_update;   s    zFunc._do_updatec             C   s
   t | jƒS )N)Úiterr   )r   r   r   r   Ú__iter__>   s    zFunc.__iter__c             C   s
   t | jƒS )N)Úlenr   )r   r   r   r   Ú__len__A   s    zFunc.__len__c             C   s    dt | jƒt | jƒt| jƒf S )Nz<Func: %s<->%s %s>)Úfuncnamer	   r
   Ústrr   )r   r   r   r   Ú__str__D   s    
zFunc.__str__c             C   s   | j  ¡  d S )N)r   Úflush)r   r   r   r   r'   K   s    z
Func.flushc             C   s   t | jƒ d S )N)r   r   )r   r   r   r   r   N   s    z
Func.closeN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r   r   r   r!   r#   r&   Ú__repr__r'   r   r   r   r   r   r      s    r   c             C   s0   xt | dƒr| j} qW y| jS    t| ƒS dS )zGet the name of a function.ÚfuncN)Úhasattrr-   r(   r%   )r-   r   r   r   r$   R   s    
r$   N)	Z
__future__r   r   r   Úcommonr   r   r   r$   r   r   r   r   Ú<module>   s   L