
    iA                        d Z ddlZddlZddlmZ ddlmZm	Z	 ddl
mZ dZ ej                  d	d
      Z ej                  d      Z ej                  d      Z G d deej"                  e         Z G d dej"                  e         Z G d de      Zefdej,                  ee	f   dee   fdZdedej0                  egej2                  f   defdZy)z5
Method chaining interface.

.. versionadded:: 1.0.0
    N)InvalidMethod   )UNSETUnset   )AllFuncs)chaintap	ValueT_coT)	covariantTT2c                       e Zd ZdZeZeZefde	j                  eef   ddfdZddZdefdZdefdZdd
Zde	j&                  dd	fdZdefdZy)Chainz-Enables chaining of :attr:`module` functions.valuereturnNc                     || _         y )N_valueselfr   s     P/home/Script/Script_env/lib/python3.12/site-packages/pydash/chaining/chaining.py__init__zChain.__init__!   s	        c                 .    t        | j                  |      S )zImplement `AllFuncs` interface.)ChainWrapperr   )r   funcs     r   _wrapzChain._wrap$   s    DKK..r   c                 &     | | j                         S )z
        Return current value of the chain operations.

        Returns:
            Current value of chain operations.
        r   r   s    r   r   zChain.value(   s     DKK  r   c                 T    | j                   j                  | j                               S )z
        Return current value as string.

        Returns:
            Current value of chain operations casted to ``str``.
        )module	to_stringr   r    s    r   r#   zChain.to_string1   s     {{$$TZZ\22r   Chain[ValueT_co]c                 4    t        | j                               S )z
        Executes the chained sequence and returns the wrapped result.

        Returns:
            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      rV|g}t        |j                   t              r9|j                   }|j	                  d|       t        |j                   t              r9t        |      }|D ]<  } t        |j                   |j                        |j                  i |j                  }> |S )z
        Return a clone of the chained sequence planting `value` as the wrapped value.

        Args:
            value: Value to plant as the initial chain value.
        r   r   )	r   hasattr
isinstancer   insertr   methodargskwargs)r   r   wrapperwrappersclonewraps         r   plantzChain.plantD   s     ++7H%yHW^^\:!..7+ W^^\: $El 	D;Lt{{;++E	 r   c                 p    t        | j                  t              r| j                  j                  |      }|S )z
        Return result of passing `value` through chained methods.

        Args:
            value: Initial value to pass through chained methods.

        Returns:
            Result of method chain evaluation of `value`.
        )r)   r   r   unwrapr   s     r   __call__zChain.__call__`   s+     dkk<0KK&&u-Er   )r   z'ChainWrapper[t.Union[ValueT_co, Unset]])r   r$   )__name__
__module____qualname____doc__pydr"   r   invalid_method_exceptionr   tUnionr   r   r   r   r   strr#   r&   Anyr2   r5    r   r   r   r      s|    7 F,:? aggi&67 D /!y !33 3#155 %7 8 r   r   c                   6    e Zd ZdZdeddfdZd ZefdZd Z	y)	r   zGWrap :class:`Chain` method call within a :class:`ChainWrapper` context.r   r   Nc                 <    || _         || _        d| _        i | _        y )Nr@   )r   r+   r,   r-   )r   r   r+   s      r   r   zChainWrapper.__init__s   s    	,.r   c                     | j                   j                  | j                         }| j                  j                         |_        |S )z!Generate a copy of this instance.)	__class____new____dict__copy)r   news     r   	_generatezChainWrapper._generatey   s5     nn$$T^^4}}))+
r   c                 f   | j                         }t        |j                  t              r!|j                  j	                  |      |_        nt        |t              s|t
        ur||_        |j                  t
        ur|j                  } |j                  |g|j                  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`.
        )	rI   r)   r   r   r4   r   r+   r,   r-   )r   r   r.   s      r   r4   zChainWrapper.unwrap   s     .."gnnl3$^^2259GNE<0U%5G"GN>>&NNEw~~eEgllEgnnEEr   c                 4    || _         || _        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:
            New instance of :class:`Chain` with the results of :attr:`method` passed in as
                value.
        )r,   r-   r   )r   r,   r-   s      r   r5   zChainWrapper.__call__   s     	T{r   )
r6   r7   r8   r9   r   r   rI   r   r4   r5   r@   r   r   r   r   p   s-    Q/i /D / ! F4r   r   c                   N    e Zd ZdZd Zefdej                  ee	f   de
e   fdZy)_Dashz}Class that provides attribute access to valid :mod:`pydash` methods and callable access to
    :mod:`pydash` method chaining.c                 ,    t         j                  |      S )z"Proxy to :meth:`Chain.get_method`.)r   
get_method)r   attrs     r   __getattr__z_Dash.__getattr__   s    %%r   r   r   c                     t        |      S )zAReturn a new instance of :class:`Chain` with `value` as the seed.r   r   s     r   r5   z_Dash.__call__   s    U|r   N)r6   r7   r8   r9   rQ   r   r<   r=   r   r   r   r5   r@   r   r   rM   rM      s8    && ;@ aggi&67 E)DT r   rM   r   r   c                     t        |       S )ar  
    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: Value to initialize chain operations with.

    Returns:
        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`.
    rS   )r   s    r   r	   r	      s    l <r   interceptorc                      ||        | 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: Current value of chain operation.
        interceptor: Function called on `value`.

    Returns:
        `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   rU   s     r   r
   r
      s    2 Lr   )r9   typingr<   pydashr:   pydash.exceptionsr   helpersr   r   	all_funcsr   __all__TypeVarr   r   r   Genericr   r   objectrM   r=   r	   Callabler?   r
   r@   r   r   <module>ra      s      + " 
 AIIkT2	AIIcNQYYt_SHaii	* Sl5199Y' 5p
F 
 &+ 6E" 6uQx 6rq qzz1#quu*5 ! r   