U
    “öhÓ  ã                   @  s‚   d Z ddlmZ ddlZddlZddlZddlmZ dddœdd	„Zd
d„ Z	dddœdd„Z
dddœdd„Zddddœdd„ZdS )zGConvenience functions for writing to and reading from Python variables.é    )ÚannotationsN)ÚAnyÚstrÚNone)Úvar_nameÚreturnc                 C  s2   |   ¡ std | ¡ƒ‚t | ¡r.td | ¡ƒ‚dS )z7Validates that the variable name is a valid identifier.z&Invalid Python variable name, got "{}"z$Cannot use Python keywords, got "{}"N)ÚisidentifierÚ
ValueErrorÚformatÚkeywordÚ	iskeyword)r   © r   úI/tmp/pip-unpacked-wheel-doshhd5e/google/generativeai/notebook/py_utils.pyÚvalidate_var_name   s    
r   c                   C  s
   t jd S )NÚ__main__)ÚsysÚmodulesr   r   r   r   Úget_main_module    s    r   r   c                 C  sL   t | ƒ ttƒ ƒ}| |kr"||  S | ttƒkr:ttƒ|  S td | ¡ƒ‚dS )z>Retrieves the value of `var_name` from the global environment.z"{}" not foundN)r   Úvarsr   ÚbuiltinsÚ	NameErrorr
   )r   Úg_varsr   r   r   Ú
get_py_var$   s    
r   Úboolc                 C  sD   zt | ƒ t| ƒ}W n* tk
r*   Y dS  tk
r>   Y dS X dS )z@Returns true if `var_name` is defined in the global environment.FT)r   r   r	   r   )r   Ú_r   r   r   Ú
has_py_var/   s    r   )r   Úvalr   c                 C  s   t | ƒ ttƒ ƒ}||| < dS )z7Sets the value of `var_name` in the global environment.N)r   r   r   )r   r   r   r   r   r   Ú
set_py_var<   s    
r   )Ú__doc__Ú
__future__r   r   r   r   Útypingr   r   r   r   r   r   r   r   r   r   Ú<module>   s   