B
    q\                 @   s8   d dl Z d dlZd dlZG dd dZe jje dS )    Nc               @   s   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	d.ddZ
dd Zdd Zedd Zedd Zdd Zedd Zedd Zedd  Zed!d" Zd/d$d%Zd&d' Zd(d) Zd*d+ Zd,d- ZdS )0Rowa  A class to represent one row of a Table object.

    A Row object is returned when a Table object is indexed with an integer
    or when iterating over a table::

      >>> from astropy.table import Table
      >>> table = Table([(1, 2), (3, 4)], names=('a', 'b'),
      ...               dtype=('int32', 'int32'))
      >>> row = table[1]
      >>> row
      <Row index=1>
        a     b
      int32 int32
      ----- -----
          2     4
      >>> row['a']
      2
      >>> row[1]
      4
    c             C   sP   || _ t|| _t|}|| k s,||kr@td|t|| j| | _d S )Nz0index {0} out of range for table with length {1})_tableoperatorindex_indexlen
IndexErrorformat)selftabler   n r   0lib/python3.7/site-packages/astropy/table/row.py__init__   s    zRow.__init__c                sL    j |r6 fdd|D } j j|dd j }n j j|  j }|S )Nc                s   g | ]} j | qS r   )r   ).0name)r
   r   r   
<listcomp>-   s    z#Row.__getitem__.<locals>.<listcomp>F)copy)r   _is_list_or_tuple_of_str	__class__r   columns)r
   itemcolsoutr   )r
   r   __getitem__+   s
    zRow.__getitem__c             C   s8   | j |r"| j j| j||d n|| j j| | j< d S )N)colnamesvals)r   r   Z_set_rowr   r   )r
   r   valr   r   r   __setitem__5   s    zRow.__setitem__c             C   s   | j S )N)r   )r
   r   r   r   _ipython_key_completions_;   s    zRow._ipython_key_completions_c             C   s   | j jrtd|  |kS )Nz;Unable to compare rows for masked table due to numpy.ma bug)r   masked
ValueErroras_void)r
   otherr   r   r   __eq__>   s    z
Row.__eq__c             C   s   | j jrtd|  |kS )Nz;Unable to compare rows for masked table due to numpy.ma bug)r   r    r!   r"   )r
   r#   r   r   r   __ne__F   s    z
Row.__ne__Nc             C   s   |dk	rt dt|  S )a  Support converting Row to np.array via np.array(table).

        Coercion to a different dtype via np.array(table, dtype) is not
        supported and will raise a ValueError.

        If the parent table is masked then the mask information is dropped.
        Nz Datatype coercion is not allowed)r!   npasarrayr"   )r
   dtyper   r   r   	__array__K   s    zRow.__array__c             C   s   t | jjS )N)r   r   r   )r
   r   r   r   __len__X   s    zRow.__len__c             c   s*   | j }x| jj D ]}|| V  qW d S )N)r   r   r   values)r
   r   colr   r   r   __iter__[   s    zRow.__iter__c             C   s   | j S )N)r   )r
   r   r   r   r   `   s    z	Row.tablec             C   s   | j S )N)r   )r
   r   r   r   r   d   s    z	Row.indexc       	         s   | j | jj }tfdd|D }| jjrddlm  tfdd|D } fdd|D }dd |D }tj	|g|d	d
 }tj	|g| j
d	d
 }tjj||d}ntj	|g| j
d	d
 }|S )a  
        Returns a *read-only* copy of the row values in the form of np.void or
        np.ma.mvoid objects.  This corresponds to the object types returned for
        row indexing of a pure numpy structured array or masked array. This
        method is slow and its use is discouraged when possible.

        Returns
        -------
        void_row : np.void (unmasked) or np.ma.mvoid (masked)
            Copy of row values
        c             3   s   | ]}t |  V  qd S )N)r&   r'   )r   r,   )r   r   r   	<genexpr>v   s    zRow.as_void.<locals>.<genexpr>   )descrc             3   s&   | ]}t |d r|j  ndV  qdS )maskFN)hasattrr1   )r   r,   )r   r   r   r.      s   c             3   s   | ]} |V  qd S )Nr   )r   r,   )r0   r   r   r.      s    c             S   s   g | ]\}}}|t |fqS r   )bool)r   r   Ztype_shaper   r   r   r      s    zRow.as_void.<locals>.<listcomp>)r(   r   )datar1   )r   r   r   r+   tupler    r   r0   r&   Zarrayr(   ZmaZmvoid)	r
   r   r   ZmasksZdescrsZmask_dtypesZrow_maskZrow_valsZvoid_rowr   )r0   r   r   r"   h   s    
zRow.as_voidc             C   s   | j jS )N)r   meta)r
   r   r   r   r7      s    zRow.metac             C   s   | j jS )N)r   r   )r
   r   r   r   r      s    zRow.columnsc             C   s   | j jS )N)r   r   )r
   r   r   r   r      s    zRow.colnamesc             C   s   | j jS )N)r   r(   )r
   r   r   r   r(      s    z	Row.dtypeFc             C   st   | j dkr| j n| j t| j }| j||d  }| jjd| j g}|jrV|d |j||ddt	| jdS )zV
        Display row as a single-line table but with appropriate header line.
        r   r/   z	index={0}zmasked=Trueztable{0})	max_widthZtableid)
r   r   r   r   __name__r	   r    append_base_repr_id)r
   htmlr   r   Z
descr_valsr   r   r   r<      s     

zRow._base_repr_c             C   s   | j ddS )NT)r>   )r<   )r
   r   r   r   _repr_html_   s    zRow._repr_html_c             C   s   | j ddS )NF)r>   )r<   )r
   r   r   r   __repr__   s    zRow.__repr__c             C   s@   | j dkr| j n| j t| j }d| j||d  jddS )Nr   
r/   r8   )r9   )r   r   r   joinr   Zpformat)r
   r   r   r   r   __str__   s     zRow.__str__c             C   s   t | dS )Nzutf-8)strencode)r
   r   r   r   	__bytes__   s    zRow.__bytes__)N)F)r:   
__module____qualname____doc__r   r   r   r   r$   r%   r)   r*   r-   propertyr   r   r"   r7   r   r   r(   r<   r?   r@   rC   rF   r   r   r   r   r   	   s,   

$
r   )collectionsr   Znumpyr&   r   abcSequenceregisterr   r   r   r   <module>   s
    0