
    d]2                     8   d Z ddlZddlZddlmZmZ dZ	 dZ	 g Z		 i Z
	 i Z	 i Z	 i Z	 d Zd Zd	 Zd
 Zd Zd Zd Zd(dZe	                    d           e	                    d           d Zd Zd Zd Zd Zd Zd Zd Zd Zd Z g dZ!dZ"d Z#g Z$dZ%ej&        '                    ej(                  ej&        '                    d          k    rd  Z)nej*        Z)d! Z+d" Z,e,d#             Z-e,d$             Z.d% Z/ e             d& Z0e1d'k    r e0             dS dS ))a5  Utilities for handling different array flavors in PyTables.

Variables
=========

`__docformat`__
    The format of documentation strings in this module.
`internal_flavor`
    The flavor used internally by PyTables.
`all_flavors`
    List of all flavors available to PyTables.
`alias_map`
    Maps old flavor names to the most similar current flavor.
`description_map`
    Maps flavors to short descriptions of their supported objects.
`identifier_map`
    Maps flavors to functions that can identify their objects.

    The function associated with a given flavor will return a true
    value if the object passed to it can be identified as being of
    that flavor.

    See the `flavor_of()` function for a friendlier interface to
    flavor identification.

`converter_map`
    Maps (source, destination) flavor pairs to converter functions.

    Converter functions get an array of the source flavor and return
    an array of the destination flavor.

    See the `array_of_flavor()` and `flavor_to_flavor()` functions for
    friendlier interfaces to flavor conversion.

    N   )FlavorErrorFlavorWarningreStructuredTextnumpyc                     | t           vr9d                    d t           D                       }t          d| d|          dS )z5Raise a ``FlavorError`` if the `flavor` is not valid.z, c              3      K   | ]}|V  d S N ).0flavs     -lib/python3.11/site-packages/tables/flavor.py	<genexpr>zcheck_flavor.<locals>.<genexpr>X   s"      #A#ATD#A#A#A#A#A#A    z	flavor ``zH`` is unsupported or unavailable; available flavors in this system are: N)all_flavorsjoinr   )flavoravailable_flavss     r   check_flavorr   T   s]     [  ))#A#A[#A#A#AAAk vv() ) 	) ! r   c                 x    ||f}|t           vrt          d|d|d          t           |         } ||           S )a  Get a version of the given `array` in a different flavor.

    The input `array` must be of the given `src_flavor`, and the
    returned array will be of the indicated `dst_flavor`.  Both
    flavors may be the same, but it is not guaranteed that the
    returned array will be the same object as the input one in this
    case.

    If the conversion is not supported, a ``FlavorError`` is raised.

    zconversion from flavor ``z`` to flavor ``z/`` is unsupported or unavailable in this system)converter_mapr   )array
src_flavor
dst_flavorconvkeyconvfuncs        r   array_of_flavor2r   _   sY     :&Gm##k'ZZ5 6 6 	6 W%H8E??r   c                     	 t          | ||          S # t          $ r7}t          j        |j        d         d|dt
                     | cY d}~S d}~ww xY w)a  Get a version of the given `array` in a different flavor.

    The input `array` must be of the given `src_flavor`, and the
    returned array will be of the indicated `dst_flavor` (see below
    for an exception to this).  Both flavors may be the same, but it
    is not guaranteed that the returned array will be the same object
    as the input one in this case.

    If the conversion is not supported, a `FlavorWarning` is issued
    and the input `array` is returned as is.

    r   z; returning an object of the ``z`` flavor insteadN)r   r   warningswarnargsr   )r   r   r   fes       r   flavor_to_flavorr#   v   sy    z:>>>   ZZZ12?	A 	A 	As    
