
    dFp                         d Z ddlZddlZddlZddlZddlmZmZm	Z	m
Z ddlmZ ddlmZ ddlmZmZmZ ddlmZ d	 Zd
 Zd Z G d de          ZdS )zHere is defined the Leaf class.    N   )check_flavorinternal_flavortoarray	alias_map)Node)Filters)
byteorderslazyattrSizeType)PerformanceWarningc                 T    d}|t          dt          j        |           z            z  S )z,Return the fitted chunksize for expected_mb.i       )intmathlog10)expected_mbbasesizes     +lib/python3.11/site-packages/tables/leaf.py	csformular      s+     Hc!TZ4445555    c                 (    | dk     rd} n| dk    rd} | S )z:Protection against creating too small or too large chunks.r   i  )r   s    r   limit_esr      s*     Q	u		r   c                     t          |           } t          t          j        |                     }d|z  } t	          |           }|dz  S )a  Compute the optimum HDF5 chunksize for I/O purposes.

    Rational: HDF5 takes the data in bunches of chunksize length to
    write the on disk. A BTree in memory is used to map structures on
    disk. The more chunks that are allocated for a dataset the larger
    the B-tree. Large B-trees take memory and causes file storage
    overhead as well as more disk I/O and higher contention for the meta
    data cache.  You have to balance between memory and I/O overhead
    (small B-trees) and time to access to data (big B-trees).

    The tuning of the chunksize parameter affects the performance and
    the memory consumed. This is based on my own experiments and, as
    always, your mileage may vary.

    
      )r   r   r   r   r   )r   zone	chunksizes      r   calc_chunksizer    %   sH    " ;''Ktz+&&''Dd(K+&&Iq=r   c                       e Zd ZdZej        Z	 ej        Z	 e	d             Z
e	d             Ze	d             Ze	d             Zed             Ze	d             Ze	d             Ze	d	             Zej        d
             Zej        d             Ze	d             Z	 	 	 d' fd	Zd Zd Z fdZd Zd Zd(dZd)dZd)dZd Zd Z d Z!d Z"	 	 d*dZ#	 	 d*dZ$d Z%d  Z&d! Z'd" Z(d# Z)d$ Z*d) fd%	Z+d)d&Z, xZ-S )+Leafa  Abstract base class for all PyTables leaves.

    A leaf is a node (see the Node class in :class:`Node`) which hangs from a
    group (see the Group class in :class:`Group`) but, unlike a group, it can
    not have any further children below it (i.e. it is an end node).

    This definition includes all nodes which contain actual data (datasets
    handled by the Table - see :ref:`TableClassDescr`, Array -
    see :ref:`ArrayClassDescr`, CArray - see :ref:`CArrayClassDescr`, EArray -
    see :ref:`EArrayClassDescr`, and VLArray - see :ref:`VLArrayClassDescr`
    classes) and unsupported nodes (the UnImplemented
    class - :ref:`UnImplementedClassDescr`) these classes do in fact inherit
    from Leaf.


    .. rubric:: Leaf attributes

    These instance variables are provided in addition to those in Node
    (see :ref:`NodeClassDescr`):

    .. attribute:: byteorder

        The byte ordering of the leaf data *on disk*.  It will be either
        ``little`` or ``big``.

    .. attribute:: dtype

        The NumPy dtype that most closely matches this leaf type.

    .. attribute:: extdim

        The index of the enlargeable dimension (-1 if none).

    .. attribute:: nrows

        The length of the main dimension of the leaf data.

    .. attribute:: nrowsinbuf

        The number of rows that fit in internal input buffers.

        You can change this to fine-tune the speed or memory
        requirements of your application.

    .. attribute:: shape

        The shape of data in the leaf.

    c                     | j         S )zmThe name of this node in its parent group (This is an
        easier-to-write alias of :attr:`Node._v_name`).)_v_nameselfs    r   namez	Leaf.namey   s     |r   c                 $    t          | dd          S )zThe HDF5 chunk size for chunked leaves (a tuple).

        This is read-only because you cannot change the chunk size of a
        leaf once it has been created.
        _v_chunkshapeN)getattrr%   s    r   
chunkshapezLeaf.chunkshape   s     t_d333r   c                     | j         S )zA node identifier, which may change from run to run.
        (This is an easier-to-write alias of :attr:`Node._v_objectid`).

        .. versionchanged:: 3.0
           The *objectID* property has been renamed into *object_id*.

        )_v_objectidr%   s    r   	object_idzLeaf.object_id   s     r   c                 *    t          | j                  S )zHThe number of dimensions of the leaf data.

        .. versionadded: 2.4)lenshaper%   s    r   ndimz	Leaf.ndim   s    
 4:r   c                 *    t          j        |           S )z]Filter properties for this leaf.

        See Also
        --------
        Filters

        )r	   
