o
    }o™iõ  ã                   @   sb   d dl mZ d dlmZmZmZ d dlZedƒZG dd„ dƒZ		 e	ƒ Z
e
 e¡defdd	„ƒZdS )
é    )Úpartial)ÚAnyÚCallableÚTypeVarNÚTc                   @   sF   e Zd ZdZdd„ Zdeegef fdd„Zdd„ Z	d	e
fd
d„ZdS )ÚPredicateDispatcha:  A dispatcher that routes values to handlers based on predicates.

    This class implements a predicate-based dispatch system where handlers are registered
    with conditions that determine when they should be used. When called, it tries each
    predicate in order until it finds a match.

    Example:
        ```python
        dispatcher = PredicateDispatch()

        @dispatcher.register(lambda x: isinstance(x, str))
        def handle_strings(s):
            return f"String: {s}"

        result = dispatcher("hello")  # Returns "String: hello"
        ```
    c                 C   s
   g | _ dS )z%Initialize an empty handler registry.N©Úhandlers)Úself© r   úO/home/ubuntu/.local/lib/python3.10/site-packages/nemo/lightning/io/to_config.pyÚ__init__*   s   
zPredicateDispatch.__init__Ú	predicatec                    s"   dt tgtf f‡ ‡fdd„}|S )a  Register a new handler with a predicate.

        Args:
            predicate: A callable that takes a value and returns True if the handler
                should process this value.

        Returns:
            A decorator function that registers the handler.
        Úfuncc                    s   ˆj  ˆ | f¡ | S ©N)r	   Úappend)r   ©r   r
   r   r   Ú	decorator9   s   z-PredicateDispatch.register.<locals>.decorator)r   r   r   )r
   r   r   r   r   r   Úregister.   s   zPredicateDispatch.registerc                 C   s(   | j D ]\}}||ƒr||ƒ  S q|S )a  Process a value through the registered handlers.

        Args:
            value: The value to be processed.

        Returns:
            The processed value from the first matching handler, or the original value
            if no handlers match.
        r   )r
   Úvaluer   Úhandlerr   r   r   Ú__call__?   s
   
ÿzPredicateDispatch.__call__Úclsc                    s   |   ‡ fdd„¡S )a%  Register a handler for instances of a specific class.

        A convenience method that automatically creates an isinstance predicate.

        Args:
            cls: The class to check instances against.

        Returns:
            A decorator function that registers the handler.
        c                    s
   t | ˆ ƒS r   )Ú
isinstance)Úv©r   r   r   Ú<lambda>Y   s   
 z2PredicateDispatch.register_class.<locals>.<lambda>)r   )r
   r   r   r   r   Úregister_classN   s   z PredicateDispatch.register_classN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   Úboolr   r   Útyper   r   r   r   r   r      s    r   r   c                 C   s   t j| jg| j¢R i | j¤ŽS )ae  Convert functools.partial objects to Fiddle Partial configurations.

    This handler enables serialization of partial function applications by converting
    them to Fiddle's equivalent representation.

    Args:
        value: A functools.partial object.

    Returns:
        A Fiddle Partial configuration representing the same partial application.
    )ÚfdlÚPartialr   ÚargsÚkeywords)r   r   r   r   Úhandle_partialn   s   r(   )Ú	functoolsr   Útypingr   r   r   Úfiddler$   r   r   Ú	to_configr   r(   r   r   r   r   Ú<module>   s   E