U
    /e                     @   sN   d Z ddlZddlmZ ddlmZ ejZe Zd	ddZ	G dd dZ
dS )
z$
Control global computation context
    N)partial)configc                 C   s$   | dkrt t||dS t| ||dS )a  Allow function to be taken over by globals

    This modifies a method so that occurrences of it may be taken over by
    functions registered in the global options. Can be used as a decorator or a
    function.

    Parameters
    ----------
    default : callable
        The default callable to use.
    key : str
        Key under which we register this function in the global parameters
    falsey : callable, None, optional
        A function to use if the option is falsey. If not provided, the default
        is used instead.

    Examples
    --------
    >>> import dask
    >>> class Foo:
    ...     @globalmethod(key='bar', falsey=lambda: 3)
    ...     def bar():
    ...         return 1
    >>> f = Foo()
    >>> f.bar()
    1
    >>> with dask.config.set(bar=lambda: 2):
    ...     print(f.bar())
    2
    >>> with dask.config.set(bar=False):
    ...     print(f.bar())
    3
    N)keyfalseydefaultr   r   )r   globalmethodGlobalMethodr    r
   0/tmp/pip-unpacked-wheel-dbjnr7gq/dask/context.pyr      s    "r   c                   @   s    e Zd ZdddZdddZdS )r	   Nc                 C   s   || _ || _|| _d S N)_default_key_falsey)selfr   r   r   r
   r
   r   __init__7   s    zGlobalMethod.__init__c                 C   s4   | j tkr.t| j  rt| j  S | jd k	r.| jS | jS r   )r   _globalsr   r   )r   instanceownerr
   r
   r   __get__<   s    



zGlobalMethod.__get__)N)N)__name__
__module____qualname__r   r   r
   r
   r
   r   r	   6   s   
r	   )NNN)__doc__	threading	functoolsr   Zdaskr   r   localZthread_stater   r	   r
   r
   r
   r   <module>   s   
'