A,A	AAc                 .    t          | t          |          S )zGet a version of the given `array` in a different `dst_flavor`.

    The input `array` must be of the internal flavor, and the returned
    array will be of the given `dst_flavor`.  See `flavor_to_flavor()`
    for more information.

    )r#   internal_flavorr   r   s     r   internal_to_flavorr'      s     E?J???r   c                 .    t          | |t                    S )zGet a version of the given `array` in the internal flavor.

    The input `array` must be of the given `src_flavor`, and the
    returned array will be of the internal flavor.

    If the conversion is not supported, a ``FlavorError`` is raised.

    )r   r%   )r   r   s     r   array_as_internalr)      s     E:???r   c                     t           D ]}t          |         |           r|c S t          |           j        }d                    d t           D                       }t          d|d|          )zIdentify the flavor of a given `array`.

    If the `array` can not be matched with any flavor, a ``TypeError``
    is raised.

    z; c              3   0   K   | ]}t           |         V  d S r
   description_map)r   fls     r   r   zflavor_of.<locals>.<genexpr>   s'      JJ 3JJJJJJr   zobjects of type ``zD`` are not supported in this context, sorry; supported objects are: )r   identifier_maptype__name__r   	TypeError)r   r   	type_namesupported_descss       r   	flavor_ofr5      s       &!%(( 	MMM	U$IiiJJkJJJJJO
)'0yy//	CD D Dr   c                 >    t          | t          |           |          S )zGet a version of the given `array` in a different `dst_flavor`.

    The flavor of the input `array` is guessed, and the returned array
    will be of the given `dst_flavor`.

    If the conversion is not supported, a ``FlavorError`` is raised.

    )r   r5   r&   s     r   array_of_flavorr7      s     E9U#3#3Z@@@r   pythonc                     t          t                    t          |           z
  t          hz
  }|D ]}t          |           dS )a&  Disable all flavors except those in keep.

    Providing an empty keep sequence implies disabling all flavors (but the
    internal one).  If the sequence is not specified, only optional flavors are
    disabled.

    .. important:: Once you disable a flavor, it can not be enabled again.

    N)setr   r%   _disable_flavor)keepremover   s      r   restrict_flavorsr?      sN     D		)_,==F       r   r9   c                  \    t           D ]#} t          d| z            }|D ]}| t          |<   $dS )z(Register aliases of *available* flavors.z_%s_aliasesN)r   eval	alias_map)r   aliasesaliass      r   _register_aliasesrE      sP      & &}v-.. 	& 	&E%Ie	&& &r   c                  N    t           D ]} t          d| z            t          | <   dS )z-Register descriptions of *available* flavors.z_%s_descN)r   rA   r-   r   s    r   _register_descriptionsrH      s5     < <"&zF':";";< <r   c                  N    t           D ]} t          d| z            t          | <   dS )z5Register identifier functions of *available* flavors.z_is_%sN)r   rA   r/   rG   s    r   _register_identifiersrJ      s7      9 9!%h&7!8!8v9 9r   c                      d } t           D ]K}t           D ]A}d}	 t          d| d|           }n# t          $ r ||k    r| }Y nw xY w|r|t          ||f<   BLdS )z9Register converter functions between *available* flavors.c                     | S r
   r   r   s    r   identityz&_register_converters.<locals>.identity   s    r   N_conv__to_)r   rA   	NameErrorr   )rN   r   r   r   s       r   _register_convertersrR      s      ! C C
