a
    5PÎj¹&  ã                   @   s<  d dl Z d dlZd dlmZmZmZmZmZm	Z	m
Z
mZmZmZmZmZmZmZmZmZ d dlmZ G dd„ dejƒZdZG dd„ dejƒZG dd	„ d	ejƒZG d
d„ dejƒZdd„ Zdd„ Zdddœdd„Z d !e¡e _"dddœdd„Z#dddœdd„Z$d !e¡e$_"d#dddœdd„Z%dddœdd„Z&dd œd!d"„Z'dS )$é    N)ÚIpcReadOptionsÚIpcWriteOptionsÚ	ReadStatsÚ
WriteStatsÚMessageÚMessageReaderÚRecordBatchReaderÚ_ReadPandasMixinÚMetadataVersionÚread_messageÚread_record_batchÚread_schemaÚread_tensorÚwrite_tensorÚget_record_batch_sizeÚget_tensor_sizec                   @   s    e Zd ZdZdddœdd„ZdS )ÚRecordBatchStreamReadera  
    Reader for the Arrow streaming binary format.

    Parameters
    ----------
    source : bytes/buffer-like, pyarrow.NativeFile, or file-like Python object
        Either an in-memory buffer, or a readable file object.
        If you want to use memory map use MemoryMappedFile as source.
    options : pyarrow.ipc.IpcReadOptions
        Options for IPC deserialization.
        If None, default values will be used.
    memory_pool : MemoryPool, default None
        If None, default memory pool is used.
    N©ÚoptionsÚmemory_poolc                C   s   t |ƒ}| j|||d� d S )Nr   ©Ú _ensure_default_ipc_read_optionsÚ_open)ÚselfÚsourcer   r   © r   ú[/mounts/lovelace/software/anaconda3/envs/metadmg/lib/python3.9/site-packages/pyarrow/ipc.pyÚ__init__2   s    z RecordBatchStreamReader.__init__©Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   "   s   r   aN  Parameters
----------
sink : str, pyarrow.NativeFile, or file-like Python object
    Either a file path, or a writable file object.
schema : pyarrow.Schema
    The Arrow schema for data to be written to the file.
options : pyarrow.ipc.IpcWriteOptions
    Options for IPC serialization.

    If None, default values will be used: the legacy format will not
    be used unless overridden by setting the environment variable
    ARROW_PRE_0_15_IPC_FORMAT=1, and the V5 metadata version will be
    used unless overridden by setting the environment variable
    ARROW_PRE_1_0_METADATA_VERSION=1.
use_legacy_format : bool, default None
    Deprecated in favor of setting options. Cannot be provided with
    options.

    If None, False will be used unless this default is overridden by
    setting the environment variable ARROW_PRE_0_15_IPC_FORMAT=1c                   @   s&   e Zd Zd e¡Zdddœdd„ZdS )ÚRecordBatchStreamWriterz0Writer for the Arrow streaming binary format

{}N©Úuse_legacy_formatr   c                C   s   t ||ƒ}| j|||d� d S ©N©r   ©Ú_get_legacy_format_defaultr   ©r   ÚsinkÚschemar%   r   r   r   r   r   S   s    
z RecordBatchStreamWriter.__init__©r   r    r!   ÚformatÚ_ipc_writer_class_docr"   r   r   r   r   r   r#   N   s   þr#   c                   @   s"   e Zd ZdZddddœdd„ZdS )ÚRecordBatchFileReadera»  
    Class for reading Arrow record batch data from the Arrow binary file format

    Parameters
    ----------
    source : bytes/buffer-like, pyarrow.NativeFile, or file-like Python object
        Either an in-memory buffer, or a readable file object.
        If you want to use memory map use MemoryMappedFile as source.
    footer_offset : int, default None
        If the file is embedded in some larger file, this is the byte offset to
        the very end of the file data
    options : pyarrow.ipc.IpcReadOptions
        Options for IPC serialization.
        If None, default values will be used.
    memory_pool : MemoryPool, default None
        If None, default memory pool is used.
    Nr   c                C   s   t |ƒ}| j||||d� d S )N©Úfooter_offsetr   r   r   )r   r   r2   r   r   r   r   r   r   k   s    ÿzRecordBatchFileReader.__init__)Nr   r   r   r   r   r0   X   s   ÿr0   c                   @   s&   e Zd Zd e¡Zdddœdd„ZdS )ÚRecordBatchFileWriterz1Writer to create the Arrow binary file format