_from_leafr%   s    r   filterszLeaf.filters   s     !$'''r   c                 *    |                                  S )a  Whether timestamps for the leaf are recorded

        If the leaf is not a dataset, this will fail with HDF5ExtError.

        The track times dataset creation property does not seem to
        survive closing and reopening as of HDF5 1.8.17.  Currently,
        it may be more accurate to test whether the ctime for the
        dataset is 0:
        track_times = (leaf._get_obj_timestamps().ctime == 0)
        )_get_obj_track_timesr%   s    r   track_timeszLeaf.track_times   s     ((***r   c                 *    | j         dk     rdS | j         S )zThe dimension along which iterators work.

        Its value is 0 (i.e. the first dimension) when the dataset is not
        extendable, and self.extdim (where available) for extendable ones.
        r   )extdimr%   s    r   maindimzLeaf.maindim   s     ;??1{r   c                     | j         S )a/  The type of data object read from this leaf.

        It can be any of 'numpy' or 'python'.

        You can (and are encouraged to) use this property to get, set
        and delete the FLAVOR HDF5 attribute of the leaf. When the leaf
        has no such attribute, the default flavor is used..
        )_flavorr%   s    r   flavorzLeaf.flavor   s     |r   c                 |    | j                                          t          |           |x| j        _        | _        d S N)_v_file_check_writabler   _v_attrsFLAVORr=   )r&   r>   s     r   r>   zLeaf.flavor   s9    $$&&&V.44t|||r   c                 ,    | j         `t          | _        d S r@   )rC   rD   r   r=   r%   s    r   r>   zLeaf.flavor   s    M &r   c                 *    |                                  S )a%  
        The size of this leaf's data in bytes as it is stored on disk.  If the
        data is compressed, this shows the compressed size.  In the case of
        uncompressed, chunked data, this may be slightly larger than the amount
        of data, due to partially filled chunks.
        )_get_storage_sizer%   s    r   size_on_diskzLeaf.size_on_disk   s     %%'''r   FNTc                     || _         	 d | _        	 d | _        	 |r1||j        }|| j        d<   |dvrt          d|z            || _        	 || _        t                      	                    |||           d S )Nr5   )NlittlebigzGthe byteorder can only take 'little' or 'big' values and you passed: %s)
_v_new
nrowsinbufr=   
_v_filters__dict__
ValueError	byteorder_want_track_timessuper__init__)	r&   
parentnoder'   newr5   rQ   _logr8   	__class__s	           r   rT   zLeaf.__init__   s     ?	 8 	@$/'.DM)$ 777 )+456 6 6 'DN?!,
 	T400000r   c                     | j         S )a.  Return the length of the main dimension of the leaf data.

        Please note that this may raise an OverflowError on 32-bit platforms
        for datasets having more than 2**31-1 rows.  This is a limitation of
        Python that you can work around by using the nrows or shape attributes.

        )nrowsr%   s    r   __len__zLeaf.__len__  s     zr   c                    g }| j         j        r|                    d           | j         j        rq| j         j        r|                    d           | j         j        r|                    d           |                    | j         j         d| j         j         d           | j         d| j        j	         | j
         d                    |           d| j        S )	zxThe string representation for this object is its pathname in the
        HDF5 object tree plus some additional metainfo.
fletcher32shuffle
bitshuffle()z (z, z) )r5   r]   append	complevelr^   r_   complib_v_pathnamerX   __name__r1   join_v_title)r&   r5   s     r   __str__zLeaf.__str__  s     <" 	)NN<(((<! 	P|# *y)))|& -|,,,NNdl2NNT\5KNNNOOO# G Gt~'> G:G#yy11G G59]G G 	Hr   c                    t                                                       | j        rK| j        t          | _        dS | j        j        d         r"| j                            d| j                   dS dS | j        j        d         r7t          | j        dt                    }t          j        ||          | _        dS t          | _        dS )zCode to be run after node creation and before creation logging.

        This method gets or sets the flavor of the leaf.

        NPYTABLES_SYS_ATTRSrD   )rS   _g_post_init_hookrL   r=   r   rA   paramsrC   _g__setattrr*   flavor_alias_mapget)r&   r>   rX   s     r   rl   zLeaf._g_post_init_hook!  s     	!!###; 	/|#.<&';< FM--hEEEEEF F |"#78 / /JJ/3FFCC.r   c                 H   | j         dk    rt          d          fS d}||z  |z  }t          |          }| j        j        }||                    d          r| j        dk    r}|| _        |dz  }t          j	                    }d|v rR|d         }	|
                    d	d
          }
