3
6ßL]õ  ã               @   sD   d dl Zd dlmZ d dlmZ G dd„ deƒZG dd„ deƒZ	dS )é    N)Ú	DataFrame)ÚTablec               @   s    e Zd ZdZdd„ Zdd„ ZdS )Ú_TableRowAccessorzH A simple object which provides read access to the rows in a Table.
    c             C   s
   || _ d S )N)Ú	_h5_table)ÚselfZh5_table© r   ú?/tmp/pip-install-5sebvq2j/apptools/apptools/io/h5/table_node.pyÚ__init__	   s    z_TableRowAccessor.__init__c             C   s
   | j | S )N)r   )r   Úkeyr   r   r   Ú__getitem__   s    z_TableRowAccessor.__getitem__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r	   r   r   r   r   r   r      s   r   c               @   s€   e Zd ZdZdd„ Zedd„ ƒZedd„ ƒZdd	„ Zd
d„ Z	e
dd„ ƒZdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zedd„ ƒZdS )ÚH5TableNodez® A wrapper for PyTables Table nodes.

    Parameters
    ----------
    node : tables.Table instance
        An H5 node which is a pytables.Table or H5TableNode instance
    c             C   sB   ddl m} | j|ƒst‚t|dƒr*|jn|| _|| jjƒ| _d S )Né   )ÚH5Attrsr   )Úfiler   Úis_table_nodeÚAssertionErrorÚhasattrr   Z_v_attrsÚattrs)r   Únoder   r   r   r   r	      s    zH5TableNode.__init__c             K   s:   t |ttfƒrtj|ƒ}| j|||f|Ž || }| |ƒS )aÅ   Add table node to an H5 file at the specified path.

        Parameters
        ----------
        h5 : H5File
            The H5 file where the table node will be stored.
        node_path : str
            Path to node where data is stored (e.g. '/path/to/my_table')
        description : list of tuples or numpy dtype object
            The description of the columns in the table. This is either a list
            of (column name, dtype, [, shape or itemsize]) tuples or a numpy
            record array dtype. For more information, see the documentation for
            `Table` in PyTables.
        **kwargs : dict
            Additional keyword arguments to pass to pytables.File.create_table
        )Ú
isinstanceÚtupleÚlistÚnpZdtypeÚ_create_pytables_node)ÚclsÚh5Ú	node_pathÚdescriptionÚkwargsr   r   r   r   Úadd_to_h5file%   s
    
zH5TableNode.add_to_h5filec             C   s   t |ttfƒS )zL Return True if pytables_node is a pytables.Table or a H5TableNode.
        )r   ÚPyTablesTabler   )r   Zpytables_noder   r   r   r   ?   s    zH5TableNode.is_table_nodec                s2   t t‡ fdd„t | jƒ ƒD ƒŽ ƒ}| jj|ƒ dS )z˜ Add some data to the table.

        Parameters
        ----------
        data : dict
            A dictionary of column name -> values items
        c                s   g | ]}ˆ | ‘qS r   r   )Ú.0Úname)Údatar   r   ú
<listcomp>Q   s    z&H5TableNode.append.<locals>.<listcomp>N)r   ÚzipÚkeysr   Úappend)r   r'   Zrowsr   )r'   r   r+   I   s    "zH5TableNode.appendc                s2   t |tƒrˆ jj|ƒS ‡ fdd„|D ƒ}tj|ƒS )ac   Return one or more columns of data from the table.

        Parameters
        ----------
        col_or_cols : str or list of str
            A single column name or a list of column names

        Return
        ------
        data : ndarray
            An array of column data with the column order matching that of
            `col_or_cols`.
        c                s   g | ]}ˆ j j|ƒ‘qS r   )r   Úcol)r%   r&   )r   r   r   r(   e   s    z+H5TableNode.__getitem__.<locals>.<listcomp>)r   Ústrr   r,   r   Zcolumn_stack)r   Zcol_or_colsZcolumn_datar   )r   r   r   T   s    
zH5TableNode.__getitem__c             C   s
   t | jƒS )z= Return an object which provides access to row data.
        )r   r   )r   r   r   r   Úixh   s    zH5TableNode.ixc             C   s   | j jS )N)r   Zcolnames)r   r   r   r   r*   n   s    zH5TableNode.keysc             C   s   t | jdd… ƒS )z… Return table data as a pandas `DataFrame`.

        XXX: This does not work if the table contains a multidimensional column
        N)r   r.   )r   r   r   r   Úto_dataframeq   s    zH5TableNode.to_dataframec             C   s
   t | jƒS )N)Úreprr   )r   r   r   r   Ú__repr__}   s    zH5TableNode.__repr__c             C   s   | j jS )N)r   Znrows)r   r   r   r   Ú__len__€   s    zH5TableNode.__len__c             C   s   | j jƒ  d| _ dS )z} Implement the PyTables `Node._f_remove` method so that H5File
        doesn't choke when trying to remove our node.
        N)r   Ú	_f_remove)r   r   r   r   r3   ‡   s    
zH5TableNode._f_removec             K   s*   |j |ƒ\}}|j}|j|||f|Ž d S )N)Z
split_pathZ_h5Zcreate_table)r   r   r    r!   r"   Úpathr&   Zpyt_filer   r   r   r   Ž   s    z!H5TableNode._create_pytables_nodeN)r   r   r   r   r	   Úclassmethodr#   r   r+   r   Úpropertyr.   r*   r/   r1   r2   r3   r   r   r   r   r   r      s   
r   )
Znumpyr   Zpandasr   Ztables.tabler   r$   Úobjectr   r   r   r   r   r   Ú<module>   s   
