Your IP : 216.73.216.213


Current Path : /opt/alt/python35/lib/python3.5/site-packages/joblib/test/__pycache__/
Upload File :
Current File : //opt/alt/python35/lib/python3.5/site-packages/joblib/test/__pycache__/test_hashing.cpython-35.pyc



��X>�@sLdZddlZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlm
Z
mZddlmZddlmZddlmZddlmZddlmZdd	lmZdd
lmZddlmZm Z ddl!m"Z"dd
l#m$Z$ye%d�Wne&k
rtdd�Z%YnXdd�Z'dd�Z(dd�Z)Gdd�de*�Z+Gdd�de*�Z,dd�Z-dd�Z.e dd ��Z/e d!d"��Z0e d#d$��Z1e d%d&��Z2e d'd(��Z3d)d*�Z4ej5j6ee�d+d,��Z7e d-d.��Z8e d/d0��Z9ej5j6ee�d1d2��Z:ej5j6ee�d3d4��Z;d5d6�Z<e d7d8��Z=d9d:�Z>e d;d<��Z?e d=d>��Z@e d?d@��ZAe dAdB��ZBdCdD�ZCdS)Ez
Test the hashing module.
�N)�assert_equal�assert_not_equal)�hash)�filter_args)�Memory)�assert_raises_regex)�env)�setup_module)�teardown_module)�np�
with_numpy)�TransportableException)�PY3_OR_LATER�strcCs|S)N�)�srr�I/opt/alt/python35/lib/python3.5/site-packages/joblib/test/test_hashing.py�<lambda>'srcCs||krtd��dS)Nz%r is not lower than %r)�AssertionError)�a�brrr�assert_less*srcGsWt�}xAtd�D]3}tj�}||�|jtj�|�qWt|�S)z" Time function func on *args.
    �)�list�range�time�append�min)�func�args�times�_�t1rrr�	time_func1s	
r#cGs>t||�}t||�}dt||�||}|S)zP Return the relative time between func1 and func2 applied on
        *args.
    g�?)r#�abs)Zfunc1Zfunc2rZ
time_func1Z
time_func2�
relative_diffrrr�
relative_time<s
r&c@seZdZdd�ZdS)�KlasscCs|S)Nr)�self�xrrr�fIszKlass.fN)�__name__�
__module__�__qualname__r*rrrrr'Gsr'c@s(eZdZdd�Zdd�ZdS)�KlassWithCachedMethodcCs,tdtd�}|j|j�|_dS)N�cachedir�dir)r�test_memory_env�cacher*)r(Zmemrrr�__init__OszKlassWithCachedMethod.__init__cCs|S)Nr)r(r)rrrr*SszKlassWithCachedMethod.fN)r+r,r-r3r*rrrrr.Msr.ccs�ddddd
ddddd
dgddgddiddiddidtjdgjtd�td�ddddiddddd	ddiig}xK|D]C}x:|D]2}tjjt|�t|�k||kfVq�Wq�WdS)z' Smoke test hash on various types.
    ��g�?g@y�?rrN�dy�?�?y@�?)r4)r4r4)rr4�rr4)rr4r7)�gc�collectr�set�nose�toolsrr)�obj_list�obj1�obj2rrr�test_trival_hashZs			!

r@cCs�tjtd��}tjjt|j�t|j��tj	t
d��}tj	t
d��}tjjt|j�t|j��dS)Nr�
�	)
�io�StringIO�unicoder;r<rr�flush�collections�dequerr�extend)r�a1�a2rrr�test_hash_methodsss
%rLc
csJtjjd�}|jd�}|j�}|j�}|dd7<|||f}xT|D]L}xC|D];}tjjt|�t|�ktj	||k�fVqjWq]Wd|d|i}d|d|i}tjjt|�t|�fVd|d|i}	tjj
t|�t|	�fVtjj
t|�t|j�fVdS)z% Test hashing with numpy arrays.
    rrAr4r5N)rArA)r�random�RandomState�
random_sample�copyr;r<rr�allr�T)
�rnd�arr1�arr2Zarr3r=r>r?�d1�d2�d3rrr�test_hash_numpy|s 

  rYcCsbddg}ttjd��}dd�|D�}x'|D]}tjjt|�|�q;WdS)Nz
datetime64[s]ztimedelta64[D]rAcss'|]}tjddd|�VqdS)rrA�dtypeN)r�arange)�.0rZrrr�	<genexpr>�sz,test_numpy_datetime_array.<locals>.<genexpr>)rrr[r;r<r)�dtypesZa_hash�arrays�arrayrrr�test_numpy_datetime_array�s

