
    Kg'=                    H   U d Z ddlmZ ddlZddlZddlZddlZddlZddlm	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ZddlmZ dd	lmZ erdd
lmZ ddej8                  ddZej<                  Zde d<   ejB                  jE                  ejF                        dk\  rdZ$nQejB                  jE                  ejF                        dk  rdZ$n&	  ejJ                  dg      jM                  d       dZ$d-dZ(d.dZ)d/dZ*e$f	 	 	 	 	 	 	 d0dZ+d1dZ,	 d2	 	 	 	 	 d3dZ-d4dZ.d5d6dZ/d7dZ0i Z1de d<   d5d8d Z2d9d!Z3d:d;d"Z4ejj                  fd<d#Z6ejj                  fd<d$Z7ejj                  fd<d%Z8 G d& d'e9      Z: G d( d)      Z;d=d*Z<d>d+Z=e>d,k(  r e=        yy# e'$ r dZ$Y w xY w)?zUtility functions.    )annotationsN)perf_counter)AnyLiteralTextIOTYPE_CHECKING)Path)Callable   )array_of_flavor)Atombiglittle
irrelevant)><=|bool | Nonecopy_if_neededz2.0.0z1.28.0F)copyc                    | dv ry|S )z2Fix the byteorder depending on the PyTables types.)stringboolint8uint8objectr    )ptype	byteorders     ,lib/python3.12/site-packages/tables/utils.pycorrect_byteorderr"   2   s    ==    c                   t        |       t        u ryt        | d      rZt        | d      r| j                  dk7  ry	 | j	                          t        | t              rt        j                  dt        d       yt        | t        j                        ryt        | t        j                        r,| j                  dk(  r| j                  j                  d	   d
k(  ryy# t        $ r Y yw xY w)z/Check if an object can work as an index or not.T	__index__shaper   FzKusing a boolean instead of an integer will result in an error in the future   )
stacklevelr   i)typeinthasattrr&   r%   
isinstancer   warningswarnDeprecationWarning	TypeErrornpintegerndarraydtypestrindexs    r!   is_idxr9   :   s    E{c		$5'"u{{b'8	OO%&*& 	  
E2::	& 	5"**%[[BKKOOA#%  		s   <C 	C'&C'c                    	 t        | d      r| j                         S t        |       S # t        $ r t	        d      w xY w)z)Convert a possible index into a long int.itemznot an integer type.)r,   r;   r+   	Exceptionr1   r7   s    r!   idx2longr=   [   sB    05&!::<u: 0.//0s   ) 
) >c                l   t        | d      }|j                  dk(  r$t        j                  ||j                  |      }|S |j                  dt        |j                  j                          }t        j                  |d|j                  fg      }||d   dd |j                  |j                        }|S )AConvert a generic object into a NumPy object compliant with atom.numpyr   )r5   r   N )r5   f0)r   r&   r2   arrayr5   lenemptyview)arratomr   nparrnewshapenparr2s         r!   convert_to_np_atomrL   j   s    
 C)E zzRdjjt< L ;;7#djj&6&6"7!78(B

+;*<=tQDJJ'Lr#   c                    |j                   dv rdnt        }t        | ||      }t        |j                  j
                     }|dv r#|t        j
                  k7  r|j                         }|S )r?   )time64T)r   r   )r*   r   rL   