t          |	          t          u rt          |
          t          u r|	|
k    r|	}|dk    rd}| j        }||z  }|dk    rd}t          | j                   }d||<   t          j        |t                    }||k    r	||z  ||<   nTt#          t%          |                    D ]2}d||<   t          j        |t                    }||k    r
||z  ||<    n3||d<   t'          d |D                       S )z'Calculate the shape for the HDF5 chunk.r   r   i   Nblosc2TABLE   l3_cache_sizel2_cache_sizez	Not foundir   dtypec              3   4   K   | ]}t          |          V  d S r@   )r   ).0ss     r   	<genexpr>z(Leaf._calc_chunkshape.<locals>.<genexpr>|  s(      55QXa[[555555r   )r1   r   r    r5   rd   
startswith
_c_classid_v_blocksizecpuinfoget_cpu_inforp   typer   r;   listnpprodranger0   tuple)r&   expectedrowsrowsizeitemsizeMBr   r   rd   cpu_inforu   rv   r;   chunknitemsr+   newchunknitemsjs                   r   _calc_chunkshapezLeaf._calc_chunkshape6  s    :QKK>! #g-"4";//	,&x((  Ow&& !*DOI+--H(** ( 9 !)_k J J''3..''3..!M11 -I:%%&	,8+!K$*%%

78<<<[(("-"?Jw 3z??++ 
- 
- !
1!#8!D!D!D![00$/>$AJqME 1 "-
255*555555r   c                     | j         j        }| j        }|d         }|dk    r||z  }nd}|dk    r7d}|d         |z  }||k    r$t          j        d| j        |fz  t                     |S )z<Calculate the number of rows that fits on a PyTables buffer.IO_BUFFER_SIZEr   r   BUFFER_TIMESa  The Leaf ``%s`` is exceeding the maximum recommended rowsize (%d bytes);
be ready to see PyTables asking for *lots* of memory and possibly slow
I/O.  You may want to reduce the rowsize by trimming the value of
dimensions that are orthogonal (and preferably close) to the *main*
dimension of this leave.  Alternatively, in case you have specified a
very small/large chunksize, you may want to increase/decrease it.)rA   rm   r   warningswarnre   r   )r&   rm   r   
buffersizerM   
maxrowsizes         r   _calc_nrowsinbufzLeaf._calc_nrowsinbuf~  s     $,,-
a<<#w.JJJ ??J/*<J## E "&!1: >? 12 2 2 r   c                     || j         }n| j        |         }|r|r|dk     rt          d          t          |||                              t          |                    S )Nr   zslice step cannot be negative)rZ   r1   rP   sliceindicesr   )r&   startstopstepdimwarn_negsteprZ   s          r   _process_rangezLeaf._process_range  si    ;JEEJsOE 	>D 	>TAXX<=== UD$''//E

;;;r   c                     | j         }|:|8|6d|cxk     r|k    rn nt          d|d|d          d}|dk    r|}n|dz   }|                     ||||          \  }}}|||fS )Nr   zstart of range (z") is greater than number of rows (ra   r   ry   )r   )rZ   
IndexErrorr   )r&   r   r   r   r   rZ   s         r   _process_range_readzLeaf._process_range_read  s    
$, 5!!!!E!!!!! j:?%%"H I I ID{{qy //tT=I 0 K KtTtT""r   c                     |                     dd           }|                     dd           }|                     dd           }|                     d| j                  }	|                     d| j                  }
|                     d| j                  }|                     dd          }|                     d	d           }|d
k    r| j        }n|dk    rd }|	| j        }	|
| j        }
 | j        ||||||	|
||f	i |\  }}|r"| j                            |j        d           n;d| j        v r2| j        j        d         r |j        	                    d| j
                   | j
        |_
        | |dxx         dz  cc<   |dxx         |z  cc<   |S )Nr   r   r   titler5   r+   copyuserattrsTstatskeepauto)	copyclassrD   rk   leavesr   bytes)poprh   r5   r+   _g_copy_with_statsrC   _g_copyrA   rm   rn   r=   )r&   	newparentnewname	recursiverW   kwargsr   r   r   r   r5   r+   r   r   new_noder   s                   r   r   zLeaf._g_copy  s   

7D))zz&$''zz&$''

7DM22**Y55ZZdo>>


?D99

