a
    ߙfb                     @   sH   d dl Z d dl mZ d dlmZ d dlZG dd dZe jj	
e dS )    N)OrderedDictindexc                   @   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	d2ddZ
dd Z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d3d(d)Zd*d+ Zd,d- Zd.d/ Zd0d1 ZdS )4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   sJ   t |}t|}|| k s"||kr6td|t||| | _|| _d S )Nz.index {} out of range for table with length {})operator_indexlen
IndexErrorformat_index_table)selftabler   n r   0lib/python3.9/site-packages/astropy/table/row.py__init__    s    
zRow.__init__c              	      s~   zt  jj| j }W n^ ttfyx    j|rb fdd|D } jj|dd j }n jj|  j }Y n0 |S )Nc                    s   g | ]} j | qS r   r   ).0namer   r   r   
<listcomp>6       z#Row.__getitem__.<locals>.<listcomp>F)copy)	r   __getitem__r   columnsr
   KeyError	TypeError_is_list_or_tuple_of_str	__class__)r   itemoutcolsr   r   r   r   .   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__=   s    zRow.__setitem__c                 C   s   | j S N)r"   r   r   r   r   _ipython_key_completions_C   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__F   s    z
Row.__eq__c                 C   s   | j jrtd|  |kS r(   r)   r-   r   r   r   __ne__N   s    z
Row.__ne__Nc                 C   s   |du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__S   s    zRow.__array__c                 C   s   t | jjS r&   )r   r   r   r   r   r   r   __len__`   s    zRow.__len__c                 c   s&   | j }| jj D ]}|| V  qd S r&   )r
   r   r   values)r   r   colr   r   r   __iter__c   s    zRow.__iter__c                 C   s   | j j S r&   )r   r   keysr   r   r   r   r9   h   s    zRow.keysc                 C   s   |   S r&   )r8   r   r   r   r   r6   k   s    z
Row.valuesc                 C   s   | j S r&   r   r   r   r   r   r   n   s    z	Row.tablec                 C   s   | j S r&   )r
   r   r   r   r   r   r   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 : ``numpy.void`` or ``numpy.ma.mvoid``
            Copy of row values.
            ``numpy.void`` if unmasked, ``numpy.ma.mvoid`` else.
        c                 3   s   | ]}t |  V  qd S r&   )r1   r2   r   r7   r   r   r   	<genexpr>   r   zRow.as_void.<locals>.<genexpr>   descrc                 3   s&   | ]}t |d r|j  ndV  qdS )maskFN)hasattrr?   r:   r   r   r   r;      s   c                 3   s   | ]} |V  qd S r&   r   r:   r=   r   r   r;      r   c                 S   s   g | ]\}}}|t |fqS r   )bool)r   r   type_shaper   r   r   r      r   zRow.as_void.<locals>.<listcomp>)r3   r   )datar?   )r
   r   r   r6   tupler*   r   r>   r1   Zarrayr3   ZmaZmvoid)	r   r!   r#   ZmasksZdescrsZmask_dtypesZrow_maskZrow_valsZvoid_rowr   )r>   r   r   r,   v   s    zRow.as_voidc                 C   s   | j jS r&   )r   metar   r   r   r   rF      s    zRow.metac                 C   s   | j jS r&   )r   r   r   r   r   r   r      s    zRow.columnsc                 C   s   | j jS r&   )r   r"   r   r   r   r   r"      s    zRow.colnamesc                 C   s   | j jS r&   )r   r3   r   r   r   r   r3      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=zmasked=Truer   )	max_widthZtableid)	r   r   r   r   __name__r*   append_base_repr_id)r   htmlr   r   Z
descr_valsr   r   r   rK      s     


zRow._base_repr_c                 C   s   | j ddS )NTrM   rK   r   r   r   r   _repr_html_   s    zRow._repr_html_c                 C   s   | j ddS )NFrN   rO   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<   rG   )rH   )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encoder   r   r   r   	__bytes__   s    zRow.__bytes__)N)F)rI   
__module____qualname____doc__r   r   r%   r'   r/   r0   r4   r5   r8   r9   r6   propertyr   r   r,   rF   r   r"   r3   rK   rP   rQ   rT   rW   r   r   r   r   r   
   s<   


%




r   )collectionsr   operatorr   r   Znumpyr1   r   abcSequenceregisterr   r   r   r   <module>   s    >