o
    ;Di01                     @   s   d dl mZmZmZ d dlmZ d dlmZ d dlm	Z	 d dl
mZmZmZ d dlmZ 		d#dd	ZG d
d deZed$ddZed$ddZdd Z		d%ddZG dd deZdd Z				d&ddZee_d'dd Zee_d!d" ZdS )(    )absolute_importprint_functiondivision)contextmanager)string_types)ArgumentError)Tableiterpeekdata)infer_dtypeNc              
   C   s   t | |||||||dS )ap
  
    Provides access to an HDF5 table. E.g.::

        >>> import petl as etl
        >>>
        >>> # set up a new hdf5 table to demonstrate with
        >>> class FooBar(tables.IsDescription): # doctest: +SKIP
        ...     foo = tables.Int32Col(pos=0) # doctest: +SKIP
        ...     bar = tables.StringCol(6, pos=2) # doctest: +SKIP
        >>> #
        >>> def setup_hdf5_table():
        ...     import tables
        ...     h5file = tables.open_file('example.h5', mode='w',
        ...                               title='Example file')
        ...     h5file.create_group('/', 'testgroup', 'Test Group')
        ...     h5table = h5file.create_table('/testgroup', 'testtable', FooBar,
        ...                                   'Test Table')
        ...     # load some data into the table
        ...     table1 = (('foo', 'bar'),
        ...               (1, b'asdfgh'),
        ...               (2, b'qwerty'),
        ...               (3, b'zxcvbn'))
        ...     for row in table1[1:]:
        ...         for i, f in enumerate(table1[0]):
        ...             h5table.row[f] = row[i]
        ...         h5table.row.append()
        ...     h5file.flush()
        ...     h5file.close()
        >>>
        >>> setup_hdf5_table() # doctest: +SKIP
        >>>
        >>> # now demonstrate use of fromhdf5
        >>> table1 = etl.fromhdf5('example.h5', '/testgroup', 'testtable') # doctest: +SKIP
        >>> table1 # doctest: +SKIP
        +-----+-----------+
        | foo | bar       |
        +=====+===========+
        |   1 | b'asdfgh' |
        +-----+-----------+
        |   2 | b'qwerty' |
        +-----+-----------+
        |   3 | b'zxcvbn' |
        +-----+-----------+

        >>> # alternatively just specify path to table node
        ... table1 = etl.fromhdf5('example.h5', '/testgroup/testtable') # doctest: +SKIP
        >>> # ...or use an existing tables.File object
        ... h5file = tables.open_file('example.h5') # doctest: +SKIP
        >>> table1 = etl.fromhdf5(h5file, '/testgroup/testtable') # doctest: +SKIP
        >>> # ...or use an existing tables.Table object
        ... h5tbl = h5file.get_node('/testgroup/testtable') # doctest: +SKIP
        >>> table1 = etl.fromhdf5(h5tbl) # doctest: +SKIP
        >>> # use a condition to filter data
        ... table2 = etl.fromhdf5(h5tbl, condition='foo < 3') # doctest: +SKIP
        >>> table2 # doctest: +SKIP
        +-----+-----------+
        | foo | bar       |
        +=====+===========+
        |   1 | b'asdfgh' |
        +-----+-----------+
        |   2 | b'qwerty' |
        +-----+-----------+

        >>> h5file.close() # doctest: +SKIP

    )wherename	conditioncondvarsstartstopstep)HDF5Viewsourcer   r   r   r   r   r   r    r   I/var/www/Datamplify/venv/lib/python3.10/site-packages/petl/io/pytables.pyfromhdf5   s   Er   c                   @   s"   e Zd Z		dddZdd ZdS )r   Nc	           	      C   4   || _ || _|| _|| _|| _|| _|| _|| _d S Nr   )	selfr   r   r   r   r   r   r   r   r   r   r   __init__Z      
zHDF5View.__init__c              	   C   &   t | j| j| j| j| j| j| j| jS r   )	iterhdf5r   r   r   r   r   r   r   r   r   r   r   r   __iter__e      zHDF5View.__iter__NNNNNNN__name__
__module____qualname__r   r!   r   r   r   r   r   X   
    
r   rc                 c   s    dd l }d}d }t| |jr| }n,t| tr(|j| |d}d}|j||d}nt| |jr8| }|j||d}ntd|  z|V  W |rK|  d S d S |rT|  w w )Nr   FmodeT)r   z\invalid source argument, expected file name or tables.File or tables.Table object, found: %r)	tables
isinstancer   r   	open_fileget_nodeFiler   close)r   r   r   r+   r,   needs_closingh5fileh5tblr   r   r   _get_hdf5_tablej   s.   

r5   c                 c   sx    dd l }d}t| tr|j| |d}d}nt| |jr| }ntd|  z|V  W |r2|  d S d S |r;|  w w )Nr   Fr*   TzLinvalid source argument, expected file name or tables.File object, found: %r)r,   r-   r   r.   r0   r   r1   )r   r+   r,   r2   r3   r   r   r   _get_hdf5_file   s$   