7D))JJ6!!J =ME?lG 4D3wtT7J8 8068 85
  	FM!!("3t!DDDD&&|"#78 F!--hEEE< (OOOq OOO'NNNe#NNNr   c                     t           |         }| j        || _        |dv rA|| j        k    r5|j        j        s|                                }|                    d           nd| _        |S )z1Fix the byteorder of data passed in constructors.N)rJ   rK   T
irrelevant)r
   rQ   flags	writeablecopybyteswap)r&   data
dbyteorders      r   _g_fix_byteorder_datazLeaf._g_fix_byteorder_data  ss    
+
 >!'DN ***T^++z+ '99;;Dd### *DNr   c                    |}t          |          t          t          fv r}t          |t                    r7t	          |          t	          | j                  k    rt          d|          	 t          |          }nL# t          $ r t          d|          w xY wt          |t          j                  st          d|          t	          |          dk    rt          j        g d          S |j        j        dk    r]|j        | j        k    st          d          t          j        |                                d          }t          j        |          }nh|j        j        dk    s|j        j        dk    r8t	          |j                  d	k    rt          d
          t	          |j                  d	k    r]|j        d         t	          | j                  k    rt          d
          t          j        |d          }t          j        |          }nt          j        |d          }|j        |n|j        }||u r|                                }|dk     }|| j        z   |         ||<   t          j        |dk               st          j        || j        k              rt          d          nt          d          |j        j        s|                                }|S )a  Perform a point-wise selection.

        `key` can be any of the following items:

        * A boolean array with the same shape than self. Those positions
          with True values will signal the coordinates to be returned.

        * A numpy array (or list or tuple) with the point coordinates.
          This has to be a two-dimensional array of size len(self.shape)
          by num_elements containing a list of of zero-based values
          specifying the coordinates in the dataset of the selected
          elements. The order of the element coordinates in the array
          specifies the order in which the array elements are iterated
          through when I/O is performed. Duplicate coordinate locations
          are not checked for.

        Return the coordinates array.  If this is not possible, raise a
        `TypeError` so that the next selection method can be tried out.

        This is useful for whatever `Leaf` instance implementing a
        point-wise selection.

        zInvalid index or slice: r   i8rw   bz-Boolean indexing array has incompatible shapeiur   z0Coordinate indexing array has incompatible shapeNzIndex out of boundsz!Only integer coordinates allowed.)r   r   r   