% 	C 	CJ
 H( E E E E EFF ( ( (++'H(  C:Bz:67	CC Cs   /AAc                  v    t                       t                       t                       t                       dS )z!Register all *available* flavors.N)rE   rH   rJ   rR   r   r   r   _register_allrT   	  s:     r   c                     g }t                                           D ] \  }}|| k    r|                    |           !|D ]
}t           |= dS )z3Deregister aliases of a given `flavor` (no checks).N)rB   itemsappend)r   
rm_aliasesan_aliasa_flavors       r   _deregister_aliasesr[     sk     J ) 1 1 ( (8vh'''    h   r   c                     t           | = dS )z7Deregister description of a given `flavor` (no checks).Nr,   rG   s    r   _deregister_descriptionr]     s     	r   c                     t           | = dS )z?Deregister identifier function of a given `flavor` (no checks).N)r/   rG   s    r   _deregister_identifierr_   #  s     	vr   c                 j    g }t           D ]}| |v r|                    |           |D ]
}t           |= dS )z?Deregister converter functions of a given `flavor` (no checks).N)r   rW   )r   rm_flavor_pairsflavor_pairs      r   _deregister_convertersrc   )  s\     O$ 0 0[  "";///& ' '+&&' 'r   c                     t          |            t          |            t          |            t          |            t                              |            dS )z2Completely disable the given `flavor` (no checks).N)r[   r]   r_   rc   r   r>   rG   s    r   r<   r<   4  sV     F###6"""6"""vr   )ListTupleIntFloatStringVLStringObjectz;homogeneous list or tuple, integer, float, complex or bytesc           	      j    t          | t          t          t          t          t
          t          f          S r
   )
isinstancetuplelistintfloatcomplexbytesrM   s    r   
_is_pythonrt   H  s    eeT3wFGGGr   zNumPy array, record or scalarz1.19.0c                     t          j                    5  t          j        d           	 t          j        | g|R i |} n"# t          j        $ r t          d          w xY w	 d d d            n# 1 swxY w Y   | S )Nerrorz-cannot guess the desired dtype from the input)r   catch_warningssimplefilternpr   VisibleDeprecationWarning
ValueError)r   r!   kwargss      r   toarrayr}   Q  s    $&& 	E 	E!'***E888888/ E E E CE E EE 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E s'   A/A A/ AA//A36A3c                 N    t          | t          j        t          j        f          S r
   )rm   ry   ndarraygenericrM   s    r   	_is_numpyr   _  s    ebj"*5666r   c                 B      fd} j         |_          j        |_        |S )zDecorate `convfunc` to return a *contiguous* NumPy array.

    Note: When arrays are 0-strided, the copy is avoided.  This allows
    to use `array` to still carry info about the dtype and shape.
    c                      |           }t          |d          r8|j        j        s,t          |j                  dk    r|                                }|S )Nflagsr   )hasattrr   
contiguoussumstridescopy)r   nparrr   s     r   conv_to_numpyz(_numpy_contiguous.<locals>.conv_to_numpyj  sV    E7## 	!K*	!EM""a''JJLLEr   )r1   __doc__)r   r   s   ` r   _numpy_contiguousr   c  s:         &.M$,Mr   c                     t          j        |           }|j        j        dk    r&	 |                    d          S # t
          $ r Y nw xY w|S NUS)ry   asarraydtypekindastypeUnicodeEncodeErrorr   r   s     r   _conv_numpy_to_numpyr   v  sa     JuE{3	<<$$$! 	 	 	D	 Ls   ; 
AAc                     t          |           }|j        j        dk    r&	 |                    d          S # t          $ r Y nw xY w|S r   )r}   r   r   r   r   r   s     r   _conv_python_to_numpyr     s[    ENNE{3	<<$$$! 	 	 	D	 Ls   6 
AAc                 n    | j         dk    r|                                 } n|                                 } | S )Nr   )shapetolistitemrM   s    r   _conv_numpy_to_pythonr     s2    {b 

Lr   c                  6    ddl } |                                  dS )zRun ``doctest`` on this module.r   N)doctesttestmod)r   s    r   _testr     s#     NNNOOr   __main__)r8   )2r   r   r   ry   
exceptionsr   r   __docformat__r%   r   rB   r-   r/   r   r   r   r#   r'   r)   r5   r7   r?   rW   rE   rH   rJ   rR   rT   r[   r]   r_   rc   r<   _python_aliases_python_descrt   _numpy_aliases_numpy_desclibNumpyVersion__version__r}   r   r   r   r   r   r   r   r1   r   r   r   <module>r      s  " "H      2 2 2 2 2 2 2 2 # 9 -  0	 ? D ) ) )  .  ,	@ 	@ 	@
@ 
@ 
@D D D$
A 
A 
A       *   7      8   & & &< < <9 9 9C C C*              ' ' '    
3H H H - 6r~&&"&*=*=h*G*GGG	 	 	 	 hG7 7 7  &             z	EGGGGG r   