U
    h                     @  s   d Z ddlmZ ddlZddlmZmZmZ eZddddd	Z	d
dddddZ
G dd deeef ejdZG dd deZdS )zLLMFnOutputRow.    )annotationsN)AnyIteratorMapping	type[Any]str)xreturnc                 C  s   t | dr| jS t| S )N__name__)hasattrr
   r   )r    r   U/tmp/pip-unpacked-wheel-doshhd5e/google/generativeai/notebook/lib/llmfn_output_row.py_get_name_of_type   s    
r   r   None)valueresult_typer	   c                 C  s6   |t krd S t| |s2tdt|tt| d S )Nz2Value of last entry must be of type "{}", got "{}")r   
isinstance
ValueErrorformatr   type)r   r   r   r   r   _validate_is_result_type    s    

r   c                   @  sv   e Zd ZdZejdddddZejdddd	Zejd
dddZejddddZ	ejddddZ
dS )LLMFnOutputRowViewz!Immutable view of LLMFnOutputRow.r   boolkr	   c                 C  s   dS )z)For expressions like: x in this_instance.Nr   selfr   r   r   r   __contains__0   s    zLLMFnOutputRowView.__contains__r	   c                 C  s   dS )z)For expressions like: str(this_instance).Nr   r   r   r   r   __str__4   s    zLLMFnOutputRowView.__str__r   c                 C  s   dS )z.Returns the type enforced for the result cell.Nr   r   r   r   r   r   9   s    zLLMFnOutputRowView.result_typer   c                 C  s   dS )z!Get the value of the result cell.Nr   r   r   r   r   result_value=   s    zLLMFnOutputRowView.result_valuec                 C  s   dS )zGet the key of the result cell.Nr   r   r   r   r   
result_keyA   s    zLLMFnOutputRowView.result_keyN)r
   
__module____qualname____doc__abcabstractmethodr   r    r   r!   r"   r   r   r   r   r   ,   s   r   )	metaclassc                   @  s   e Zd ZdZdddddZddd	d
ZddddZdddddZdddddZddddZ	ddddZ
ddddZddddZd(dd d!d"d#d$Zddd!d%d&d'ZdS ))LLMFnOutputRowa  Container that represents a single row in a table of outputs.

  We represent outputs as a table. This class represents a single row in the
  table like a dictionary, where the key is the column name and the value is the
  cell value.

  A single cell is designated the "result". This contains the output of the LLM
  model after running any post-processing functions specified by the user.

  In addition to behaving like a dictionary, this class provides additional
  methods, including:
  - Getting the value of the "result" cell
  - Setting the value (and optionally the key) of the "result" cell.
  - Add a new non-result cell

  Notes: As an implementation detail, the result-cell is always kept as the
  rightmost cell.
  zMapping[str, _CELLVALUETYPE]r   )datar   c                 C  s@   t || _| jstd|| _t| j d }t|| j dS )a  Constructor.

    Args:
      data: The initial value of the row. The last entry will be treated as the
        result. Cannot be empty. The value of the last entry must be `str`.
      result_type: The type of the result cell. This will be enforced at
        runtime.
    zMust provide non-empty dataN)dict_datar   _result_typelistvaluesr   )r   r*   r   r!   r   r   r   __init__Z   s    
zLLMFnOutputRow.__init__zIterator[str]r   c                 C  s
   | j  S N)r-   __iter__r   r   r   r   r3   n   s    zLLMFnOutputRow.__iter__intc                 C  s
   | j  S r2   )r-   __len__r   r   r   r   r5   q   s    zLLMFnOutputRow.__len__r   _CELLVALUETYPEr   c                 C  s   | j |S r2   )r-   __getitem__r   r   r   r   r7   t   s    zLLMFnOutputRow.__getitem__r   c                 C  s   | j |S r2   )r-   r   r   r   r   r   r   x   s    zLLMFnOutputRow.__contains__c                 C  s   d | j S )NzLLMFnOutputRow: {})r   r-   r    r   r   r   r   r    {   s    zLLMFnOutputRow.__str__c                 C  s   | j S r2   )r.   r   r   r   r   r   ~   s    zLLMFnOutputRow.result_typer   c                 C  s   | j |   S r2   )r-   r"   r   r   r   r   r!      s    zLLMFnOutputRow.result_valuec                 C  s   t | j d S )Nr+   )r/   r-   keysr   r   r   r   r"      s    zLLMFnOutputRow.result_keyNz
str | Noner   )r   keyr	   c                 C  sH   t || j |  }|dks$||kr2|| j|< dS | j|= || j|< dS )zSet the value of the result cell.

    Sets the value (and optionally the key) of the result cell.

    Args:
      value: The value to set the result cell today.
      key: Optionally change the key as well.
    N)r   r.   r"   r-   )r   r   r9   current_keyr   r   r   set_result_value   s    	
zLLMFnOutputRow.set_result_value)r9   r   r	   c                 C  sb   || j kr6d}|}|| j kr2d||}|d }q|}|  }| j |}|| j |< || j |< dS )zAdd a non-result cell.

    Adds a new non-result cell. This does not affect the result cell.

    Args:
      key: The key of the new cell to add.
      value: The value of the new cell to add.
       z{}_{}N)r-   r   r"   pop)r   r9   r   idxZcandidate_keyr"   r!   r   r   r   add   s    




zLLMFnOutputRow.add)N)r
   r#   r$   r%   r1   r3   r5   r7   r   r    r   r!   r"   r;   r?   r   r   r   r   r)   F   s   r)   )r%   
__future__r   r&   typingr   r   r   r6   r   r   r   ABCMetar   r)   r   r   r   r   <module>   s   