byteordersr5   r    sysbyteswap)objrH   r   rI   r    s        r!   convert_to_np_atom2rS      s_     99
*4DsD$/E5;;001I%%)s}}*D  Lr#   c                f   t        |       j                         }|dk(  rt        j                  |t        j                        st        d| d      |j                         st        d| d      t        j                  |t        j                        st        d| d      y|dk(  rt        j                  |t        j                        rt        |d       yt        j                  |j                  t        j                        st        d|j                   d      |j                  j                         st        d|j                   d	      t        j                  |j                  t        j                        st        d
|j                   d      y|dk(  r>t        j                  |t        j                        rt        |d       yt        |d       y|dk(  r@t        |d       t        j                  |t        j                        st        d| d      yt        d|      )a  Check for file access in the specified `mode`.

    `mode` is one of the modes supported by `File` objects.  If the file
    indicated by `filename` can be accessed using that `mode`, the
    function ends successfully.  Else, an ``IOError`` is raised
    explaining the reason of the failure.

    All this paraphernalia is used to avoid the lengthy and scaring HDF5
    messages produced when there are problems opening a file.  No
    changes are ever made to the file system.

    rz``z`` does not existz`` is not a regular filezfile ``z `` exists but it can not be readwzr+z`` is not a directoryzdirectory ``z#`` exists but it can not be writtenazinvalid mode: N)r	   resolveosaccessF_OKFileNotFoundErroris_fileIsADirectoryErrorR_OKPermissionErrorcheck_file_accessparentis_dirNotADirectoryErrorW_OK
ValueError)filenamemodepaths      r!   ra   ra      s    >!!#Ds{yyrww'#b.?$@AA||~#b.F$GHHyyrww'!$?@  ( 
99T277# dD) 99T[["''2'"T[[M9J(KLL;;%%'(%:;  99T[["''2%"4;;- 0   3
 
99T277#dD)dC(	$$yyrww'!$BC  (
 >$233r#   c                Z      j                    fd}t        |dd j                        S )a  Create a *lazy attribute* from the result of `fget`.

    This function is intended to be used as a *method decorator*.  It
    returns a *property* which caches the result of calling the `fget`
    instance method.  The docstring of `fget` is used for the property
    itself.  For instance:

    >>> class MyClass(object):
    ...     @lazyattr
    ...     def attribute(self):
    ...         'Attribute description.'
    ...         print('creating value')
    ...         return 10
    ...
    >>> type(MyClass.attribute)
    <class 'property'>
    >>> MyClass.attribute.__doc__
    'Attribute description.'
    >>> obj = MyClass()
    >>> obj.__dict__
    {}
    >>> obj.attribute
    creating value
    10
    >>> obj.__dict__
    {'attribute': 10}
    >>> obj.attribute
    10
    >>> del obj.attribute
    Traceback (most recent call last):
      ...
    AttributeError: ...

    .. warning::

        Please note that this decorator *changes the type of the
        decorated object* from an instance method into a property.

    c                L    | j                   }|v r|   S  |       x|<   }|S N)__dict__)selfmydictvaluefgetnames      r!   newfgetzlazyattr.<locals>.newfget   s4    6>$<#Dz)tur#   N)__name__property__doc__)rq   rs   rr   s   ` @r!   lazyattrrw      s+    P ==D GT466r#   c                V   t        d      j                         j                         D ]  }|j                  d      rt	        |j                         d         }2|j                  d      rt	        |j                         d         }`|j                  d      rt	        |j                         d         }|j                  d      rt	        |j                         d         }|j                  d      rt	        |j                         d         }|j                  d      st	        |j                         d         }	 t        d	| z         t        d
dddd       t        ddddd       t        ddd	dd       t               }
t        d|
|z
  d       |
S )z2Show the used memory (only works for Linux 2.6.x).z/proc/self/statuszVmSize:r   zVmRSS:zVmData:zVmStk:zVmExe:zVmLib:z Memory usage: ******* %s *******zVmSize: z>7z kB	VmRSS: z kBzVmData: z kB	VmStk: zVmExe:  z kB	VmLib: zWallClock time: z.3f)r	   	read_text
splitlines
startswithr+   splitprintclock)explaintrefencodinglinevmsizevmrssvmdatavmstkvmexevmlibtnows              r!   
show_statsr   	  si   ()335@@B??9%a)F__X&