isinstancer0   r1   r   r   rP   	TypeErrorr   ndarrayarrayrx   kindasarraynonzero	transposebaser   anyr   
contiguous)r&   key	input_keycoordsr   idxs         r   _point_selectionzLeaf._point_selection  s   0 	99u%%#u%% E#c((S__*D*D !CC!C!CDDDDcll D D D B3 B BCCCDC,, 	@>s>>??? s88q==8Bd++++9>S  9
** CE E E ZT:::F\&))FFY^s""cin&;&;39~~!! FH H HSY1$$9Q<3tz??22$JL L LCt444f-- Ct444 $[066fkDy  1*C!DJ.4F3K vfqj!! 8RVFdj,@%A%A 8 !67778 ?@@@|& 	#[[]]Fs   -A= =Bc                 0    |                      d           dS )zRemove this node from the hierarchy.

        This method has the behavior described
        in :meth:`Node._f_remove`. Please note that there is no recursive flag
        since leaves do not have child nodes.

        FN)	_f_remover%   s    r   removezLeaf.removeQ  s     	ur   c                 0    |                      |           dS )zqRename this node in place.

        This method has the behavior described in :meth:`Node._f_rename()`.

        N)	_f_rename)r&   r   s     r   renamezLeaf.rename\  s     	wr   c                 6    |                      ||||           dS )zkMove or rename this node.

        This method has the behavior described in :meth:`Node._f_move`

        N)_f_move)r&   r   r   	overwritecreateparentss        r   movez	Leaf.movee  s"     	YMBBBBBr   c                 $     | j         ||||fi |S )a{  Copy this node and return the new one.

        This method has the behavior described in :meth:`Node._f_copy`. Please
        note that there is no recursive flag since leaves do not have child
        nodes.

        .. warning::

            Note that unknown parameters passed to this method will be
            ignored, so may want to double check the spelling of these
            (i.e. if you write them incorrectly, they will most probably
            be ignored).

        Parameters
        ----------
        title
            The new title for the destination. If omitted or None, the original
            title is used.
        filters : Filters
            Specifying this parameter overrides the original filter properties
            in the source node. If specified, it must be an instance of the
            Filters class (see :ref:`FiltersClassDescr`). The default is to
            copy the filter properties from the source node.
        copyuserattrs
            You can prevent the user attributes from being copied by setting
            this parameter to False. The default is to copy them.
        start, stop, step : int
            Specify the range of rows to be copied; the default is to copy all
            the rows.
        stats
            This argument may be used to collect statistics on the copy
            process. When used, it should be a dictionary with keys 'groups',
            'leaves' and 'bytes' having a numeric value. Their values will be
            incremented to reflect the number of groups, leaves and bytes,
            respectively, that have been copied during the operation.
        chunkshape
            The chunkshape of the new leaf.  It supports a couple of special
            values.  A value of keep means that the chunkshape will be the same
            than original leaf (this is the default).  A value of auto means
            that a new shape will be computed automatically in order to ensure
            best performance when accessing the dataset through the main
            dimension.  Any other value should be an integer or a tuple
            matching the dimensions of the leaf.

        )_f_copy)r&   r   r   r   r   r   s         r   r   z	Leaf.copyo  s6    ` t|w	=D D<BD D 	Dr   c                 d    | j         dk     rt          d          |                     |           dS )a  Truncate the main dimension to be size rows.

        If the main dimension previously was larger than this size, the extra
        data is lost.  If the main dimension previously was shorter, it is
        extended, and the extended part is filled with the default values.

        The truncation operation can only be applied to *enlargeable* datasets,
        else a TypeError will be raised.

        r   z,non-enlargeable datasets cannot be truncatedN)r:   r   _g_truncate)r&   sizes     r   truncatezLeaf.truncate  s8     ;??JKKKr   c                 *    |                                  S )zoIs this node visible?

        This method has the behavior described in :meth:`Node._f_isvisible()`.

        )_f_isvisibler%   s    r   	isvisiblezLeaf.isvisible  s       """r   c                 ,    |                      |          S )z~Get a PyTables attribute from this node.

        This method has the behavior described in :meth:`Node._f_getattr`.

        )
_f_getattrr&   r'   s     r   get_attrzLeaf.get_attr  s     t$$$r   c                 2    |                      ||           dS )zSet a PyTables attribute for this node.

        This method has the behavior described in :meth:`Node._f_setattr()`.

        N)
_f_setattr)r&   r'   values      r   set_attrzLeaf.set_attr  s     	e$$$$$r   c                 0    |                      |           dS )zDelete a PyTables attribute from this node.

        This method has the behavior described in :meth:`Node_f_delAttr`.

        N)
_f_delattrr   s     r   del_attrzLeaf.del_attr  s     	r   c                 .    |                                   dS )a.  Flush pending data to disk.

        Saves whatever remaining buffered data to disk. It also releases
        I/O buffers, so if you are filling many datasets in the same
        PyTables session, please call flush() extensively so as to help
        PyTables to keep memory requirements low.

        N)_g_flushr%   s    r   flushz
Leaf.flush  s     	r   c                     | j         sdS |r$t          | d          r|                                  |                                  t	                                                       dS )zClose this node in the tree.

        This method has the behavior described in :meth:`Node._f_close`.
        Besides that, the optional argument flush tells whether to flush
        pending data to disk or not before closing.

        N_v_iobuf)	_v_isopenhasattrr   _g_closerS   _f_close)r&   r   rX   s     r   r   zLeaf._f_close  si     ~ 	F  	WT:.. 	JJLLL 	 	r   c                 0    |                      |           dS )znClose this node in the tree.

        This method is completely equivalent to :meth:`Leaf._f_close`.

        N)r   )r&   r   s     r   closez
Leaf.close  s     	er   )FNNTT)NT)T)NNFF).rf   
__module____qualname____doc__r   rC   attrsrh   r   propertyr'   r+   r.   r2   r   r5   r8   r;   r>   setterdeleterrH   rT   r[   ri   rl   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   __classcell__)rX   s   @r   r"   r"   >   sC       0 0f MECMED   X
 4 4 X4     X    X 	( 	( X	( + + X+ 	 	 X	 
 
 X
 ]5 5 ]5
 ^' ' ^' ( ( X( %)&*!"1 "1 "1 "1 "1 "1H	 	 	H H H / / / / /*F6 F6 F6P  << < < < # # # #$' ' ' 'R  0O O Od	 	 	      ,0,1C C C C ,0,11D 1D 1D 1Df  "# # #% % %% % %  
 
 
     2       r   r"   )r  r   r   numpyr   r   r>   r   r   r   r   ro   noder   r5   r	   utilsr
   r   r   
exceptionsr   r   r   r    r"   r   r   r   <module>r     s-   % %       4 4 4 4 4 4 4 4 4 4 4 4             1 1 1 1 1 1 1 1 1 1 * * * * * *6 6 6    2G G G G G4 G G G G Gr   