B
    i\9                 @   sf  d Z ddlm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
Z
ddlmZ ddlZdd	ejd
dZe
jZdd Zdd Zdd Zd6ddZdd Zd7ddZdd Zd8ddZdd Zi ZddlZd9d d!Zd"d# Zd:d%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/d0 d0e&Z'd1d2 Z(d3d4 Z)e*d5krbe)  dS );zUtility functions.    )print_function)absolute_importN)time   )array_of_flavorbiglittle
irrelevant)><=|c             C   s   | dk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.7/site-packages/tables/utils.pycorrect_byteorder*   s    r   c             C   s   t | tjkrdS t| drrt| dr4| jdkr4dS y&|   t| trXtj	dt
dd dS  tk
rn   dS X 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sixZinteger_typeshasattrr   r   
isinstancer   warningswarnDeprecationWarning	TypeErrornumpyZintegerZndarraydtypestr)indexr   r   r   is_idx3   s(    


r&   c             C   s"   yt | S    tdY nX dS )z)Convert a possible index into a long int.znot an integer type.N)intr!   )r%   r   r   r   idx2longO   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 )zAConvert 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_atom\   s    


r/   c             C   s@   |j dk}t| ||}t|jj }|dkr<|tjkr<| }|S )zAConvert a generic object into a NumPy object compliant with atom.)Ztime64)r   r   )r   r/   
byteordersr#   r   sysZbyteswap)r   r-   r)   r.   r   r   r   r   convert_to_np_atom2z   s    
r2   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.

    r3   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>      s>    


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 }|kr| S  |  |< }|S )N)__dict__)selfZmydictvalue)fgetnamer   r   newfget   s
    zlazyattr.<locals>.newfgetN)__name__property__doc__)rH   rJ   r   )rH   rI   r   lazyattr   s    )rN   c             C   sP  |dkrt  }dt  }tj|dtjdj}x|D ]}||}|	dr`t
| d }q6|	dr|t
| d }q6|	drt
| d }q6|	d	rt
| d }	q6|	d
rt
| d }
q6|	dr6t
| d }q6W |  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:   )r1   getdefaultencodingr7   getpid
subprocessPopenPIPErP   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    







r`   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    


rd   c             C   s8   |d kr4| j j}|tkr g t|< t| t|  d S )N)	__class__rK   tracked_classesappendweakrefref)instancerI   r   r   r   log_instance_creation9  s
    rk   c             C   s&   | dkrt tt}|S |  S d S )N*)sortedr   Ziterkeysrf   rY   )scr   r   r   string_to_classesB  s    rp   rl   c             C   s   t | }dd |D S )Nc             S   s   g | ]}|t t| fqS r   )r+   rf   ).0Zcnr   r   r   
<listcomp>L  s    z*fetch_logged_instances.<locals>.<listcomp>)rp   )classesZ
classnamesr   r   r   fetch_logged_instancesJ  s    rt   c             C   s0   x*t | D ]}|d|tt| f  q
W d S )Nz%s: %d
)rp   writer+   rf   )rs   file	classnamer   r   r   count_logged_instancesP  s    rx   c             C   sV   xPt | D ]D}|d|  x0t| D ]$}| }|d k	r&|dt|  q&W q
W d S )Nz
%s:
z    %s
)rp   ru   rf   repr)rs   rv   rw   ri   objr   r   r   list_logged_instancesV  s    r{   c             C   s~   xxt | D ]l}|d|  xXt| D ]L}| }|d k	r&|d|  x*t|jD ]\}}|d||f  qTW q&W q
W d S )Nz
%s:
z    %s:
z        %20s : %s
)rp   ru   rf   r   Z	iteritemsrE   )rs   rv   rw   ri   rz   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 )N)
maxentriessuperr~   __init__)rF   r   )re   r   r   r   r  s    zCacheDict.__init__c                s\   t | | jkrF| jd }x,t|  d | D ]}tt| | q.W tt| || d S )N
   )r+   r   listkeysr   r~   __delitem____setitem__)rF   r|   rG   entries_to_removek)re   r   r   r   v  s
    
zCacheDict.__setitem__)rK   
__module____qualname__rM   r   r   __classcell__r   r   )re   r   r~   o  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
_nailcount)rF   r   r   r   r   r     s    zNailedDict.__init__c             C   s   | j   d S )N)r   clear)rF   r   r   r   r     s    zNailedDict.clearc             C   s   |  j d7  _ d S )Nr   )r   )rF   r   r   r   nail  s    zNailedDict.nailc             C   s   |  j d8  _ d S )Nr   )r   )rF   r   r   r   unnail  s    zNailedDict.unnailc             C   s   | j dkrdS || jk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 )Nr   )r   KeyErrorr   )rF   r|   r   r   r   __getitem__  s    
zNailedDict.__getitem__Nc             C   s   | j dkr|S | j||S )Nr   )r   r   get)rF   r|   defaultr   r   r   r     s    
zNailedDict.getc             C   sb   | j dkrd S | j}t|| jkrVt| jd d}x"t| d | D ]
}||= qHW |||< d S )Nr   r   r   )r   r   r+   r   maxr   r   )rF   r|   rG   cacher   r   r   r   r   r     s    

zNailedDict.__setitem__)N)rK   r   r   rM   r   r   r   r   r   r   r   r   r   r   r   r   r     s   
r   c              C   st   t tdrLdtjkr6td} t| trL| dkrL| S nttdd  S dtjk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   r7   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)r3   )N)N)rl   )+rM   Z
__future__r   r   r7   r1   r   rT   rer   r"   Zflavorr   r   r   r0   Zint64ZSizeTyper   r&   r(   r/   r2   r>   rN   r`   rd   rf   rh   rk   rp   rt   rP   rx   r{   r}   dictr~   r   r   r   r   rK   r   r   r   r   <module>   sN   	

85
#
	

6
