o
    ,wi_                     @   s<  d Z ddlmZ ddlmZ ddlmZ ddlmZ ddlZddl	Z
dedejfd	d
Ze
jedfe
jedfe
jedfe
jedfe
jedfe
jedfe
jedfe
jedfe
jedfe
jedfe
jedfe
jedfe
jedfe
jedfe
jedfe
jedffZdZdd Z dd Z!d d! Z"dS )"zFiddle extensions to handle numpy code more elegantly.

Currently this affects codegen, graphviz, and other debugging functions.
    )daglish_extensions)import_manager)py_val_to_cst_converter)special_value_codegenNnamereturnc                    s   t d fddS )Nnumpyc                    s   |  d  S )N. )np_namer   r
   Y/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/fiddle/_src/extensions/numpy.py<lambda>   s    z%_make_np_importable.<locals>.<lambda>)r   SingleImportabler   r
   r   r   _make_np_importable   s   r   int8
complex128clongdouble	complex64float64
longdoublefloat16float32int16int32int64timedelta64uint8uint64uint16uint32))r   zimport numpy as npc                 C   s   t | tjS )a!  Returns True if `value` is a Numpy dtype instance.

  Dtype instances are created via np.dtype(), and can be simple ones like
  np.dtype('float32'), but also complex ones like,

  np.dtype([('name', 'S10'), ('age', 'i4'), ('height', 'f8')])

  Args:
    value: Arbitrary value to check.
  )
isinstancenpdtype)valuer
   r
   r   is_plain_dtype=   s   r%   c                 C   s\   | j rtd| j}|drtd|  dtjtj|ttddt	||gdS )Nz$Struct dtypes are not supported yet.voidzDtype object z] had a name that started with "void", this usually indicates that it could not be serialized.r#   )r$   attr)funcargs)
fieldsNotImplementedErrorr   
startswithcstCall	Attributer"   NameArg)r$   convert_childr   r
   r
   r   convert_np_type_to_cstK   s   

r3   c                  C   s^   t D ]
\} }t| | qtD ]
\}}t|| qttt	 t D ]	\}}t
| q#dS )zdRegisters Numpy fiddle extensions.

  This allows for things like nicer handling of numpy dtypes.
  N)_np_type_importablesr   register_exact_value_import_aliasesr   register_import_aliasr    register_py_val_to_cst_converterr%   r3   r   register_immutable)r$   
importable
module_strimport_stmtr#   _r
   r
   r   enableZ   s   r>   )#__doc__fiddle._srcr   fiddle._src.codegenr   r   r   libcstr-   r   r"   str
Importabler   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r4   r6   r%   r3   r>   r
   r
   r
   r   <module>   s:   