Path: blob/master/Convolutional Neural Networks/dummy/__pycache__/cache.cpython-36.pyc
24287 views
3
��Y� � @ s� d dl Z d dlZd dlZdd� Zdd� Zedkr�dd� Zed � ed
eddd
�Z ede � e� G dd� d�Z
ed� ede
ddd�Zej� dS )� Nc O sz t jj| �r<t| dd��}tj|�}W dQ R X td| � n:|||�}t| dd��}tj||� W dQ R X td| � |S )a�
Cache-wrapper for a function or class. If the cache-file exists
then the data is reloaded and returned, otherwise the function
is called and the result is saved to cache. The fn-argument can
also be a class instead, in which case an object-instance is
created and saved to the cache-file.
:param cache_path:
File-path for the cache-file.
:param fn:
Function or class to be called.
:param args:
Arguments to the function or class-init.
:param kwargs:
Keyword arguments to the function or class-init.
:return:
The result of calling the function or creating the object-instance.
�rb)�modeNz- Data loaded from cache-file: �wbz- Data saved to cache-file: )�os�path�exists�open�pickle�load�print�dump)�
cache_path�fn�args�kwargs�file�obj� r �!/home/jovyan/work/Resnet/cache.py�cache s
r c
C s2 t j| �}t|dd��}tj||� W dQ R X dS )a�
Convert a numpy-file to pickle-file.
The first version of the cache-function used numpy for saving the data.
Instead of re-calculating all the data, you can just convert the
cache-file using this function.
:param in_path:
Input file in numpy-format written using numpy.save().
:param out_path:
Output file written as a pickle-file.
:return:
Nothing.
r )r N)�npr
r r r )Zin_pathZout_path�datar r r r �convert_numpy2pickleJ s
r �__main__c C s | | S )Nr )�a�br r r �expensive_functioni s r z"Computing expensive_function() ...zcache_expensive_function.pkl�{ i� )r
r r r zresult =c @ s e Zd Zdd� Zdd� ZdS )�ExpensiveClassc C s || _ || _|| | _d S )N)�c�d�result)�selfr r r r r �__init__ s zExpensiveClass.__init__c C s( t d| j� t d| j� t d| j� d S )Nzc =zd =zresult = c * d =)r r r r! )r"