a
    =_9                     @   sF  d 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Zddlm	Z	 ddej
dd	ZejZd
d Zdd Zdd Zd4ddZdd Zd5ddZdd Zd6ddZdd Zi ZddlZd7ddZd d! Zd8d#d$Zejfd%d&Zejfd'd(Zejfd)d*ZG d+d, d,e Z!G d-d. d.e"Z#d/d0 Z$d1d2 Z%e&d3krBe%  dS )9zUtility functions.    N)time   )array_of_flavorbiglittle
irrelevant)><=|c                 C   s   | dv rdS |S dS )z2Fix the byteorder depending on the PyTables types.)stringboolZint8Zuint8objectr   N )Zptype	byteorderr   r   +lib/python3.9/site-packages/tables/utils.pycorrect_byteorder'   s    r   c                 C   s   t | tu rdS t| drrt| dr2| jdkr2dS z(|   t| trVtjdt	dd W dS  t
yn   Y dS 0 n:t| tjrdS t| tjr| jdkr| jjd	 d
krdS dS )z0Checks 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numpyZintegerZndarraydtypestrindexr   r   r   is_idx0   s,    


r%   c                 C   s$   z
t | W S    tdY n0 dS )z)Convert a possible index into a long int.znot an integer type.N)r   r   r#   r   r   r   idx2longL   s    
r&   Fc                 C   sv   t | d}|jdkr(tj||j|d}nJ|jdt|jj  }tj|d|jfgd}||d dd< ||j}|S )AConvert a generic object into a NumPy object compliant with atom.r    r   )r!   copyN )r!   Zf0)r   r   r    Zarrayr!   lenemptyZview)Zarratomr(   nparrZnewshapeZnparr2r   r   r   convert_to_np_atomY   s    


r.   c                 C   s@   |j dv }t| ||}t|jj }|dv r<|tjkr<| }|S )r'   )Ztime64)r   r   )r   r.   
byteordersr!   r   sysZbyteswap)r   r,   r(   r-   r   r   r   r   convert_to_np_atom2w   s    
r1   rc                 C   s^  |dkr\t | t js$td| f t j| s>td| f t | t jsZtd| f n|dkrt | t jr~t| d nft j| }|sd}t |t jstd|f t j	|std|f t |t j
std	|f nt|d
krt | t jrt| d n
t| d nB|dkrLt| d t | t j
sZtd| f ntd|f dS )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.

    r2   z``%s`` does not existz``%s`` is not a regular filez)file ``%s`` exists but it can not be readwzr+.z``%s`` is not a directoryz1directory ``%s`` exists but it can not be writtenaz,file ``%s`` exists but it can not be writtenzinvalid mode: %rN)osaccessF_OKIOErrorpathisfileR_OKcheck_file_accessdirnameisdirW_OK
ValueError)filenamemodeZ
parentnamer   r   r   r=      sD    


r=   c                    s$    j  fd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)
    <type '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: can't delete attribute

    .. warning::

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

    c                    s*   | j }|v r| S  |  |< }|S N)__dict__)selfZmydictvaluefgetnamer   r   newfget   s
    zlazyattr.<locals>.newfgetN)__name__property__doc__)rI   rK   r   rH   r   lazyattr   s    )rO   c                 C   sL  |du rt  }dt  }tj|dtjdj}|D ]}||}|	dr^t
| d }q4|	drzt
| d }q4|	drt
| d }q4|	d	rt
| d }	q4|	d
rt
| d }
q4|	dr4t
| d }q4|  td|   td||f  td||	f  td|
|f  t }tdt|| d |S )z2Show the used memory (only works for Linux 2.6.x).Nzcat /proc/%s/statusT)shellstdoutzVmSize:r   zVmRSS:zVmData:zVmStk:zVmExe:zVmLib:z Memory usage: ******* %s *******zVmSize: %7s kB	VmRSS: %7s kBzVmData: %7s kB	VmStk: %7s kBzVmExe:  %7s kB	VmLib: %7s kBzWallClock time:   )r0   getdefaultencodingr6   getpid
subprocessPopenPIPErQ   decode
startswithr   splitcloseprintr   round)ZexplainZtrefencodingcmdZsoutlineZvmsizeZvmrssZvmdataZvmstkZvmexeZvmlibZtnowr   r   r   
show_stats   s4    






ra   c                 C   st   t d| }t|}|dk r.tt|}ntt|}ttt d| }t d|}t||  | }|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.

    g      $@r   g       @)powr    Zlog10r   ZfloorZceilZlog2Zaround)dataZleast_significant_digitZ	precisionZexpbitsZscaleZdatoutr   r   r   quantize  s    


