ó
jˆú\c           @@ sÚ  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 i d d	 6d
 d 6e j d 6d d 6Z e
 j Z d „  Z d „  Z d „  Z e d „ Z d „  Z d d „ Z d „  Z d d „ Z d „  Z i  Z d d l Z d d „ Z d „  Z  d d „ Z! e j" d „ Z# e j" d „ Z$ e j" d „ Z% d  e& f d! „  ƒ  YZ' d" e( f d# „  ƒ  YZ) d$ „  Z* d% „  Z+ e, d& k rÖe+ ƒ  n  d S('   s   Utility functions.i    (   t   print_function(   t   absolute_importN(   t   timei   (   t   array_of_flavort   bigt   >t   littlet   <t   =t
   irrelevantt   |c         C@ s   |  d k r d S| Sd S(	   s2   Fix the byteorder depending on the PyTables types.t   stringt   boolt   int8t   uint8t   objectR	   N(   R   R   R   R   R   (    (   t   ptypet	   byteorder(    (    s+   lib/python2.7/site-packages/tables/utils.pyt   correct_byteorder*   s    c         C@ sñ   t  |  ƒ t j k r t St |  d ƒ rœ t |  d ƒ rJ |  j d k rJ t Sy: |  j ƒ  t |  t	 ƒ r t
 j d t d d ƒn  t SWqí t k
 r˜ t SXnQ t |  t j ƒ r² t St |  t j ƒ rí |  j d	 k rí |  j j d d k rí t St S(
   s0   Checks if an object can work as an index or not.t	   __index__t   shapesK   using a boolean instead of an integer will result in an error in the futuret
   stackleveli   i   t   i(    (    (   t   typet   sixt   integer_typest   Truet   hasattrR   t   FalseR   t
   isinstanceR   t   warningst   warnt   DeprecationWarningt	   TypeErrort   numpyt   integert   ndarrayt   dtypet   str(   t   index(    (    s+   lib/python2.7/site-packages/tables/utils.pyt   is_idx3   s(    
!c         C@ s(   y t  |  ƒ SWn t d ƒ ‚ n Xd S(   s)   Convert a possible index into a long int.s   not an integer type.N(   t   intR!   (   R'   (    (    s+   lib/python2.7/site-packages/tables/utils.pyt   idx2longO   s    c         C@ s›   t  |  d ƒ } | j d k r? t j | d | j d | ƒ} nX | j t | j j ƒ  } t j | d d | j f g ƒ} | | d (| j | j ƒ } | S(   sA   Convert a generic object into a NumPy object compliant with atom.R"   R%   t   copyt    t   f0(    (   R   R   R"   t   arrayR%   t   lent   emptyt   view(   t   arrt   atomR+   t   nparrt   newshapet   nparr2(    (    s+   lib/python2.7/site-packages/tables/utils.pyt   convert_to_np_atom\   s    !
!c         C@ s_   | j  d k } t |  | | ƒ } t | j j } | d k r[ | t j k r[ | j ƒ  } n  | S(   sA   Convert a generic object into a NumPy object compliant with atom.t   time64R   R   (   R8   (   R   R   (   R   R7   t
   byteordersR%   R   t   syst   byteswap(   R   R3   R+   R4   R   (    (    s+   lib/python2.7/site-packages/tables/utils.pyt   convert_to_np_atom2z   s    t   rc         C@ sÿ  | d k r t  j |  t  j ƒ s7 t d |  f ƒ ‚ n  t  j j |  ƒ s_ t d |  f ƒ ‚ n  t  j |  t  j ƒ sût d |  f ƒ ‚ qûnn| d k r`t  j |  t  j ƒ r¾ t |  d ƒ qût  j j |  ƒ } | sß d } n  t  j | t  j ƒ s
t d | f ƒ ‚ n  t  j j	 | ƒ s2t d | f ƒ ‚ n  t  j | t  j
 ƒ sût d	 | f ƒ ‚ qûn› | d
 k r¡t  j |  t  j ƒ r‘t |  d ƒ qût |  d ƒ nZ | d k rèt |  d ƒ t  j |  t  j
 ƒ sût d |  f ƒ ‚ qûn t d | f ƒ ‚ d S(   så  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.

    R=   s   ``%s`` does not exists   ``%s`` is not a regular files)   file ``%s`` exists but it can not be readt   ws   r+t   .s   ``%s`` is not a directorys1   directory ``%s`` exists but it can not be writtent   as,   file ``%s`` exists but it can not be writtens   invalid mode: %rN(   t   ost   accesst   F_OKt   IOErrort   patht   isfilet   R_OKt   check_file_accesst   dirnamet   isdirt   W_OKt
   ValueError(   t   filenamet   modet
   parentname(    (    s+   lib/python2.7/site-packages/tables/utils.pyRH   Œ   s>    	c         @ s1   ˆ  j  ‰ ‡  ‡ f d †  } t | d d ˆ  j ƒ S(   s"  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         @ s5   |  j  } ˆ | k r | ˆ Sˆ  |  ƒ | ˆ <} | S(   N(   t   __dict__(   t   selft   mydictt   value(   t   fgett   name(    s+   lib/python2.7/site-packages/tables/utils.pyt   newfgetï   s
    	N(   t   __name__t   propertyt   Nonet   __doc__(   RT   RV   (    (   RT   RU   s+   lib/python2.7/site-packages/tables/utils.pyt   lazyattrÄ   s    )	c         C@ s×  | d k r t j ƒ  } n  d t j ƒ  } t j | d t d t j ƒj	 } x| D]} | j
 | ƒ } | j d ƒ r t | j ƒ  d ƒ } qS | j d ƒ r¸ t | j ƒ  d ƒ } qS | j d ƒ rà t | j ƒ  d ƒ } qS | j d ƒ rt | j ƒ  d ƒ }	 qS | j d	 ƒ r0t | j ƒ  d ƒ }
 qS | j d
 ƒ rS t | j ƒ  d ƒ } qS qS W| j ƒ  t d |  ƒ t d | | f ƒ t d | |	 f ƒ t d |
 | f ƒ t ƒ  } t d t | | d ƒ ƒ | S(   s2   Show the used memory (only works for Linux 2.6.x).s   cat /proc/%s/statust   shellt   stdouts   VmSize:i   s   VmRSS:s   VmData:s   VmStk:s   VmExe:s   VmLib:s    Memory usage: ******* %s *******s   VmSize: %7s kB	VmRSS: %7s kBs   VmData: %7s kB	VmStk: %7s kBs   VmExe:  %7s kB	VmLib: %7s kBs   WallClock time:i   N(   RY   R:   t   getdefaultencodingRA   t   getpidt
   subprocesst   PopenR   t   PIPER]   t   decodet
   startswithR)   t   splitt   closet   printR   t   round(   t   explaint   treft   encodingt   cmdt   soutt   linet   vmsizet   vmrsst   vmdatat   vmstkt   vmexet   vmlibt   tnow(    (    s+   lib/python2.7/site-packages/tables/utils.pyt
   show_statsù   s4    !
	c         C@ s¤   t  d | ƒ } t j | ƒ } | d k  rC t t j | ƒ ƒ } n t t j | ƒ ƒ } t j t j t  d | ƒ ƒ ƒ } t  d | ƒ } t j | |  ƒ | } | S(   s÷   quantize 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      $@i    g       @(   t   powR"   t   log10R)   t   floort   ceilt   log2t   around(   t   datat   least_significant_digitt	   precisiont   expt   bitst   scalet   datout(    (    s+   lib/python2.7/site-packages/tables/utils.pyt   quantize  s    
"c         C@ sR   | d  k rN |  j j } | t k r1 g  t | <n  t | j t j |  ƒ ƒ n  d  S(   N(   RY   t	   __class__RW   t   tracked_classest   appendt   weakreft   ref(   t   instanceRU   (    (    s+   lib/python2.7/site-packages/tables/utils.pyt   log_instance_creation9  s
    c         C@ s3   |  d k r% t  t j t ƒ ƒ } | S|  j ƒ  Sd  S(   Nt   *(   t   sortedR   t   iterkeysR†   Re   (   t   st   c(    (    s+   lib/python2.7/site-packages/tables/utils.pyt   string_to_classesB  s    RŒ   c         C@ s3   t  |  ƒ } g  | D] } | t t | ƒ f ^ q S(   N(   R‘   R/   R†   (   t   classest
   classnamest   cn(    (    s+   lib/python2.7/site-packages/tables/utils.pyt   fetch_logged_instancesJ  s    c         C@ s<   x5 t  |  ƒ D]' } | j d | t t | ƒ f ƒ q Wd  S(   Ns   %s: %d
(   R‘   t   writeR/   R†   (   R’   t   filet	   classname(    (    s+   lib/python2.7/site-packages/tables/utils.pyt   count_logged_instancesP  s    c         C@ sp   xi t  |  ƒ D][ } | j d | ƒ xA t | D]5 } | ƒ  } | d  k	 r/ | j d t | ƒ ƒ q/ q/ Wq Wd  S(   Ns   
%s:
s       %s
(   R‘   R–   R†   RY   t   repr(   R’   R—   R˜   R‰   t   obj(    (    s+   lib/python2.7/site-packages/tables/utils.pyt   list_logged_instancesV  s    	c         C@ s¤   x t  |  ƒ D] } | j d | ƒ xu t | D]i } | ƒ  } | d  k	 r/ | j d | ƒ x: t j | j ƒ D]# \ } } | j d | | f ƒ qn Wq/ q/ Wq Wd  S(   Ns   
%s:
s       %s:
s           %20s : %s
(   R‘   R–   R†   RY   R   t	   iteritemsRP   (   R’   R—   R˜   R‰   R›   t   keyRS   (    (    s+   lib/python2.7/site-packages/tables/utils.pyt   dump_logged_instances`  s    	t	   CacheDictc           B@ s    e  Z d  Z d „  Z d „  Z RS(   s8   A dictionary that prevents itself from growing too much.c         C@ s#   | |  _  t t |  ƒ j |  ƒ d  S(   N(   t
   maxentriest   superR    t   __init__(   RQ   R¡   (    (    s+   lib/python2.7/site-packages/tables/utils.pyR£   r  s    	c         C@ sy   t  |  ƒ |  j k r\ |  j d } x7 t |  j ƒ  ƒ |  D] } t t |  ƒ j | ƒ q9 Wn  t t |  ƒ j | | ƒ d  S(   Ni
   (   R/   R¡   t   listt   keysR¢   R    t   __delitem__t   __setitem__(   RQ   Rž   RS   t   entries_to_removet   k(    (    s+   lib/python2.7/site-packages/tables/utils.pyR§   v  s
    (   RW   t
   __module__RZ   R£   R§   (    (    (    s+   lib/python2.7/site-packages/tables/utils.pyR    o  s   	t
   NailedDictc           B@ sY   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d	 d „ Z
 d „  Z RS(
   s=   A dictionary which ignores its items when it has nails on it.c         C@ s   | |  _  i  |  _ d |  _ d  S(   Ni    (   R¡   t   _cachet
   _nailcount(   RQ   R¡   (    (    s+   lib/python2.7/site-packages/tables/utils.pyR£   ƒ  s    		c         C@ s   |  j  j ƒ  d  S(   N(   R¬   t   clear(   RQ   (    (    s+   lib/python2.7/site-packages/tables/utils.pyR®   Ž  s    c         C@ s   |  j  d 7_  d  S(   Ni   (   R­   (   RQ   (    (    s+   lib/python2.7/site-packages/tables/utils.pyt   nail‘  s    c         C@ s   |  j  d 8_  d  S(   Ni   (   R­   (   RQ   (    (    s+   lib/python2.7/site-packages/tables/utils.pyt   unnail”  s    c         C@ s    |  j  d k r t S| |  j k S(   Ni    (   R­   R   R¬   (   RQ   Rž   (    (    s+   lib/python2.7/site-packages/tables/utils.pyt   __contains__š  s    c         C@ s)   |  j  d k r t | ƒ ‚ n  |  j | S(   Ni    (   R­   t   KeyErrorR¬   (   RQ   Rž   (    (    s+   lib/python2.7/site-packages/tables/utils.pyt   __getitem__Ÿ  s    c         C@ s&   |  j  d k r | S|  j j | | ƒ S(   Ni    (   R­   R¬   t   get(   RQ   Rž   t   default(    (    s+   lib/python2.7/site-packages/tables/utils.pyR´   ¤  s    c         C@ s€   |  j  d k r d  S|  j } t | ƒ |  j k rr t |  j d d ƒ } x( t | j ƒ  ƒ |  D] } | | =q^ Wn  | | | <d  S(   Ni    i
   i   (   R­   R¬   R/   R¡   t   maxR¤   R¥   (   RQ   Rž   RS   t   cacheR¨   R©   (    (    s+   lib/python2.7/site-packages/tables/utils.pyR§   ©  s    	N(   RW   Rª   RZ   R£   R®   R¯   R°   R±   R³   RY   R´   R§   (    (    (    s+   lib/python2.7/site-packages/tables/utils.pyR«   €  s   						c          C@ s¨   t  t d ƒ ro d t j k rO t j d ƒ }  t |  t ƒ rl |  d k rl |  Sqo t t j d ƒ d j ƒ  ƒ Sn  d t j k r¤ t t j d ƒ }  |  d k r¤ |  Sn  d S(   sD   Detects the number of cores on a system.

    Cribbed from pp.

    t   sysconft   SC_NPROCESSORS_ONLNi    s   sysctl -n hw.ncpui   t   NUMBER_OF_PROCESSORS(	   R   RA   t   sysconf_namesR¸   R   R)   t   popen2t   readt   environ(   t   ncpus(    (    s+   lib/python2.7/site-packages/tables/utils.pyt   detect_number_of_cores¶  s     c          C@ s   d d l  }  |  j ƒ  d S(   s   Run ``doctest`` on this module.i    N(   t   doctestt   testmod(   RÁ   (    (    s+   lib/python2.7/site-packages/tables/utils.pyt   _testÑ  s    t   __main__(-   RZ   t
   __future__R    R   RA   R:   R   R`   t   reR   R"   t   flavorR   R   R   R9   t   int64t   SizeTypeR   R(   R*   R   R7   R<   RH   R[   RY   Rv   R„   R†   Rˆ   R‹   R‘   R•   R]   R™   Rœ   RŸ   t   dictR    R   R«   RÀ   RÃ   RW   (    (    (    s+   lib/python2.7/site-packages/tables/utils.pyt   <module>   sP   

						8	5#			
6		