o
    i                     @  sl   d dl mZ d dlmZ d dlmZmZmZmZ d dl	m
Z
 er,d dlmZ d dlmZ G dd deZd	S )
    )annotations)TYPE_CHECKING)BufferCopyNotAllowedErrorDlpackDeviceType	DtypeKind)polars_dtype_to_dtype)NoReturn)Seriesc                   @  sZ   e Zd ZdZdddd
dZedddZedddZdddZdddZ	dddZ
dS ) PolarsBufferaQ  
    A buffer object backed by a Polars Series consisting of a single chunk.

    Parameters
    ----------
    data
        The Polars Series backing the buffer object.
    allow_copy
        Allow data to be copied during operations on this column. If set to `False`,
        a RuntimeError will be raised if data would be copied.
    T)
allow_copydatar
   r   boolreturnNonec                C  s.   |  dkr|sd}t|| }|| _d S )N   z-non-contiguous buffer must be made contiguous)Zn_chunksr   Zrechunk_data)selfr   r   msg r   P/home/app/Keep/.python/lib/python3.10/site-packages/polars/interchange/buffer.py__init__    s   
zPolarsBuffer.__init__intc                 C  sj   t | jj}|d tjkr*| j \}}}|| }t|d\}}|dkr&|S |d S | j |d d  S )zBuffer size in bytes.r      r   )r   r   dtyper   ZBOOL_get_buffer_infodivmodlen)r   r   _offsetlengthZn_bitsZn_bytesrestr   r   r   bufsize)   s   zPolarsBuffer.bufsizec                 C  s   | j  \}}}|S )z-Pointer to start of the buffer as an integer.)r   r   )r   Zpointerr   r   r   r   ptr:   s   zPolarsBuffer.ptrr	   c                 C  s   d}t |)z-Represent this structure as DLPack interface.
__dlpack__)NotImplementedError)r   r   r   r   r   r$   @   s   zPolarsBuffer.__dlpack__tuple[DlpackDeviceType, None]c                 C  s
   t jdfS )zCDevice type and device ID for where the data in the buffer resides.N)r   ZCPU)r   r   r   r   __dlpack_device__E   s   
zPolarsBuffer.__dlpack_device__strc                 C  s2   | j }| j}|  d j}d| d| d|dS )Nr   zPolarsBuffer(bufsize=z, ptr=z	, device=))r"   r#   r'   name)r   r"   r#   Zdevicer   r   r   __repr__I   s   zPolarsBuffer.__repr__N)r   r
   r   r   r   r   )r   r   )r   r	   )r   r&   )r   r(   )__name__
__module____qualname____doc__r   propertyr"   r#   r$   r'   r+   r   r   r   r   r      s    	

r   N)
__future__r   typingr   Zpolars.interchange.protocolr   r   r   r   Zpolars.interchange.utilsr   r	   Zpolarsr
   r   r   r   r   r   <module>   s    