import operator
import sys
+from typing import Any , Optional , Union , TYPE_CHECKING
+
import numpy as np
+import numpy.typing as npt
from . import hdf5extension
from .filters import Filters
@@ -108,6 +113,10 @@ Source code for tables.array
from .utils import ( is_idx , convert_to_np_atom2 , SizeType , lazyattr ,
byteorders , quantize )
+if TYPE_CHECKING :
+ from .atom import Atom , EnumAtom
+ from .group import Group
+ from .misc.enum import Enum
# default version for ARRAY objects
# obversion = "1.0" # initial version
@@ -115,7 +124,9 @@ Source code for tables.array
# obversion = "2.1" # Added support for complex datatypes
# obversion = "2.2" # This adds support for time datatypes.
# obversion = "2.3" # This adds support for enumerated datatypes.
-obversion = "2.4" # Numeric and numarray flavors are gone.
+obversion = "2.4" # Numeric and numarray flavors are gone.
+
+SelectionType = Union [ int , slice , list [ Union [ int , slice ]], npt . ArrayLike ]
[docs] class Array ( hdf5extension . Array , Leaf ):
@@ -157,7 +168,7 @@
Source code for tables.array
of the same type).
.. versionchanged:: 3.0
- Renamed form *object* into *obj*.
+ Renamed from *object* into *obj*.
title
A description for this node (it sets the ``TITLE`` HDF5 attribute on
disk).
@@ -180,12 +191,12 @@ Source code for tables.array
_c_classid = 'ARRAY'
@lazyattr
- def dtype ( self ):
+ def dtype ( self ) -> np . dtype :
"""The NumPy ``dtype`` that most closely matches this array."""
return self . atom . dtype
@property
- def nrows ( self ):
+ def nrows ( self ) -> int :
"""The number of rows in the array."""
if self . shape == ():
return SizeType ( 1 ) # scalar case
@@ -193,7 +204,7 @@ Source code for tables.array
return self . shape [ self . maindim ]
@property
- def rowsize ( self ):
+ def rowsize ( self ) -> int :
"""The size of the rows in bytes in dimensions orthogonal to
*maindim*."""
maindim = self . maindim
@@ -204,17 +215,22 @@ Source code for tables.array
return rowsize
@property
- def size_in_memory ( self ):
+ def size_in_memory ( self ) -> int :
"""The size of this array's data in bytes when it is fully loaded into
memory."""
return self . nrows * self . rowsize
- def __init__ ( self , parentnode , name ,
- obj = None , title = "" ,
- byteorder = None , _log = True , _atom = None ,
- track_times = True ):
-
- self . _v_version = None
+ def __init__ ( self ,
+ parentnode : "Group" ,
+ name : str ,
+ obj : Optional [ npt . ArrayLike ] = None ,
+ title : str = "" ,
+ byteorder : Optional [ str ] = None ,
+ _log : bool = True ,
+ _atom : Union [ "Atom" , "EnumAtom" , None ] = None ,
+ track_times : bool = True ) -> None :
+
+ self . _v_version : Optional [ str ] = None
"""The object version of this array."""
self . _v_new = new = obj is not None
"""Is this the first time the node has been created?"""
@@ -234,23 +250,23 @@ Source code for tables.array
"""Whether the ``Array`` object must be converted or not."""
# Miscellaneous iteration rubbish.
- self . _start = None
+ self . _start : Optional [ int ] = None
"""Starting row for the current iteration."""
- self . _stop = None
+ self . _stop : Optional [ int ] = None
"""Stopping row for the current iteration."""
- self . _step = None
+ self . _step : Optional [ int ] = None
"""Step size for the current iteration."""
- self . _nrowsread = None
+ self . _nrowsread : Optional [ int ] = None
"""Number of rows read up to the current state of iteration."""
- self . _startb = None
+ self . _startb : Optional [ int ] = None
"""Starting row for current buffer."""
- self . _stopb = None
+ self . _stopb : Optional [ int ] = None
"""Stopping row for current buffer. """
- self . _row = None
+ self . _row : Optional [ int ] = None
"""Current row in iterators (sentinel)."""
self . _init = False
"""Whether we are in the middle of an iteration or not (sentinel)."""
- self . listarr = None
+ self . listarr : Optional [ npt . ArrayLike ] = None
"""Current buffer in iterators."""
# Documented (*public*) attributes.
@@ -258,18 +274,18 @@ Source code for tables.array
"""An Atom (see :ref:`AtomClassDescr`) instance representing the *type*
and *shape* of the atomic objects to be saved.
"""
- self . shape = None
+ self . shape : Optional [ list [ int ]] = None
"""The shape of the stored array."""
- self . nrow = None
+ self . nrow : Optional [ int ] = None
"""On iterators, this is the index of the current row."""
- self . extdim = - 1 # ordinary arrays are not enlargeable
+ self . extdim = - 1 # ordinary arrays are not enlargeable
"""The index of the enlargeable dimension."""
# Ordinary arrays have no filters: leaf is created with default ones.
super () . __init__ ( parentnode , name , new , Filters (), byteorder , _log ,
track_times )
- def _g_create ( self ):
+ def _g_create ( self ) -> int :
"""Save a new array in file."""
self . _v_version = obversion
@@ -312,7 +328,7 @@ Source code for tables.array
return self . _v_objectid
- def _g_open ( self ):
+ def _g_open ( self ) -> int :
"""Get the metadata info for an array in file."""
( oid , self . atom , self . shape , self . _v_chunkshape ) = self . _open_array ()
@@ -321,7 +337,7 @@ Source code for tables.array
return oid
-[docs] def get_enum ( self ):
+
[docs] def get_enum ( self ) -> "Enum" :
"""Get the enumerated type associated with this array.
If this array is of an enumerated type, the corresponding Enum instance
@@ -336,7 +352,10 @@
Source code for tables.array
-
[docs] def iterrows ( self , start = None , stop = None , step = None ):
+
[docs] def iterrows ( self ,
+
start : Optional [ int ] = None ,
+
stop : Optional [ int ] = None ,
+
step : Optional [ int ] = None ) -> Union [ tuple , "Array" ]:
"""Iterate over the rows of the array.
This method returns an iterator yielding an object of the current
@@ -371,7 +390,7 @@
Source code for tables.array
self . _init_loop ()
return self
-
[docs] def __iter__ ( self ):
+
[docs] def __iter__ ( self ) -> "Array" :
"""Iterate over the rows of the array.
This is equivalent to calling :meth:`Array.iterrows` with default
@@ -399,16 +418,16 @@
Source code for tables.array
self . _init_loop ()
return self
-
def _init_loop ( self ):
+
def _init_loop ( self ) -> None :
"""Initialization for the __iter__ iterator."""
self . _nrowsread = self . _start
self . _startb = self . _start
-
self . _row = - 1 # Sentinel
+
self . _row = - 1 # Sentinel
self . _init = True # Sentinel
-
self . nrow = SizeType ( self . _start - self . _step ) # row number
+
self . nrow = SizeType ( self . _start - self . _step ) # row number
-
[docs] def __next__ ( self ):
+
[docs] def __next__ ( self ) -> Any :
"""Get the next element of the array during an iteration.
The element is returned as an object of the current flavor.
@@ -419,8 +438,8 @@
Source code for tables.array
# listarr buffer
if self . _nrowsread >= self . _stop :
self . _init = False
- self . listarr = None # fixes issue #308
- raise StopIteration # end of iteration
+ self . listarr = None # fixes issue #308
+ raise StopIteration # end of iteration
else :
# Read a chunk of rows
if self . _row + 1 >= self . nrowsinbuf or self . _row < 0 :
@@ -443,9 +462,11 @@ Source code for tables.array
if self . shape :
return self . listarr [ self . _row ]
else :
- return self . listarr # Scalar case
+ return self . listarr # Scalar case
-
def _interpret_indexing ( self , keys ):
+
def _interpret_indexing (
+
self , keys : SelectionType ,
+
) -> tuple [ np . ndarray , np . ndarray , np . ndarray , list [ int ]]:
"""Internal routine used by __getitem__ and __setitem__"""
maxlen = len ( self . shape )
@@ -512,7 +533,11 @@
Source code for tables.array
return startl , stopl , stepl , shape
- def _fancy_selection ( self , args ):
+ def _fancy_selection ( self , args : list [ Union [ int , list [ int ]]]) -> tuple [
+ list [ tuple [ int , int , int , int , str ]],
+ Optional [ tuple [ int , np . ndarray ]],
+ tuple [ int , ... ],
+ ]:
"""Performs a NumPy-style fancy selection in `self`.
Implements advanced NumPy-style selection operations in
@@ -526,7 +551,7 @@ Source code for tables.array
# Internal functions
- def validate_number ( num , length ):
+ def validate_number ( num : int , length : int ) -> None :
"""Validate a list member for the given axis length."""
try :
@@ -536,7 +561,7 @@ Source code for tables.array
if num > length - 1 :
raise IndexError ( "Index out of bounds: %d " % num )
- def expand_ellipsis ( args , rank ):
+ def expand_ellipsis ( args : tuple [ Union [ int , list [ int ]], ... ], rank : int ) -> list :
"""Expand ellipsis objects and fill in missing axes."""
n_el = sum ( 1 for arg in args if arg is Ellipsis )
@@ -558,7 +583,7 @@ Source code for tables.array
return final_args
- def translate_slice ( exp , length ):
+ def translate_slice ( exp : slice , length : int ) -> tuple [ int , int , int ]:
"""Given a slice object, return a 3-tuple (start, count, step)
This is for use with the hyperslab selection routines.
@@ -642,7 +667,7 @@ Source code for tables.array
list_seen = True
else :
if ( not isinstance ( exp [ 0 ], ( int , np . integer )) or
- ( isinstance ( exp [ 0 ], np . ndarray ) and not
+ ( isinstance ( exp [ 0 ], np . ndarray ) and not
np . issubdtype ( exp [ 0 ] . dtype , np . integer ))):
raise TypeError ( "Only integer coordinates allowed." )
@@ -653,7 +678,9 @@ Source code for tables.array
# (only one unordered list is allowed)
if len ( nexp ) != len ( np . unique ( nexp )):
raise IndexError (
- "Selection lists cannot have repeated values" )
+ "Selection lists cannot have repeated values. "
+ "To see how to handle this, please see https://github.com/PyTables/PyTables/issues/1149"
+ )
neworder = nexp . argsort ()
if ( neworder . shape != ( len ( exp ),) or
np . sum ( np . abs ( neworder - np . arange ( len ( exp )))) != 0 ):
@@ -687,7 +714,7 @@ Source code for tables.array
mshape = tuple ( x for x in mshape if x != 0 )
return selection , reorder , mshape
-[docs] def __getitem__ ( self , key ):
+
[docs] def __getitem__ ( self , key : SelectionType ) -> Union [ list , np . ndarray ]:
"""Get a row, a range of rows or a slice from the array.
The set of tokens allowed for the key is the same as that for extended
@@ -735,7 +762,7 @@
Source code for tables.array
return internal_to_flavor ( arr , self . flavor )
-
[docs] def __setitem__ ( self , key , value ):
+
[docs] def __setitem__ ( self , key : SelectionType , value : Any ) -> None :
"""Set a row, a range of rows or a slice in the array.
It takes different actions depending on the type of the key parameter:
@@ -759,7 +786,7 @@
Source code for tables.array
::
- a1[0] = 333 # assign an integer to a Integer Array row
+ a1[0] = 333 # assign an integer to an Integer Array row
a2[0] = 'b' # assign a string to a string Array row
a3[1:4] = 5 # broadcast 5 to slice 1:4
a4[1:4:2] = 'xXx' # broadcast 'xXx' to slice 1:4:2
@@ -797,7 +824,7 @@ Source code for tables.array
selection , reorder , shape = self . _fancy_selection ( key )
self . _write_selection ( selection , reorder , shape , nparr )
- def _check_shape ( self , nparr , slice_shape ):
+ def _check_shape ( self , nparr : np . ndarray , slice_shape : tuple [ int , ... ]) -> np . ndarray :
"""Test that nparr shape is consistent with underlying object.
If not, try creating a new nparr object, using broadcasting if
@@ -816,7 +843,11 @@ Source code for tables.array
else :
return nparr
- def _read_slice ( self , startl , stopl , stepl , shape ):
+ def _read_slice ( self ,
+ startl : np . ndarray ,
+ stopl : np . ndarray ,
+ stepl : np . ndarray ,
+ shape : list [ int ]) -> np . ndarray :
"""Read a slice based on `startl`, `stopl` and `stepl`."""
nparr = np . empty ( dtype = self . atom . dtype , shape = shape )
@@ -829,7 +860,7 @@ Source code for tables.array
nparr = nparr [()]
return nparr
- def _read_coords ( self , coords ):
+ def _read_coords ( self , coords : np . ndarray ) -> np . ndarray :
"""Read a set of points defined by `coords`."""
nparr = np . empty ( dtype = self . atom . dtype , shape = len ( coords ))
@@ -840,7 +871,10 @@ Source code for tables.array
nparr = nparr [()]
return nparr
- def _read_selection ( self , selection , reorder , shape ):
+ def _read_selection ( self ,
+ selection : list [ tuple [ int , int , int , int , str ]],
+ reorder : Optional [ tuple [ int , npt . ArrayLike ]],
+ shape : tuple [ int , ... ]) -> np . ndarray :
"""Read a `selection`.
Reorder if necessary.
@@ -864,21 +898,30 @@ Source code for tables.array
nparr = nparr [ tuple ( k )] . copy ()
return nparr
- def _write_slice ( self , startl , stopl , stepl , shape , nparr ):
+ def _write_slice ( self ,
+ startl : np . ndarray ,
+ stopl : np . ndarray ,
+ stepl : np . ndarray ,
+ shape : list [ int ],
+ nparr : np . ndarray ) -> None :
"""Write `nparr` in a slice based on `startl`, `stopl` and `stepl`."""
nparr = self . _check_shape ( nparr , tuple ( shape ))
countl = (( stopl - startl - 1 ) // stepl ) + 1
self . _g_write_slice ( startl , stepl , countl , nparr )
- def _write_coords ( self , coords , nparr ):
+ def _write_coords ( self , coords : np . ndarray , nparr : np . ndarray ) -> None :
"""Write `nparr` values in points defined by `coords` coordinates."""
if len ( coords ) > 0 :
nparr = self . _check_shape ( nparr , ( len ( coords ),))
self . _g_write_coords ( coords , nparr )
- def _write_selection ( self , selection , reorder , shape , nparr ):
+ def _write_selection ( self ,
+ selection : list [ tuple [ int , int , int , int , str ]],
+ reorder : Optional [ tuple [ int , npt . ArrayLike ]],
+ shape : tuple [ int , ... ],
+ nparr : np . ndarray ) -> None :
"""Write `nparr` in `selection`.
Reorder if necessary.
@@ -896,7 +939,12 @@ Source code for tables.array
nparr = nparr [ tuple ( k )] . copy ()
self . _g_write_selection ( selection , nparr )
- def _read ( self , start , stop , step , out = None ):
+ def _read ( self ,
+ start : int ,
+ stop : int ,
+ step : int ,
+ out : Optional [ np . ndarray ] = None ,
+ ) -> np . ndarray :
"""Read the array from disk without slice or flavor processing."""
nrowstoread = len ( range ( start , stop , step ))
@@ -925,7 +973,12 @@ Source code for tables.array
arr . byteswap ( True )
return arr
-[docs] def read ( self , start = None , stop = None , step = None , out = None ):
+
[docs] def read ( self ,
+
start : Optional [ int ] = None ,
+
stop : Optional [ int ] = None ,
+
step : Optional [ int ] = None ,
+
out : Optional [ np . ndarray ] = None ,
+
) -> np . ndarray :
"""Get data in the array as an object of the current flavor.
The start, stop and step parameters can be used to select only a
@@ -963,8 +1016,9 @@
Source code for tables.array
arr = self . _read ( start , stop , step , out )
return internal_to_flavor ( arr , self . flavor )
-
def _g_copy_with_stats ( self , group , name , start , stop , step ,
-
title , filters , chunkshape , _log , ** kwargs ):
+
def _g_copy_with_stats ( self , group : "Group" , name : str , start : int , stop : int , step : int ,
+
title : str , filters : Filters , chunkshape : tuple [ int , ... ],
+
_log : bool , ** kwargs ) -> tuple [ "Array" , int ]:
"""Private part of Leaf.copy() for each kind of leaf."""
# Compute the correct indices.
@@ -985,7 +1039,7 @@
Source code for tables.array
return ( object_ , nbytes )
- def __repr__ ( self ):
+ def __repr__ ( self ) -> str :
"""This provides more metainfo in addition to standard __str__"""
return f """ { self }
@@ -1016,7 +1070,7 @@ Source code for tables.array
-
© Copyright 2011–2023, PyTables maintainers.
+
© Copyright 2011–2024, PyTables maintainers.
Built with Sphinx using a
diff --git a/_modules/tables/atom.html b/_modules/tables/atom.html
index 6d63573..d610540 100644
--- a/_modules/tables/atom.html
+++ b/_modules/tables/atom.html
@@ -1,12 +1,14 @@
-
+
- tables.atom — PyTables 3.9.2 documentation
-
-
-
+ tables.atom — PyTables 3.10.0 documentation
+
+
+
+
+
@@ -47,7 +49,7 @@
- 3.9.2
+ 3.10.0