ó
‡ˆ\c           @   sö   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 Z d d l Z d d l m	 Z	 m
 Z
 e
 r‚ e j Z n	 e j Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d	 e f d
 „  ƒ  YZ d e f d „  ƒ  YZ d e d „ Z d S(   sb   
Fast cryptographic hash of Python objects, with a special case for fast
hashing of numpy arrays.
iÿÿÿÿNi   (   t   _bytes_or_unicodet   PY3_OR_LATERt   _ConsistentSetc           B   s   e  Z d  Z d „  Z RS(   sa    Class used to ensure the hash of Sets is preserved
        whatever the order of its items.
    c         C   sM   y t  | ƒ |  _ Wn3 t t j f k
 rH t  d „  | Dƒ ƒ |  _ n Xd  S(   Nc         s   s   |  ] } t  | ƒ Vq d  S(   N(   t   hash(   t   .0t   e(    (    s?   lib/python2.7/site-packages/sklearn/externals/joblib/hashing.pys	   <genexpr>*   s    (   t   sortedt	   _sequencet	   TypeErrort   decimalt   InvalidOperation(   t   selft   set_sequence(    (    s?   lib/python2.7/site-packages/sklearn/externals/joblib/hashing.pyt   __init__   s    (   t   __name__t
   __module__t   __doc__R   (    (    (    s?   lib/python2.7/site-packages/sklearn/externals/joblib/hashing.pyR      s   t   _MyHashc           B   s   e  Z d  Z d „  Z RS(   s7    Class used to hash objects that won't normally pickle c         G   s   | |  _  d  S(   N(   t   args(   R   R   (    (    s?   lib/python2.7/site-packages/sklearn/externals/joblib/hashing.pyR   0   s    (   R   R   R   R   (    (    (    s?   lib/python2.7/site-packages/sklearn/externals/joblib/hashing.pyR   -   s   t   Hasherc           B   sÁ   e  Z d  Z d d „ Z e d „ Z d „  Z d „  Z d	 e	 j
 d „ Z e j j ƒ  Z e e e e ƒ <e e e e ƒ <e e e e ƒ <e e e e j ƒ <d „  Z d „  Z e e e e ƒ  ƒ <RS(
   sW    A subclass of pickler, to do cryptographic hashing, rather than
        pickling.
    t   md5c         C   sV   t  j ƒ  |  _ t r t j n t j } t j |  |  j d | ƒt	 j
 | ƒ |  _ d  S(   Nt   protocol(   t   iot   BytesIOt   streamR   t   picklet   DEFAULT_PROTOCOLt   HIGHEST_PROTOCOLt   PicklerR   t   hashlibt   newt   _hash(   R   t	   hash_nameR   (    (    s?   lib/python2.7/site-packages/sklearn/externals/joblib/hashing.pyR   9   s
    	c         C   s   y |  j  | ƒ Wn5 t j k
 rH } | j d | | f f 7_ ‚  n X|  j j ƒ  } |  j j | ƒ | r{ |  j j ƒ  Sd  S(   Ns"   PicklingError while hashing %r: %r(	   t   dumpR   t   PicklingErrorR   R   t   getvalueR   t   updatet	   hexdigest(   R   t   objt   return_digestR   t   dumps(    (    s?   lib/python2.7/site-packages/sklearn/externals/joblib/hashing.pyR   C   s    c         C   sÑ   t  | t j t i  j ƒ f ƒ r½ t | d ƒ r? | j j } n	 | j } | j } t | ƒ t t	 ƒ k r~ t
 | | j ƒ } q½ | d  k rœ t
 | | ƒ } q½ | j j } t
 | | | ƒ } n  t j |  | ƒ d  S(   Nt   __func__(   t
   isinstancet   typest
   MethodTypet   typet   popt   hasattrR)   R   t   __self__R   R   t   Nonet	   __class__R   t   save(   R   R&   t	   func_namet   instt   cls(    (    s?   lib/python2.7/site-packages/sklearn/externals/joblib/hashing.pyR3   N   s    !		c         C   s'   t  | t ƒ r d  St j |  | ƒ d  S(   N(   R*   R    R   t   memoize(   R   R&   (    (    s?   lib/python2.7/site-packages/sklearn/externals/joblib/hashing.pyR7   a   s    c         C   sá   t  d | d | ƒ } t j d k r. | d =n  y t j |  | |  Wn• t j k
 rÜ t j |  | |  t | d d  ƒ } | d k rÝ | } | d  k r§ | j	 } n  t j
 | } t | | ƒ sÙ t | | | ƒ qÙ qÝ n Xd  S(   Nt   namet   packi   i   R   t   __main__(   i   i   (   t   dictt   syst   version_infoR   t   save_globalR   R"   t   getattrR1   R   t   modulesR/   t   setattr(   R   R&   R8   R9   t   kwargst   modulet   my_namet   mod(    (    s?   lib/python2.7/site-packages/sklearn/externals/joblib/hashing.pyR>   l   s    
c         C   s^   y  t  j |  t t | ƒ ƒ ƒ Wn7 t k
 rY t  j |  t t d „  | Dƒ ƒ ƒ ƒ n Xd  S(   Nc         s   s'   |  ] \ } } t  | ƒ | f Vq d  S(   N(   R   (   R   t   kt   v(    (    s?   lib/python2.7/site-packages/sklearn/externals/joblib/hashing.pys	   <genexpr>—   s   (   R   t   _batch_setitemst   iterR   R   (   R   t   items(    (    s?   lib/python2.7/site-packages/sklearn/externals/joblib/hashing.pyRH   Œ   s
     c         C   s   t  j |  t | ƒ ƒ d  S(   N(   R   R3   R   (   R   t	   set_items(    (    s?   lib/python2.7/site-packages/sklearn/externals/joblib/hashing.pyt   save_setš   s    N(   R   R   R   R   t   TrueR   R3   R7   R1   t   structR9   R>   R   t   dispatcht   copyR-   t   lent   objectR   R!   RH   RL   t   set(    (    (    s?   lib/python2.7/site-packages/sklearn/externals/joblib/hashing.pyR   4   s   
				t   NumpyHasherc           B   s&   e  Z d  Z d e d „ Z d „  Z RS(   s7    Special case the hasher for when numpy is loaded.
    R   c         C   s\   | |  _  t j |  d | ƒd d l } | |  _ t | d ƒ rO | j |  _ n	 t |  _ d S(   s  
            Parameters
            ----------
            hash_name: string
                The hash algorithm to be used
            coerce_mmap: boolean
                Make no difference between np.memmap and np.ndarray
                objects.
        R    iÿÿÿÿNt	   getbuffer(	   t   coerce_mmapR   R   t   numpyt   npR/   RU   t
   _getbuffert
   memoryview(   R   R    RV   RX   (    (    s?   lib/python2.7/site-packages/sklearn/externals/joblib/hashing.pyR   ¥   s    
		c         C   sE  t  | |  j j ƒ rû | j j rû | j d k r@ | j ƒ  } n9 | j j rU | } n$ | j j	 rm | j
 } n | j ƒ  } |  j j |  j | j |  j j ƒ ƒ ƒ |  j rÎ t  | |  j j ƒ rÎ |  j j } n	 | j } | d | j | j | j f f } n6 t  | |  j j ƒ r1| j } | d | j f f } n  t j |  | ƒ d S(   s¨    Subclass the save method, to hash ndarray subclass, rather
            than pickling them. Off course, this is a total abuse of
            the Pickler class.
        t   HASHEDN(    (   R*   RX   t   ndarrayt   dtypet	   hasobjectt   shapet   flattent   flagst   c_contiguoust   f_contiguoust   TR   R$   RY   t   viewt   uint8RV   t   memmapR2   t   stridest   descrR   R3   (   R   R&   t   obj_c_contiguoust   klass(    (    s?   lib/python2.7/site-packages/sklearn/externals/joblib/hashing.pyR3   ¹   s$    "			$	(   R   R   R   t   FalseR   R3   (    (    (    s?   lib/python2.7/site-packages/sklearn/externals/joblib/hashing.pyRT   ¡   s   R   c         C   sC   d t  j k r' t d | d | ƒ } n t d | ƒ } | j |  ƒ S(   sl   Quick calculation of a hash to identify uniquely Python objects
        containing numpy arrays.


        Parameters
        -----------
        hash_name: 'md5' or 'sha1'
            Hashing algorithm used. sha1 is supposedly safer, but md5 is
            faster.
        coerce_mmap: boolean
            Make no difference between np.memmap and np.ndarray
    RW   R    RV   (   R<   R@   RT   R   R   (   R&   R    RV   t   hasher(    (    s?   lib/python2.7/site-packages/sklearn/externals/joblib/hashing.pyR   ö   s    (   R   R   R   R<   R+   RN   R   R	   t   _compatR    R   t   _PicklerR   RR   R   R   R   RT   Rl   R   (    (    (    s?   lib/python2.7/site-packages/sklearn/externals/joblib/hashing.pyt   <module>   s    	mU