Q(E__Y'a)F__X&

Q(E__X&

Q(E__X&

Q(E C 

,w
67	HVBK|E":S
9:	HVBK|E":S
9:	HU2Jl5*C
897D	TD[-
./Kr#   c                   | }|dk  rt        j                  |      nt        j                  |      }t        j                  t        j                  d| z              }d|z  }t	        j
                  || z        |z  }|S )zQuantize data to improve compression.

    Data is quantized using around(scale*data)/scale, where scale is
    2**bits, and bits is determined from the least_significant_digit.

    For example, if least_significant_digit=1, bits will be 4.

    r   
   r'   )mathfloorceillog2r2   around)dataleast_significant_digitexpbitsscaledatouts         r!   quantizer   #  sk     #
"C 1W$**S/$))C.C99TYYrC4x()DtGEYYut|$u,FMr#   z&dict[str, list[weakref.ReferenceType]]tracked_classesc                    |S| j                   j                  }|t        vr	g t        |<   t        |   j                  t	        j
                  |              yy)zLog instance creation.N)	__class__rt   r   appendweakrefref)instancerr   s     r!   log_instance_creationr   :  sK    |!!**&$&OD!$$W[[%:;	 r#   c                N    | dk(  rt        t              }|S | j                         S )z=Return the list of tracked classes matching the input string.*)sortedr   r|   )scs     r!   string_to_classesr   C  s$    Cx?#wwyr#   c                f    t        |       }|D cg c]  }|t        t        |         f c}S c c}w )z$Return the list of logged instances.)r   rD   r   )classes
classnamescns      r!   fetch_logged_instancesr   L  s3    "7+J5?@ZrR_R()*Z@@@s   .c           	     r    t        |       D ])  }|j                  | dt        t        |          d       + y)z-Write to file the number of logged instances.z: 
N)r   writerD   r   )r   file	classnames      r!   count_logged_instancesr   R  s5    &w/	

i[3y'A#B"C2FG 0r#   c                    t        |       D ]L  }|j                  d| d       t        |   D ])  } |       }||j                  dt        |      z         + N y)z+Write to file the list of loggen instances.r   :
Nz    %s
)r   r   r   repr)r   r   r   r   rR   s        r!   list_logged_instancesr   X  sV    &w/	

R	{#&'"9-C%C

:S	12 . 0r#   c           
        t        |       D ]~  }|j                  d| d       t        |   D ][  } |       }||j                  d|z         |j                  j	                         D ]  \  }}|j                  d|dd| d         ]  y)zDump the logged instances.r   r   Nz    %s:
z        z>20z : )r   r   r   rm   items)r   r   r   r   rR   keyrp   s          r!   dump_logged_instancesr   b  s    &w/	

