
    O=d#                         d Z ddlZddlmZ dZ G d de          Z G d d	e          Z G d
 de          Z	efdZ
d Zd ZdS )z5
Method chaining interface.

.. versionadded:: 1.0.0
    N   )UNSET)chaintapthruc                   Z    e Zd ZdZeZefdZd Zd Z	d Z
d Zed             Zd Zd	 Zd
S )Chainz-Enables chaining of :attr:`module` functions.c                     || _         d S )N_valueselfvalues     9D:\Sites\api_v1\venv\Lib\site-packages\pydash/chaining.py__init__zChain.__init__   s        c                 "     | | j                   S )z
        Return current value of the chain operations.

        Returns:
            mixed: Current value of chain operations.
        r   r   s    r   r   zChain.value   s     tDK   r   c                 Z    | j                             |                                           S )z
        Return current value as string.

        Returns:
            str: Current value of chain operations casted to ``str``.
        )module	to_stringr   r   s    r   r   zChain.to_string%   s"     {$$TZZ\\222r   c                 D    t          |                                           S )z
        Executes the chained sequence and returns the wrapped result.

        Returns:
            Chain: New instance of :class:`Chain` with resolved value from
                previous :class:`Class`.
        )r	   r   r   s    r   commitzChain.commit.   s     TZZ\\"""r   c                 \   | j         }g }t          |d          rT|g}t          |j         t                    r7|j         }|                    d|           t          |j         t                    7t          |          }|D ],} t          |j         |j                  |j        i |j        }-|S )z
        Return a clone of the chained sequence planting `value` as the wrapped value.

        Args:
            value (mixed): Value to plant as the initial chain value.
        r   r   )	r   hasattr
isinstanceChainWrapperinsertr	   methodargskwargs)r   r   wrapperwrappersclonewraps         r   plantzChain.plant8   s     +7H%% 	,yHW^\:: ,!.7+++ W^\:: , e 	W 	WD;Lt{;;TYV$+VVEEr   c                    |dv r| S t          | j        |d          }t          |          s.|                    d          st          | j        |dz   d          }t          |          s| j                            d|           |S )a  
        Return valid :attr:`module` method.

        Args:
            name (str): Name of pydash method to get.

        Returns:
            function: :attr:`module` callable.

        Raises:
            InvalidMethod: Raised if `name` is not a valid :attr:`module` method.
        )__wrapped__N_zInvalid pydash method: )getattrr   callableendswithInvalidMethod)clsnamer   s      r   
get_methodzChain.get_methodQ   s    " ###JT400 	;c(:(: 	; SZT::F 	M***+KT+K+KLLLr   c                 R    t          | j        |                     |                    S )aJ  
        Proxy attribute access to :attr:`module`.

        Args:
            attr (str): Name of :attr:`module` function to chain.

        Returns:
            ChainWrapper: New instance of :class:`ChainWrapper` with value passed on.

        Raises:
            InvalidMethod: Raised if `attr` is not a valid function.
        )r   r   r0   r   attrs     r   __getattr__zChain.__getattr__r   s"     DK)>)>???r   c                 n    t          | j        t                    r| j                            |          }|S )z
        Return result of passing `value` through chained methods.

        Args:
            value (mixed): Initial value to pass through chained methods.

        Returns:
            mixed: Result of method chain evaluation of `value`.
        )r   r   r   unwrapr   s     r   __call__zChain.__call__   s2     dk<00 	.K&&u--Er   N)__name__
__module____qualname____doc__pydr   r   r   r   r   r   r&   classmethodr0   r4   r7    r   r   r	   r	      s        77 F"    ! ! !3 3 3# # #  2   [@@ @ @    r   r	   c                   .    e Zd ZdZd Zd ZefdZd ZdS )r   zGWrap :class:`Chain` method call within a :class:`ChainWrapper` context.c                 >    || _         || _        d| _        i | _        d S )Nr>   )r   r   r    r!   )r   r   r   s      r   r   zChainWrapper.__init__   s"    	r   c                     | j                             | j                   }| j                                        |_        |S )z!Generate a copy of this instance.)	__class____new____dict__copy)r   news     r   	_generatezChainWrapper._generate   s5     n$$T^44}))++
r   c                 H   |                                  }t          |j        t                    r |j                            |          |_        n%t          |t                    s|t
          ur||_        |j        t
          ur|j        } |j        |g|j        R i |j        S )z
        Execute :meth:`method` with :attr:`_value`, :attr:`args`, and :attr:`kwargs`.

        If :attr:`_value` is an instance of :class:`ChainWrapper`, then unwrap it before calling
        :attr:`method`.
        )	rG   r   r   r   r6   r   r   r    r!   )r   r   r"   s      r   r6   zChainWrapper.unwrap   s     ..""gnl33 	#$^22599GNNE<00 	#U%5G5G"GN>&&NEw~eEglEEEgnEEEr   c                 <    || _         || _        t          |           S )a%  
        Invoke the :attr:`method` with :attr:`value` as the first argument and return a new
        :class:`Chain` object with the return value.

        Returns:
            Chain: New instance of :class:`Chain` with the results of :attr:`method` passed in as
                value.
        )r    r!   r	   )r   r    r!   s      r   r7   zChainWrapper.__call__   s     	T{{r   N)	r8   r9   r:   r;   r   rG   r   r6   r7   r>   r   r   r   r      sd        QQ     ! F F F F4    r   r   c                   "    e Zd ZdZd ZefdZdS )_Dashz}Class that provides attribute access to valid :mod:`pydash` methods and callable access to
    :mod:`pydash` method chaining.c                 6    t                               |          S )z"Proxy to :meth:`Chain.get_method`.)r	   r0   r2   s     r   r4   z_Dash.__getattr__   s    %%%r   c                      t          |          S )zAReturn a new instance of :class:`Chain` with `value` as the seed.r	   r   s     r   r7   z_Dash.__call__   s    U||r   N)r8   r9   r:   r;   r4   r   r7   r>   r   r   rK   rK      sF        & && & & #      r   rK   c                      t          |           S )az  
    Creates a :class:`Chain` object which wraps the given value to enable intuitive method chaining.
    Chaining is lazy and won't compute a final value until :meth:`Chain.value` is called.

    Args:
        value (mixed): Value to initialize chain operations with.

    Returns:
        :class:`Chain`: Instance of :class:`Chain` initialized with `value`.

    Example:

        >>> chain([1, 2, 3, 4]).map(lambda x: x * 2).sum().value()
        20
        >>> chain().map(lambda x: x * 2).sum()([1, 2, 3, 4])
        20

        >>> summer = chain([1, 2, 3, 4]).sum()
        >>> new_summer = summer.plant([1, 2])
        >>> new_summer.value()
        3
        >>> summer.value()
        10

        >>> def echo(item): print(item)
        >>> summer = chain([1, 2, 3, 4]).for_each(echo).sum()
        >>> committed = summer.commit()
        1
        2
        3
        4
        >>> committed.value()
        10
        >>> summer.value()
        1
        2
        3
        4
        10

    .. versionadded:: 1.0.0

    .. versionchanged:: 2.0.0
        Made chaining lazy.

    .. versionchanged:: 3.0.0

        - Added support for late passing of `value`.
        - Added :meth:`Chain.plant` for replacing initial chain value.
        - Added :meth:`Chain.commit` for returning a new :class:`Chain` instance initialized with
          the results from calling :meth:`Chain.value`.
    rN   )r   s    r   r   r      s    j <<r   c                      ||            | S )a  
    Invokes `interceptor` with the `value` as the first argument and then returns `value`. The
    purpose of this method is to "tap into" a method chain in order to perform operations on
    intermediate results within the chain.

    Args:
        value (mixed): Current value of chain operation.
        interceptor (callable): Function called on `value`.

    Returns:
        mixed: `value` after `interceptor` call.

    Example:

        >>> data = []
        >>> def log(value): data.append(value)
        >>> chain([1, 2, 3, 4]).map(lambda x: x * 2).tap(log).value()
        [2, 4, 6, 8]
        >>> data
        [[2, 4, 6, 8]]

    .. versionadded:: 1.0.0
    r>   r   interceptors     r   r   r     s    0 KLr   c                      ||           S )a  
    Returns the result of calling `interceptor` on `value`. The purpose of this method is to pass
    `value` through a function during a method chain.

    Args:
        value (mixed): Current value of chain operation.
        interceptor (callable): Function called with `value`.

    Returns:
        mixed: Results of ``interceptor(value)``.

    Example:

        >>> chain([1, 2, 3, 4]).thru(lambda x: x * 2).value()
        [1, 2, 3, 4, 1, 2, 3, 4]

    .. versionadded:: 2.0.0
    r>   rQ   s     r   r   r   *  s    & ;ur   )r;   pydashr<   helpersr   __all__objectr	   r   rK   r   r   r   r>   r   r   <module>rX      s              { { { { {F { { {|5 5 5 5 56 5 5 5p
 
 
 
 
F 
 
 
  5 5 5 5p  8    r   