CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
y33-j3T

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: y33-j3T/Coursera-Deep-Learning
Path: blob/master/Convolutional Neural Networks/dummy/__pycache__/cache.cpython-36.pyc
Views: 13377
3

��Y��@s�ddlZddlZddlZdd�Zdd�Zedkr�dd�Zed	�ed
eddd
�Z	ede	�e�Gdd�d�Z
ed�ede
ddd�Zej�dS)�NcOsztjj|�r<t|dd��}tj|�}WdQRXtd|�n:|||�}t|dd��}tj||�WdQRXtd|�|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�caches
rc
Cs2tj|�}t|dd��}tj||�WdQRXdS)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)rN)�npr
rr	r)Zin_pathZout_path�datarrrr�convert_numpy2pickleJs
r�__main__cCs||S)Nr)�a�brrr�expensive_functionisrz"Computing expensive_function() ...zcache_expensive_function.pkl�{i�)r
rrrzresult =c@seZdZdd�Zdd�ZdS)�ExpensiveClasscCs||_||_|||_dS)N)�c�d�result)�selfrr rrr�__init__szExpensiveClass.__init__cCs(td|j�td|j�td|j�dS)Nzc =zd =zresult = c * d =)rrr r!)r"rrr�print_result�szExpensiveClass.print_resultN)�__name__�
__module__�__qualname__r#r$rrrrr~srz)Creating object from ExpensiveClass() ...zcache_ExpensiveClass.pkl)r
rrr )
rr	�numpyrrrr%rrr!rrr$rrrr�<module>s ,