
    d$                     N    d Z ddlZddlmZmZ ddlmZ dZ G d de          Z	dS )	z!Here is defined the EArray class.    N   )convert_to_np_atom2SizeType)CArrayz1.4c                   H     e Zd ZdZdZ	 	 	 	 d fd	Zd Zd Zd	 Zd
 Z	 xZ
S )EArraya  This class represents extendable, homogeneous datasets in an HDF5 file.

    The main difference between an EArray and a CArray (see
    :ref:`CArrayClassDescr`), from which it inherits, is that the former
    can be enlarged along one of its dimensions, the *enlargeable
    dimension*.  That means that the :attr:`Leaf.extdim` attribute (see
    :class:`Leaf`) of any EArray instance will always be non-negative.
    Multiple enlargeable dimensions might be supported in the future.

    New rows can be added to the end of an enlargeable array by using the
    :meth:`EArray.append` method.

    Parameters
    ----------
    parentnode
        The parent :class:`Group` object.

        .. versionchanged:: 3.0
           Renamed from *parentNode* to *parentnode*.

    name : str
        The name of this node in its parent group.

    atom
        An `Atom` instance representing the *type* and *shape*
        of the atomic objects to be saved.

    shape
        The shape of the new array.  One (and only one) of
        the shape dimensions *must* be 0.  The dimension being 0
        means that the resulting `EArray` object can be extended
        along it.  Multiple enlargeable dimensions are not supported
        right now.

    title
        A description for this node (it sets the ``TITLE``
        HDF5 attribute on disk).

    filters
        An instance of the `Filters` class that provides information
        about the desired I/O filters to be applied during the life
        of this object.

    expectedrows
        A user estimate about the number of row elements that will
        be added to the growable dimension in the `EArray` node.
        If not provided, the default value is ``EXPECTED_ROWS_EARRAY``
        (see ``tables/parameters.py``).  If you plan to create either
        a much smaller or a much bigger `EArray` try providing a guess;
        this will optimize the HDF5 B-Tree creation and management
        process time and the amount of memory used.

    chunkshape
        The shape of the data chunk to be read or written in a single
        HDF5 I/O operation.  Filters are applied to those chunks of data.
        The dimensionality of `chunkshape` must be the same as that of
        `shape` (beware: no dimension should be 0 this time!).
        If ``None``, a sensible value is calculated based on the
        `expectedrows` parameter (which is recommended).

    byteorder
        The byteorder of the data *on disk*, specified as 'little' or
        'big'. If this is not specified, the byteorder is that of the
        platform.

    track_times
        Whether time data associated with the leaf are recorded (object
        access time, raw data modification time, metadata change time, object
        birth time); default True.  Semantics of these times depend on their
        implementation in the HDF5 library: refer to documentation of the
        H5O_info_t data structure.  As of HDF5 1.8.15, only ctime (metadata
        change time) is implemented.

        .. versionadded:: 3.4.3

    Examples
    --------

    See below a small example of the use of the `EArray` class.  The
    code is available in ``examples/earray1.py``::

        import numpy as np
        import tables as tb

        fileh = tb.open_file('earray1.h5', mode='w')
        a = tb.StringAtom(itemsize=8)

        # Use ``a`` as the object type for the enlargeable array.
        array_c = fileh.create_earray(fileh.root, 'array_c', a, (0,),
                                      "Chars")
        array_c.append(np.array(['a'*2, 'b'*4], dtype='S8'))
        array_c.append(np.array(['a'*6, 'b'*8, 'c'*10], dtype='S8'))

        # Read the string ``EArray`` we have created on disk.
        for s in array_c:
            print('array_c[%s] => %r' % (array_c.nrow, s))
        # Close the file.
        fileh.close()

    The output for the previous script is something like::

        array_c[0] => 'aa'
        array_c[1] => 'bbbb'
        array_c[2] => 'aaaaaa'
        array_c[3] => 'bbbbbbbb'
        array_c[4] => 'cccccccc'

    EARRAYN Tc                     ||j         j        d         }|| _        	 t                                          ||||||||	|
|
  
         d S )NEXPECTED_ROWS_EARRAY)_v_fileparams_v_expectedrowssuper__init__)self