{}Nr$   c                C   s   t ||ƒ}| j|||d� d S r&   r(   r*   r   r   r   r   x   s    
zRecordBatchFileWriter.__init__r-   r   r   r   r   r3   r   s   þr3   c                 C   sŠ   | d ur|d urt dƒ‚n$|r>t|tƒs:td t|ƒ¡ƒ‚|S tj}| d u rbtt	t
j dd¡ƒƒ} tt	t
j dd¡ƒƒr~tj}t| |d�S )Nz8Can provide at most one of options and use_legacy_formatz expected IpcWriteOptions, got {}ZARROW_PRE_0_15_IPC_FORMATÚ0ZARROW_PRE_1_0_METADATA_VERSION)r%   Úmetadata_version)Ú
ValueErrorÚ
isinstancer   Ú	TypeErrorr.   Útyper
   ZV5ÚboolÚintÚosÚenvironÚgetZV4)r%   r   r5   r   r   r   r)   }   s&    ÿ
ÿÿÿr)   c                 C   s*   | r t | tƒs td t| ƒ¡ƒ‚| p(tƒ S )Nzexpected IpcReadOptions, got {})r7   r   r8   r.   r9   r'   r   r   r   r   ‘   s
    ÿr   r$   c                C   s   t | |||d�S ©Nr$   )r#   ©r+   r,   r%   r   r   r   r   Ú
new_stream™   s    þrA   zjCreate an Arrow columnar IPC stream writer instance

{}

Returns
-------
writer : RecordBatchStreamWriter
r   c                C   s   t | ||d�S )aô  
    Create reader for Arrow streaming format.

    Parameters
    ----------
    source : bytes/buffer-like, pyarrow.NativeFile, or file-like Python object
        Either an in-memory buffer, or a readable file object.
    options : pyarrow.ipc.IpcReadOptions
        Options for IPC serialization.
        If None, default values will be used.
    memory_pool : MemoryPool, default None
        If None, default memory pool is used.
    Returns
    -------
    reader : RecordBatchStreamReader
    r   )r   )r   r   r   r   r   r   Úopen_streamª   s    ÿrB   c                C   s   t | |||d�S r?   )r3   r@   r   r   r   Únew_file¿   s    þrC   zfCreate an Arrow columnar IPC file writer instance

{}

Returns
-------
writer : RecordBatchFileWriter
c                C   s   t | |||d�S )aŠ  
    Create reader for Arrow file format.

    Parameters
    ----------
    source : bytes/buffer-like, pyarrow.NativeFile, or file-like Python object
        Either an in-memory buffer, or a readable file object.
    footer_offset : int, default None
        If the file is embedded in some larger file, this is the byte offset to
        the very end of the file data.
    options : pyarrow.ipc.IpcReadOptions
        Options for IPC serialization.
        If None, default values will be used.
    memory_pool : MemoryPool, default None
        If None, default memory pool is used.
    Returns
    -------
    reader : RecordBatchFileReader
    r1   )r0   )r   r2   r   r   r   r   r   Ú	open_fileÐ   s    þrD   ©ÚnthreadsÚpreserve_indexc                C   sZ   t jj| ||d�}t  ¡ }t  ||j¡�}| |¡ W d  ƒ n1 sH0    Y  | ¡ S )a   
    Serialize a pandas DataFrame into a buffer protocol compatible object.

    Parameters
    ----------
    df : pandas.DataFrame
    nthreads : int, default None
        Number of threads to use for conversion to Arrow, default all CPUs.
    preserve_index : bool, default None
        The default of None will store the index as a column, except for
        RangeIndex which is stored as metadata only. If True, always
        preserve the pandas index data as a column. If False, no index
        information is saved and the result will have a default RangeIndex.

    Returns
    -------
    buf : buffer
        An object compatible with the buffer protocol.
    rE   N)ÚpaZRecordBatchZfrom_pandasZBufferOutputStreamr#   r,   Zwrite_batchÚgetvalue)ZdfrF   rG   Úbatchr+   Úwriterr   r   r   Úserialize_pandasé   s    
ÿ(rL   T©Úuse_threadsc                C   sH   t  | ¡}t  |¡�}| ¡ }W d  ƒ n1 s20    Y  |j|d�S )aR  Deserialize a buffer protocol compatible object into a pandas DataFrame.

    Parameters
    ----------
    buf : buffer
        An object compatible with the buffer protocol.
    use_threads : bool, default True
        Whether to parallelize the conversion using multiple threads.

    Returns
    -------
    df : pandas.DataFrame
    NrM   )rH   ZBufferReaderr   Zread_allZ	to_pandas)ÚbufrN   Zbuffer_readerÚreaderÚtabler   r   r   Údeserialize_pandas  s    
&rR   )N)(r<   ZpyarrowrH   Zpyarrow.libr   r   r   r   r   r   r   r	   r
   r   r   r   r   r   r   r   ÚlibZ_RecordBatchStreamReaderr   r/   Z_RecordBatchStreamWriterr#   Z_RecordBatchFileReaderr0   Z_RecordBatchFileWriterr3   r)   r   rA   r.   r"   rB   rC   rD   rL   rR   r   r   r   r   Ú<module>   s,   H
øø