U
    3d
                     @   s>   d dl mZ d dlmZ d dlmZ G dd dZdd ZdS )	    )
MethodType)wraps)update_wrapperc                   @   s"   e Zd ZdZdd ZdddZdS )_AvailableIfDescriptorav  Implements a conditional property using the descriptor protocol.

    Using this class to create a decorator will raise an ``AttributeError``
    if check(self) returns a falsey value. Note that if check raises an error
    this will also result in hasattr returning false.

    See https://docs.python.org/3/howto/descriptor.html for an explanation of
    descriptors.
    c                 C   s    || _ || _|| _t| | d S )N)fncheckattribute_namer   )selfr   r   r    r
   ?/tmp/pip-unpacked-wheel-zrfo1fqw/sklearn/utils/_available_if.py__init__   s    z_AvailableIfDescriptor.__init__Nc                    s`   t dt|j dtj  |d k	rD|s6 tj|}ntj fdd}|S )NzThis z has no attribute c                     s    | d s j| |S )Nr   )r   r   )argskwargsZattr_errr	   r
   r   out'   s    z+_AvailableIfDescriptor.__get__.<locals>.out)AttributeErrorrepr__name__r   r   r   r   r   )r	   objownerr   r
   r   r   __get__   s    
z_AvailableIfDescriptor.__get__)N)r   
__module____qualname____doc__r   r   r
   r
   r
   r   r      s   
r   c                    s    fddS )a  An attribute that is available only if check returns a truthy value.

    Parameters
    ----------
    check : callable
        When passed the object with the decorated method, this should return
        a truthy value if the attribute is available, and either return False
        or raise an AttributeError if not available.

    Returns
    -------
    callable
        Callable makes the decorated method available if `check` returns
        a truthy value, otherwise the decorated method is unavailable.

    Examples
    --------
    >>> from sklearn.utils.metaestimators import available_if
    >>> class HelloIfEven:
    ...    def __init__(self, x):
    ...        self.x = x
    ...
    ...    def _x_is_even(self):
    ...        return self.x % 2 == 0
    ...
    ...    @available_if(_x_is_even)
    ...    def say_hello(self):
    ...        print("Hello")
    ...
    >>> obj = HelloIfEven(1)
    >>> hasattr(obj, "say_hello")
    False
    >>> obj.x = 2
    >>> hasattr(obj, "say_hello")
    True
    >>> obj.say_hello()
    Hello
    c                    s   t |  | jdS )N)r   )r   r   )r   r   r
   r   <lambda>W       zavailable_if.<locals>.<lambda>r
   r   r
   r   r   available_if0   s    'r   N)typesr   	functoolsr   r   r   r   r
   r
   r
   r   <module>   s   *