parentnodenameatomshapetitlefiltersexpectedrows
chunkshape	byteorder_logtrack_times	__class__s               -lib/python3.11/site-packages/tables/earray.pyr   zEArray.__init__   si     %-45KLL+D 	T4w#Yk	C 	C 	C 	C 	C    c                 B   t          j        t          j        | j                  dk              }|dk    rB|dk    r-t	          | j                                      d          | _        nt          d          t          d          | 	                    | j
                  S )z+Create a new array in file (specific part).r   r   z6Multiple enlargeable (0-)dimensions are not supported.zZWhen creating EArrays, you need to set one of the dimensions of the Atom instance to zero.)npsumarrayr   listindexextdimNotImplementedError
ValueError_g_create_commonr   )r   zerodimss     r   	_g_createzEArray._g_create   s     6"(4:..!344a<<1}}"4:..44Q77)!" " " ?@ @ @
 $$T%9:::r    c                 n   t          | j                  }t          |j                  t          | j        j                  z
  }||k    rt          d|| j        |fz            t          |          D ]B}|| j        k    r5| j        |         |j        |         k    rt          d| j        |fz            CdS )z;Test that nparr shape is consistent with underlying EArray.zGthe ranks of the appended object (%d) and the ``%s`` EArray (%d) differz^the shapes of the appended object and the ``%s`` EArray differ in non-enlargeable dimension %dN)lenr   r   r)   _v_pathnameranger'   )r   nparrmyranknarankis        r   _check_shape_appendzEArray._check_shape_append   s     TZU[!!C	$8$88V : &(8&AB C C C v 	K 	KADKDJqMU[^$C$C  #1595Eq4I"J K K K	K 	Kr    c                     |                                   | j                                         t          || j                  }|                     |           |j        dk    r|                     |           dS dS )a  Add a sequence of data to the end of the dataset.

        The sequence must have the same type as the array; otherwise a
        TypeError is raised. In the same way, the dimensions of the
        sequence must conform to the shape of the array, that is, all
        dimensions must match, with the exception of the enlargeable
        dimension, which can be of any length (even 0!).  If the shape
        of the sequence is invalid, a ValueError is raised.

        r   N)_g_check_openr   _check_writabler   r   r5   size_append)r   sequencer1   s      r   appendzEArray.append   s{     	$$&&& $Hdi88  ''':>>LL >r    c
                 |   |                      |||          \  }}}| j        }t          | j                  }d||<   t	          t          |||                    }t          ||| j        ||||||		  	        }| j        }d | j        D             }d| _	        t          ||||z            D ][}|||z  z   }||k    r|}t          |||          ||<   |                    |                     t          |                               \d| _	        t          j        | j        t                     | j        j        z  }||fS )z2Private part of Leaf.copy() for each kind of leaf.r   )r   r   r   r   r   r   r   c                 0    g | ]}t          d |d          S )r   r   )slice).0dims     r   
<listcomp>z-EArray._g_copy_with_stats.<locals>.<listcomp>   s$    999s%3""999r    FT)dtype)_process_range_readmaindimr%   r   r.   r0   r   r   
nrowsinbuf
_v_convertr?   r:   __getitem__tupler"   prodr   itemsize)r   groupr   startstopstepr   r   r   r   kwargsrE   r   nrowsobjectrF   slicesstart2stop2nbytess                       r   _g_copy_with_statszEArray._g_copy_with_stats   sY    #66udDIId,TZ  gE%t,,--4diuE%J  
 _
99dj999  E4
):;; 	< 	<FTJ..Et||#FE488F7ONN4++E&MM::;;;;8444ty7IIr    )	NNr
   NNNNTT)__name__
__module____qualname____doc__
_c_classidr   r,   r5   r<   rW   __classcell__)r   s   @r   r   r      s        k k\ J /1,0,0(,	C C C C C C ; ; ;(K K K      .$  $  $  $  $  $  $ r    r   )
r[   numpyr"   utilsr   r   carrayr   	obversionr    r    r   <module>rc      s    ' '     0 0 0 0 0 0 0 0       	`  `  `  `  ` V `  `  `  `  ` r    