racCs�tjtjd�jd�dd�dd�dd�dd�f}tj|�}tjjt|�t|��tj	|�}tjjt|�t|��dS)	Nip��r5r�order�Fr4)rbr5r)
r�asarrayr[�reshape�ascontiguousarrayr;r<rr�asfortranarray)rr�crrr�test_hash_numpy_noncontiguous�s(rjccs�tjdd�}zqtj|dd
dd�}tj|�}x@dD]8}tjjt|d	|�t|d	|�k|fVqFWWd
dt	�kr�~t
j�ytj
|�Wn:tk
r�}ztjdks�|�WYd
d
}~XnXXd
S)zS Check that memmap and arrays hash identically if coerce_mmap is
        True.
    �prefixZjoblib_test_hash_memmap_�shaperA�modezw+FT�coerce_mmapN�m�nt)rArA)FT)�tempfile�mktempr�memmaprer;r<rr�localsr8r9�os�unlink�OSError�name)�filenamerorrn�errr�test_hash_memmap�s"
	
r{cs�tjdkrtj��tjjd�}|jd�}ttd�rWtj	�nt
��fdd�}t|t|�}t
|d�dt||�}tt|||f�}d	t||�||}t
|d�d
S)a Check the performance of hashing numpy arrays:

        In [22]: a = np.random.random(1000000)

        In [23]: %timeit hashlib.md5(a).hexdigest()
        100 loops, best of 3: 20.7 ms per loop

        In [24]: %timeit hashlib.md5(pickle.dumps(a, protocol=2)).hexdigest()
        1 loops, best of 3: 73.1 ms per loop

        In [25]: %timeit hashlib.md5(cPickle.dumps(a, protocol=2)).hexdigest()
        10 loops, best of 3: 53.9 ms per loop

        In [26]: %timeit hash(a)
        100 loops, best of 3: 20.8 ms per loop
    �win32ri@B�	getbuffercstj�|��j�S)N)�hashlib�md5�	hexdigest)r))r}rrr�sz-test_hash_numpy_performance.<locals>.<lambda>g333333�?rg�?N)�sys�platformr;�SkipTestrrMrNrO�hasattrr}�
memoryviewr&rrr#r$)rSrZmd5_hashr%Ztime_hashlibZ	time_hashr)r}r�test_hash_numpy_performance�s
r�cCsSt�}t�}tjjtt|jgd��tt|jgd���dS)z~ Make sure that calling the same method on two different instances
    of the same class does resolve to the same hashes.
    r4N)r4)r4)r'r;r<rrrr*)rrrrr�test_bound_methods_hash�s		!r�cCsYt�}t�}tjjtt|jjgd��tt|jjgd���dS)z� Make sure that calling the same _cached_ method on two different
    instances of the same class does resolve to the same hashes.
    r4N)r4)r4)r.r;r<rrrr*r)rrrrr�test_bound_cached_methods_hash�s		$r�cCsstjdd�td�D�dt�}tjdd�td�D�dt�}tjjt|�t|��dS)z< Make sure that ndarrays with dtype `object' hash correctly.cSsg|]}tj|��qSr)rr[)r\�irrr�
<listcomp>
s	z*test_hash_object_dtype.<locals>.<listcomp>�rZcSsg|]}tj|��qSr)rr[)r\r�rrrr�s	N)rr`r�objectr;r<rr)rrrrr�test_hash_object_dtype	s((r�cCsAtjd�}tjd�}tjjt|�t|��dS)Ng@g@)r�float64r;r<rr)rrrrr�test_numpy_scalarsr�cCs�t�}ddgddgddgddgddgddgddgd	dgd
dgddgddgd
dgddgi
}|j|�}|j|�}tjjt|�t|��dS)Nz#s12069__c_maps.nii.gz�!z#s12158__c_maps.nii.gzz#s12258__c_maps.nii.gzz#s12277__c_maps.nii.gzz#s12300__c_maps.nii.gzz#s12401__c_maps.nii.gzz#s12430__c_maps.nii.gzz#s13817__c_maps.nii.gzz#s13903__c_maps.nii.gzz#s13916__c_maps.nii.gzz#s13981__c_maps.nii.gzz#s13982__c_maps.nii.gzz#s13983__c_maps.nii.gz)r.r*r;r<rr)�kr6rrrrr�test_dict_hashs$													r�cCs}t�}tddddddddd	d
ddd
g
�}|j|�}|j|�}tjjt|�t|��dS)Nz#s12069__c_maps.nii.gzz#s12158__c_maps.nii.gzz#s12258__c_maps.nii.gzz#s12277__c_maps.nii.gzz#s12300__c_maps.nii.gzz#s12401__c_maps.nii.gzz#s12430__c_maps.nii.gzz#s13817__c_maps.nii.gzz#s13903__c_maps.nii.gzz#s13916__c_maps.nii.gzz#s13981__c_maps.nii.gzz#s13982__c_maps.nii.gzz#s13983__c_maps.nii.gz)r.r:r*r;r<rr)r�rrrrrr�
test_set_hash8s"	r�cCs_d}|di}|di}tjtj|��}tt||g�t||g��dS)N�foo�bar)�pickle�loads�dumpsrr)�stringrrrirrr�test_stringRs
r�cCsntjdtjfdtjfg�}|}tjtj|��}tt||g�t||g��dS)N�f1�f2)	rrZ�uint�int32r�r�r�rr)rrrirrr�
test_dtype\s'r�c#s�tjd��ddd!�fdd�td	�D�d
ddtdt�gd
ddd"dd
gig}dddddddgdddddddgi}tr�dnd}||}x1t||�D] \}}tt|�|fVq�WdS)#N�*zThis is a string to hashuC'est lété�@��1�ícsg|]}�j��qSr)rM)r\r!)�rngrrr�ps	z-test_hashes_stay_the_same.<locals>.<listcomp>�r�abczthe messageZabcde�{Zsadfasi'r5�py2Z 80436ada343b0d79a99bfd8883a96e45Z 2ff3a25200eb6219f468de2640913c2dZ 50d81c80af05061ac4dcdc2d5edee6d6Z 536af09b66a087ed18b515acc17dc7fcZ 123ffc6f13480767167e171a8e1f6f4aZ fc9314a39ff75b829498380850447047�py3Z 71b3f47df22cb19431d85d92d0b230b2Z 2d8d189e9b2b0b2e384d93c868c0e576Z e205227dd82250871fa25aa0ec690aa3Z 9e4e9bf9b91890c9734a6111a35e6633Z 6065a3c48e842ea5dee2cfd0d6820ad6Z aeda150553d4bb5c69f0e69d51b0e2ef�3~��)r�r�r�i���)	rM�Randomrr
�
ValueErrorr�ziprr)�to_hash_list�
expected_dict�py_version_str�
expected_list�to_hash�expectedr)r�r�test_hashes_stay_the_samegs0	
r�cCsMtjjd�}|jd�}tj|�}tt|�t|��dS)NrrA)rArA)rrMrNrOrhrr)r�Zarr_cZarr_frrr�Atest_hashes_are_different_between_c_and_fortran_contiguous_arrays�sr�cCsttjd��dS)Nr)rrr`rrrr�
test_0d_array�sr�cCs2tttjd��ttjdg���dS)Nr)rrrr`rrrr�)test_0d_and_1d_array_hashing_is_different�sr�c
#s�tjjd���jd%dddd�jd�t�fdd�td	�D���fd
d�td	�D�d&�jd
d	�jd�d�jddd�jd��jd�jd�gitjddd�j	d'�tj
tjddd�j	d(��tjddd�j	d)�dd�dd�fg}ddddddddgdddd d!d"d#d$gi}tr�dnd}||}x1t||�D] \}}t
t|�|fVq�WdS)*Nr�i��high�size�2z<i8c3s'|]}�jd�jd�VqdS)rz<f4N)�randn�astype)r\r!)r�rrr]�sz?test_hashes_stay_the_same_with_numpy_objects.<locals>.<genexpr>r�cs(g|]}�jd�jd��qS)rz<f4)r�r�)r\r!)r�rrr��s	z@test_hashes_stay_the_same_with_numpy_objects.<locals>.<listcomp>i
rz<f4rrA��drZr5r�Z 80f2387e7752abbda2658aafed49e086Z 0d700f7f25ea670fd305e4cd93b0e8cdZ 83a2bdf843e79e4b3e26521db73088b9Z 63e0efd43c0a9ad92a07e8ce04338dd3Z 03fef702946b602c852b8b4e60929914Z 07074691e90d7098a85956367045c81eZ d264cf79f353aa7bbfa8349e3df72d8fr�Z 10a6afc379ca2708acfbaef0ab676eabZ 988a7114f337f381393025911ebc823bZ c6809f4b97e35f2fa0ee8d653cbd025cZ b3ad17348e32728a7eb9cda1e7ede438Z 927b3e6b0b6a037e8e035bda134e0b05Z 108f6ee98e7db19ea2006ffd208f4bf1Z bd48ccaaff28e16e6badee81041b7180i���i��)rArA)rArA)rArA)rrMrN�randintr��tuplerr�r[rfrhrr�rr)r�r�r�r�r�r�r)r�r�,test_hashes_stay_the_same_with_numpy_objects�s:!"$7
r�cCs&dd�}ttjdt|�dS)NcSsdS)Nr�rrrrr�
non_picklable�sz2test_hashing_pickling_error.<locals>.non_picklablezPicklingError while hashing)rr��
PicklingErrorr)r�rrr�test_hashing_pickling_error�s	r�)D�__doc__r;rr~rqrur�r8rCrGr�rM�
nose.toolsrrZjoblib.hashingrZjoblib.func_inspectrZ
joblib.memoryrZjoblib.testingrZjoblib.test.test_memoryrr1r	Ztest_memory_setup_funcr
Ztest_memory_teardown_funcZjoblib.test.commonrrZjoblib.my_exceptionsr
Zjoblib._compatrrE�	NameErrorrr#r&r�r'r.r@rLrYrarjr{r�r�r<�
with_setupr�r�r�r�r�r�r�r�r�r�r�r�r�rrrr�<module>sh

	+
!	!!
$
7