B
    q\                 @   st   d Z ddlZddlZddlmZ ddlmZ ddlZddl	m
Z
 dddgZdddZG dd de
Zedd ZdS )z7
Table property for providing information about table.
    N)contextmanager)isclass)DataInfo
table_info	TableInfoserialize_method_as
attributes c             C   s  ddl m} |dkrtj}| jjg}| jr4|d |dt	|  dd
| d g}| j }| jrg }x |D ]}||j|d	d
 qvW ||t|d d}	n| }	|d	kr|	S d|	jkrtdd |D }
t	|
dkrt|d | jr|	d= d|	jkrt|	d dkr|	d= d|	jkrHt|	d t	| krH|	d= x<|	jD ]2}|	| jjdkrPt|	| dkrP|	|= qPW | jr||	jdddd n
|d |dd |D  d	S )a  
    Write summary information about column to the ``out`` filehandle.
    By default this prints to standard output via sys.stdout.

    The ``option`` argument specifies what type of information
    to include.  This can be a string, a function, or a list of
    strings or functions.  Built-in options are:

    - ``attributes``: basic column meta data like ``dtype`` or ``format``
    - ``stats``: basic statistics: minimum, mean, and maximum

    If a function is specified then that function will be called with the
    column as its single argument.  The function must return an OrderedDict
    containing the information attributes.

    If a list is provided then the information attributes will be
    appended for each of the options, in order.

    Examples
    --------
    >>> from astropy.table.table_helpers import simple_table
    >>> t = simple_table(size=2, kinds='if')
    >>> t['a'].unit = 'm'
    >>> t.info()
    <Table length=2>
    name  dtype  unit
    ---- ------- ----
       a   int64    m
       b float64

    >>> t.info('stats')
    <Table length=2>
    name mean std min max
    ---- ---- --- --- ---
       a  1.5 0.5   1   2
       b  1.5 0.5 1.0 2.0

    Parameters
    ----------
    option : str, function, list of (str or function)
        Info option, defaults to 'attributes'.
    out : file-like object, None
        Output destination, default is sys.stdout.  If None then a
        Table with information attributes is returned

    Returns
    -------
    info : `~astropy.table.Table` if out==None else None
       )Tabler	   zmasked=Truez
length={0}< >N)outr   )namesclassc             s   s   | ]}t |V  qd S )N)type).0col r   1lib/python3.7/site-packages/astropy/table/info.py	<genexpr>`   s    ztable_info.<locals>.<genexpr>Zn_badlengthZSUF)Z	max_widthZ	max_linesZ	show_unitz<No columns>c             s   s   | ]}|t j V  qd S )N)oslinesep)r   Zoutliner   r   r   r   t   s    )tabler   sysstdout	__class____name__ZmaskedappendformatlenjoincolumnsvaluesZcolnamesinfolistset
isinstanceZColumnClassnpallZdtypeZkindextendZpformat
writelines)tbloptionr   r   Z
descr_valsZoutlinesZcolsZinfosr   r'   Z
uniq_typesnamer   r   r   r      s@    2




 $&
c               @   s"   e Zd ZdZdddZeje_dS )r   Nr   r	   c             C   s   t | j||S )N)r   _parent)selfr0   r   r   r   r   __call__z   s    zTableInfo.__call__)r   r	   )r    
__module____qualname__r2   r4   r   __doc__r   r   r   r   r   w   s   
c          
   #   s   fdd}rhi }xR|   D ]F}t|jdr||  r|jj||jj<  fdd|jjD |j_qW z
dV  W drx | D ]\}}|| | j_qW X dS )a  Context manager to temporarily override individual
    column info.serialize_method dict values.  The serialize_method
    attribute is an optional dict which might look like ``{'fits':
    'jd1_jd2', 'ecsv': 'formatted_value', ..}``.

    ``serialize_method`` is a str or dict.  If str then it the the value
    is the ``serialize_method`` that will be used for all formats.
    If dict then the key values can be either:

    - Column name.  This has higher precedence than the second option of
      matching class.
    - Class (matches any column which is an instance of the class)

    This context manager is expected to be used only within ``Table.write``.
    It could have been a private method on Table but prefer not to add
    clutter to that class.

    Parameters
    ----------
    tbl : Table object
        Input table
    serialize_method : dict, str
        Dict with key values of column names or types, or str

    Returns
    -------
    None (context manager)
    c                sR   t  tr S | jj kr& | jj S x& D ]}t|r,t | |r, | S q,W dS )z
        Determine if the ``serialize_method`` str or dict specifies an
        override of column presets for ``col``.  Returns the matching
        serialize_method value or ``None``.
        N)r*   strr'   r1   r   )r   key)serialize_methodr   r   get_override_sm   s    

z,serialize_method_as.<locals>.get_override_smr:   c                s   i | ]
} |qS r   r   )r   Zfmt)override_smr   r   
<dictcomp>   s   z'serialize_method_as.<locals>.<dictcomp>N)Zitercolshasattrr'   r:   r1   items)r/   r:   r;   Zoriginal_smsr   r1   Zoriginal_smr   )r<   r:   r   r      s    

)r   r	   )r7   r   r   
contextlibr   inspectr   Znumpyr+   Zastropy.utils.data_infor   __all__r   r   r   r   r   r   r   <module>   s   

g	