R	{#&'"9-C%C

;,-"%,,"4"4"6JCJJ#c#eWB?@ #7	 . 0r#   c                  0     e Zd ZdZd fdZd fdZ xZS )	CacheDictz8A dictionary that prevents itself from growing too much.c                2    || _         t        | 	  |        y rl   )
maxentriessuper__init__)rn   r   r   s     r!   r   zCacheDict.__init__t  s    $r#   c                    t        |       | j                  kD  r1| j                  dz  }t        |       d | D ]  }t        |   |        t        |   ||       y )Nr   )rD   r   listr   __delitem____setitem__)rn   r   rp   entries_to_removekr   s        r!   r   zCacheDict.__setitem__x  sU    t9t& $" 4$Z 2!23#A& 4C'r#   r   r+   returnNone)r   r6   rp   r   r   r   )rt   
__module____qualname__rv   r   r   __classcell__)r   s   @r!   r   r   q  s    B( (r#   r   c                  R    e Zd ZdZddZddZddZddZddZddZ	ddd	Z
dd
Zy)
NailedDictz=A dictionary which ignores its items when it has nails on it.c                .    || _         i | _        d| _        y Nr   )r   _cache
_nailcount)rn   r   s     r!   r   zNailedDict.__init__  s    $r#   c                8    | j                   j                          y)zClear teh dictionsry.N)r   clearrn   s    r!   r   zNailedDict.clear  s    r#   c                .    | xj                   dz  c_         y)zIncrease the nail count.r   Nr   r   s    r!   nailzNailedDict.nail      1r#   c                .    | xj                   dz  c_         y)zDecrease the nail count.r   Nr   r   s    r!   unnailzNailedDict.unnail  r   r#   c                >    | j                   dkD  ry|| j                  v S )Nr   F)r   r   rn   r   s     r!   __contains__zNailedDict.__contains__  s     ??Qdkk!!r#   c                T    | j                   dkD  rt        |      | j                  |   S r   )r   KeyErrorr   r   s     r!   __getitem__zNailedDict.__getitem__  s'    ??Q3-{{3r#   Nc                \    | j                   dkD  r|S | j                  j                  ||      S )z'Return the value for the specified key.r   )r   r   get)rn   r   defaults      r!   r   zNailedDict.get  s(    ??QN{{sG,,r#   c                    | j                   dkD  ry | j                  }t        |      | j                  kD  r/t	        | j                  dz  d      }t        |      d | D ]  }||=  |||<   y )Nr   r   r   )r   r   rD   r   maxr   )rn   r   rp   cacher   r   s         r!   r   zNailedDict.__setitem__  sg    ??Qu:' #DOOr$91 =%[!3"34!H 5c
r#   r   r   r   )r   r   r   r   )r   r   r   r   rl   )r   r   r   z
Any | Noner   r   )r   r   rp   r   r   r   )rt   r   r   rv   r   r   r   r   r   r   r   r   r   r#   r!   r   r     s-    G"
 
-
r#   r   c                 h   t        t        d      rmdt        j                  v r,t        j                  d      } t	        | t
              r6| dkD  r1| S t        t        j                  d      d   j                               S dt        j                  v r#t        t        j                  d         } | dkD  r| S y)z'Detect the number of cores on a system.sysconfSC_NPROCESSORS_ONLNr   zsysctl -n hw.ncpur   NUMBER_OF_PROCESSORS)	r,   rY   sysconf_namesr   r-   r+   popen2readenviron)ncpuss    r!   detect_number_of_coresr     s     r9 B$4$44JJ45E%%%!)ryy!45a8==?@@+BJJ56719Lr#   c                 ,    ddl } | j                          y)zRun ``doctest`` on this module.r   N)doctesttestmod)r   s    r!   _testr     s    OOr#   __main__)r   r6   r    r6   r   r6   )r8   r   r   r   )r8   zint | float | np.ndarrayr   r+   )rG   npt.ArrayLikerH   r   r   r   r   
np.ndarray)rR   r   rH   r   r   r   )rU   )rg   r6   rh   zLiteral['r', 'w', 'a', 'r+']r   r   )rq   zCallable[[Any], Any]r   ru   rl   )r   r6   r   floatr   r   )r   r   r   r+   )r   r   rr   z
str | Noner   r   )r   r6   r   z	list[str])r   )r   r6   r   zlist[tuple[str, int]])r   r6   r   r   r   r   )r   r+   r   )?rv   
__future__r   rY   rP   r   r   r.   timer   r~   typingr   r   r   r   pathlibr	   collections.abcr
   r@   r2   numpy.typingnptflavorr   rH   r   r    rO   int64SizeType__annotations__libNumpyVersion__version__r   rC   	__array__r1   r"   r9   r=   rL   rS   ra   rw   r   r   r   r   r   r   stdoutr   r   r   dictr   r   r   r   rt   r   r#   r!   <module>r     s    " 	 
    & 6 6  $   # 
				
 88 66r~~&'1NVV(83N!T*
B0  9G	"*5<" 9<:4:45:4	:4z17h4( ;=7 <<A 9<

 H 8;zz 3 8;zz 	A( ("7 7t& z	G Q  s   %F F! F!