r6   c                 c   s    t | ||4}t|j}	|	V  |d ur|j|||||d}
n|j|||d}
|
D ]	}|d d  V  q)W d    d S 1 s>w   Y  d S )N)r   r   r   r   )r   r   r   )r5   tuplecolnamesr   iterrows)r   r   r   r   r   r   r   r   r4   hdritrowr   r   r   r      s   
"r   Fc              
   C   s(   |dusJ dt | |||||||dS )aB  
    Provides access to an HDF5 table, sorted by an indexed column, e.g.::

        >>> import petl as etl
        >>>
        >>> # set up a new hdf5 table to demonstrate with
        >>> class FooBar(tables.IsDescription): # doctest: +SKIP
        ...     foo = tables.Int32Col(pos=0) # doctest: +SKIP
        ...     bar = tables.StringCol(6, pos=2) # doctest: +SKIP
        >>>
        >>> def setup_hdf5_index():
        ...     import tables
        ...     h5file = tables.open_file('example.h5', mode='w',
        ...                               title='Example file')
        ...     h5file.create_group('/', 'testgroup', 'Test Group')
        ...     h5table = h5file.create_table('/testgroup', 'testtable', FooBar,
        ...                                   'Test Table')
        ...     # load some data into the table
        ...     table1 = (('foo', 'bar'),
        ...               (1, b'asdfgh'),
        ...               (2, b'qwerty'),
        ...               (3, b'zxcvbn'))
        ...     for row in table1[1:]:
        ...         for i, f in enumerate(table1[0]):
        ...             h5table.row[f] = row[i]
        ...         h5table.row.append()
        ...     h5table.cols.foo.create_csindex()  # CS index is required
        ...     h5file.flush()
        ...     h5file.close()
        >>>
        >>> setup_hdf5_index() # doctest: +SKIP
        >>>
        ... # access the data, sorted by the indexed column
        ... table2 = etl.fromhdf5sorted('example.h5', '/testgroup', 'testtable', sortby='foo') # doctest: +SKIP
        >>> table2 # doctest: +SKIP
        +-----+-----------+
        | foo | bar       |
        +=====+===========+
        |   1 | b'zxcvbn' |
        +-----+-----------+
        |   2 | b'qwerty' |
        +-----+-----------+
        |   3 | b'asdfgh' |
        +-----+-----------+

    Nzno column specified to sort by)r   r   sortbycheckCSIr   r   r   )HDF5SortedViewr   r   r   r=   r>   r   r   r   r   r   r   fromhdf5sorted   s
   1rA   c                   @   s"   e Zd Z		dddZdd ZdS )r?   NFc	           	      C   r   r   r@   )	r   r   r   r   r=   r>   r   r   r   r   r   r   r      r   zHDF5SortedView.__init__c              	   C   r   r   )	iterhdf5sortedr   r   r   r=   r>   r   r   r   r    r   r   r   r!     r"   zHDF5SortedView.__iter__NNNFNNNr$   r   r   r   r   r?      r(   r?   c                 c   sp    t | ||'}t|j}	|	V  |j|||||d}
|
D ]	}|d d  V  qW d    d S 1 s1w   Y  d S )N)r>   r   r   r   )r5   r7   r8   
itersorted)r   r   r   r=   r>   r   r   r   r4   r:   r;   r<   r   r   r   rB     s   
"rB    '    c                 C   s   ddl }t| }|rXt|dd@}|r,z||| W n
 |jy%   Y nw ||| |du r;t||\}}t|}|j||||||	|
||d	 W d   n1 sSw   Y  t	|||dd}|
d t|| W d   dS 1 svw   Y  dS )a  
    Write to an HDF5 table. If `create` is `False`, assumes the table
    already exists, and attempts to truncate it before loading. If `create`
    is `True`, a new table will be created, and if `drop` is True,
    any existing table will be dropped first. If `description` is `None`,
    the description will be guessed. E.g.::

        >>> import petl as etl
        >>> table1 = (('foo', 'bar'),
        ...           (1, b'asdfgh'),
        ...           (2, b'qwerty'),
        ...           (3, b'zxcvbn'))
        >>> etl.tohdf5(table1, 'example.h5', '/testgroup', 'testtable',
        ...            drop=True, create=True, createparents=True) # doctest: +SKIP
        >>> etl.fromhdf5('example.h5', '/testgroup', 'testtable') # doctest: +SKIP
        +-----+-----------+
        | foo | bar       |
        +=====+===========+
        |   1 | b'asdfgh' |
        +-----+-----------+
        |   2 | b'qwerty' |
        +-----+-----------+
        |   3 | b'zxcvbn' |
        +-----+-----------+

    r   Nar*   )titlefiltersexpectedrows
chunkshape	byteordercreateparents)r,   iterr6   r/   NoSuchNodeErrorremove_noder	   r   create_tabler5   truncate_insert)tabler   r   r   createdropdescriptionrI   rJ   rK   rL   rM   rN   sampler,   r;   r3   peekh5tabler   r   r   tohdf5!  s6   

"r\   c                 C   s@   t |||dd}t| | W d   dS 1 sw   Y  dS )za
    As :func:`petl.io.hdf5.tohdf5` but don't truncate the target table before
    loading.

    rH   r*   N)r5   rT   )rU   r   r   r   r[   r   r   r   
appendhdf5i  s   "r]   c                 C   sJ   t | }|D ]}t|jD ]\}}|| |j|< q|j  q|  d S r   )r
   	enumerater8   r<   appendflush)rU   r[   r;   r<   ifr   r   r   rT   y  s   rT   r#   )r)   rC   )NNFFNrE   NrF   NNFrG   )NN)
__future__r   r   r   
contextlibr   petl.compatr   petl.errorsr   petl.util.baser   r	   r
   petl.io.numpyr   r   r   r5   r6   r   rA   r?   rB   r\   r]   rT   r   r   r   r   <module>   s:   
J(
7
E
