B
    x\                 @   sz   d Z ddlZddlZddlmZmZ ddlmZm	Z	m
ZmZ ddlmZmZ dejfddZdd	d
Zdd Zdd ZdS )z pickle compat     N)
read_arraywrite_array)PY3BytesIOcPicklepickle_compat)_get_handle_stringify_pathinferc          	   C   s`   t |}t|d|dd\}}|dk r*tj}z|tj| |d W dx|D ]}|  qJW X dS )a  
    Pickle (serialize) object to file.

    Parameters
    ----------
    obj : any object
        Any python object.
    path : str
        File path where the pickled object will be stored.
    compression : {'infer', 'gzip', 'bz2', 'zip', 'xz', None}, default 'infer'
        A string representing the compression to use in the output file. By
        default, infers from the file extension in specified path.

        .. versionadded:: 0.20.0
    protocol : int
        Int which indicates which protocol should be used by the pickler,
        default HIGHEST_PROTOCOL (see [1], paragraph 12.1.2). The possible
        values for this parameter depend on the version of Python. For Python
        2.x, possible values are 0, 1, 2. For Python>=3.0, 3 is a valid value.
        For Python >= 3.4, 4 is a valid value. A negative value for the
        protocol parameter is equivalent to setting its value to
        HIGHEST_PROTOCOL.

        .. [1] https://docs.python.org/3/library/pickle.html
        .. versionadded:: 0.21.0

    See Also
    --------
    read_pickle : Load pickled pandas object (or any object) from file.
    DataFrame.to_hdf : Write DataFrame to an HDF5 file.
    DataFrame.to_sql : Write DataFrame to a SQL database.
    DataFrame.to_parquet : Write a DataFrame to the binary parquet format.

    Examples
    --------
    >>> original_df = pd.DataFrame({"foo": range(5), "bar": range(5, 10)})
    >>> original_df
       foo  bar
    0    0    5
    1    1    6
    2    2    7
    3    3    8
    4    4    9
    >>> pd.to_pickle(original_df, "./dummy.pkl")

    >>> unpickled_df = pd.read_pickle("./dummy.pkl")
    >>> unpickled_df
       foo  bar
    0    0    5
    1    1    6
    2    2    7
    3    3    8
    4    4    9

    >>> import os
    >>> os.remove("./dummy.pkl")
    wbF)compressionis_textr   )protocolN)r	   r   pklHIGHEST_PROTOCOLwritedumpsclose)objpathr   r   ffh_f r   /lib/python3.7/site-packages/pandas/io/pickle.py	to_pickle   s    :
r   c                sX   t  fdddfdd	}y|S  tk
rR   trL|ddS  Y nX dS )	a@  
    Load pickled pandas object (or any object) from file.

    .. warning::

       Loading pickled data received from untrusted sources can be
       unsafe. See `here <https://docs.python.org/3/library/pickle.html>`__.

    Parameters
    ----------
    path : str
        File path where the pickled object will be loaded.
    compression : {'infer', 'gzip', 'bz2', 'zip', 'xz', None}, default 'infer'
        For on-the-fly decompression of on-disk data. If 'infer', then use
        gzip, bz2, xz or zip if path ends in '.gz', '.bz2', '.xz',
        or '.zip' respectively, and no decompression otherwise.
        Set to None for no decompression.

        .. versionadded:: 0.20.0

    Returns
    -------
    unpickled : same type as object stored in file

    See Also
    --------
    DataFrame.to_pickle : Pickle (serialize) DataFrame object to file.
    Series.to_pickle : Pickle (serialize) Series object to file.
    read_hdf : Read HDF5 file into a DataFrame.
    read_sql : Read SQL query or database table into a DataFrame.
    read_parquet : Load a parquet object, returning a DataFrame.

    Examples
    --------
    >>> original_df = pd.DataFrame({"foo": range(5), "bar": range(5, 10)})
    >>> original_df
       foo  bar
    0    0    5
    1    1    6
    2    2    7
    3    3    8
    4    4    9
    >>> pd.to_pickle(original_df, "./dummy.pkl")

    >>> unpickled_df = pd.read_pickle("./dummy.pkl")
    >>> unpickled_df
       foo  bar
    0    0    5
    1    1    6
    2    2    7
    3    3    8
    4    4    9

    >>> import os
    >>> os.remove("./dummy.pkl")
    c          	      s:   t d dd\}}z| |S x|D ]}|  q$W X d S )NrbF)r   r   )r   r   )funcr   r   r   )r   r   r   r   read_wrapper   s    
z!read_pickle.<locals>.read_wrapperNc                s|   y0t jdd t dt dd S Q R X W nF tk
rv   y fddS  tk
rp    fddS X Y nX d S )NT)recordignorec             S   s
   t | S )N)r   load)r   r   r   r   <lambda>   s    z/read_pickle.<locals>.try_read.<locals>.<lambda>c                s   t j|  ddS )NF)encodingcompat)pcr!   )r   )r#   r   r   r"      s    c                s   t j|  ddS )NT)r#   r$   )r%   r!   )r   )r#   r   r   r"      s    )warningscatch_warningssimplefilterWarning	Exception)r   r#   )r   )r#   r   try_read   s    	zread_pickle.<locals>.try_readlatin1)r#   )N)r	   r*   r   )r   r   r+   r   )r   r   r   r   read_pickleS   s    9r-   c             C   s$   |  tj} t }t||  | S )N)ZviewnpZndarrayr   r   getvalue)arrZbufr   r   r   _pickle_array   s    
r1   c             C   s   t t| }|S )N)r   r   )bytesr0   r   r   r   _unpickle_array   s    r3   )r
   )__doc__r&   Znumpyr.   Znumpy.lib.formatr   r   Zpandas.compatr   r   r   r   r   r%   Zpandas.io.commonr   r	   r   r   r-   r1   r3   r   r   r   r   <module>   s   G
j	