a
    ¨ˆcÒ  ã                   @   s8   d dl Zd dlmZ G dd„ deƒZG dd„ deƒZdS )é    N)ÚTablec                   @   s    e Zd ZdZdd„ Zdd„ ZdS )Ú_TableRowAccessorzBA simple object which provides read access to the rows in a Table.c                 C   s
   || _ d S ©N©Ú	_h5_table)ÚselfZh5_table© r   ú\/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/apptools/io/h5/table_node.pyÚ__init__   s    z_TableRowAccessor.__init__c                 C   s
   | j | S r   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} |  |¡sJ ‚t|dƒr*|jn|| _|| jjƒ| _d S )Né   )ÚH5Attrsr   )Úfiler   Úis_table_nodeÚ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|||fi |¤Ž || }| |ƒ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 )zKReturn True if pytables_node is a pytables.Table or a H5TableNode.
        )r   ÚPyTablesTabler   )r   Zpytables_noder   r   r	   r   H   s    zH5TableNode.is_table_nodec                    s.   t t‡ fdd„|  ¡ D ƒŽ ƒ}| 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>Z   ó    z&H5TableNode.append.<locals>.<listcomp>N)r   ÚzipÚkeysr   Úappend)r   r)   Úrowsr   r(   r	   r.   R   s    zH5TableNode.appendc                    s2   t |tƒrˆ j |¡S ‡ fdd„|D ƒ}t |¡S )ab  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  |¡‘qS r   )r   Úcolr%   ©r   r   r	   r*   n   r+   z+H5TableNode.__getitem__.<locals>.<listcomp>)r   Ústrr   r0   r   Zcolumn_stack)r   Zcol_or_colsZcolumn_datar   r1   r	   r   ]   s    
zH5TableNode.__getitem__c                 C   s
   t | jƒS )z3Return an object which provides access to row data.)r   r   r1   r   r   r	   Úixq   s    zH5TableNode.ixc                 C   s   | j jS r   )r   Zcolnamesr1   r   r   r	   r-   v   s    zH5TableNode.keysc                 C   s   ddl m} || jdd… ƒS )zÔReturn table data as a pandas `DataFrame`.

        XXX: This does not work if the table contains a multidimensional column

        This method requires pandas to have been installed in the environment.
        r   )Ú	DataFrameN)Zpandasr4   r3   )r   r4   r   r   r	   Úto_dataframey   s    zH5TableNode.to_dataframec                 C   s
   t | jƒS r   )Úreprr   r1   r   r   r	   Ú__repr__‰   s    zH5TableNode.__repr__c                 C   s   | j jS r   )r   Znrowsr1   r   r   r	   Ú__len__Œ   s    zH5TableNode.__len__c                 C   s   | 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_remover1   r   r   r	   r9   “   s    
zH5TableNode._f_removec                 K   s.   |  |¡\}}|j}|j|||fi |¤Ž d S r   )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   Úpropertyr3   r-   r5   r7   r8   r9   r   r   r   r   r	   r      s"   

	
r   )Únumpyr   Ztables.tabler   r$   Úobjectr   r   r   r   r   r	   Ú<module>
   s   