re   c                 C   s8   |d u r4| j j}|tvr g t|< t| t|  d S rD   )	__class__rL   tracked_classesappendweakrefref)instancerJ   r   r   r   log_instance_creation6  s
    rl   c                 C   s$   | dkrt t }|S |  S d S )N*)sortedrg   keysrZ   )scr   r   r   string_to_classes?  s    rr   rm   c                 C   s   t | }dd |D S )Nc                 S   s   g | ]}|t t| fqS r   )r*   rg   ).0Zcnr   r   r   
<listcomp>I      z*fetch_logged_instances.<locals>.<listcomp>)rr   )classesZ
classnamesr   r   r   fetch_logged_instancesG  s    rw   c                 C   s,   t | D ]}|d|tt| f  qd S )Nz%s: %d
)rr   writer*   rg   )rv   file	classnamer   r   r   count_logged_instancesM  s    r{   c                 C   sN   t | D ]@}|d|  t| D ]$}| }|d ur"|dt|  q"qd S )N
%s:
z    %s
)rr   rx   rg   repr)rv   ry   rz   rj   objr   r   r   list_logged_instancesS  s    r   c                 C   sp   t | D ]b}|d|  t| D ]F}| }|d ur"|d|  |j D ]\}}|d||f  qLq"qd S )Nr|   z    %s:
z        %20s : %s
)rr   rx   rg   rE   items)rv   ry   rz   rj   r~   keyrG   r   r   r   dump_logged_instances]  s    r   c                       s,   e Zd ZdZ fddZ fddZ  ZS )	CacheDictz8A dictionary that prevents itself from growing too much.c                    s   || _ tt| |  d S rD   )
maxentriessuperr   __init__rF   r   rf   r   r   r   o  s    zCacheDict.__init__c                    sX   t | | jkrB| jd }t|  d | D ]}tt| | q,tt| || d S )N
   )r*   r   listro   r   r   __delitem____setitem__)rF   r   rG   entries_to_removekr   r   r   r   s  s
    
zCacheDict.__setitem__)rL   
__module____qualname__rN   r   r   __classcell__r   r   r   r   r   l  s   r   c                   @   sR   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dS )
NailedDictz=A dictionary which ignores its items when it has nails on it.c                 C   s   || _ i | _d| _d S Nr   )r   _cache
_nailcountr   r   r   r   r     s    zNailedDict.__init__c                 C   s   | j   d S rD   )r   clearrF   r   r   r   r     s    zNailedDict.clearc                 C   s   |  j d7  _ d S Nr   r   r   r   r   r   nail  s    zNailedDict.nailc                 C   s   |  j d8  _ d S r   r   r   r   r   r   unnail  s    zNailedDict.unnailc                 C   s   | j dkrdS || jv S )Nr   F)r   r   rF   r   r   r   r   __contains__  s    
zNailedDict.__contains__c                 C   s   | j dkrt|| j| S r   )r   KeyErrorr   r   r   r   r   __getitem__  s    
zNailedDict.__getitem__Nc                 C   s   | j dkr|S | j||S r   )r   r   get)rF   r   defaultr   r   r   r     s    
zNailedDict.getc                 C   s^   | j dkrd S | j}t|| jkrRt| jd d}t| d | D ]
}||= qF|||< d S )Nr   r   r   )r   r   r*   r   maxr   ro   )rF   r   rG   cacher   r   r   r   r   r     s    
zNailedDict.__setitem__)N)rL   r   r   rN   r   r   r   r   r   r   r   r   r   r   r   r   r   }  s   
r   c                  C   st   t tdrLdtjv r6td} t| trL| dkrL| S nttdd  S dtjv rpttjd } | dkrp| S dS )zDDetects the number of cores on a system.

    Cribbed from pp.

    sysconfSC_NPROCESSORS_ONLNr   zsysctl -n hw.ncpur   ZNUMBER_OF_PROCESSORS)	r   r6   sysconf_namesr   r   r   Zpopen2readenviron)Zncpusr   r   r   detect_number_of_cores  s    



r   c                  C   s   ddl } |   dS )zRun ``doctest`` on this module.r   N)doctestZtestmod)r   r   r   r   _test  s    r   __main__)F)r2   )N)N)rm   )'rN   r6   r0   r   rU   rer   r    Zflavorr   r   r/   Zint64ZSizeTyper   r%   r&   r.   r1   r=   rO   ra   re   rg   ri   rl   rr   rw   rQ   r{   r   r   dictr   r   r   r   r   rL   r   r   r   r   <module>   sJ   		

85
#
	

6
