U
    e»{j”5  ã                   @   s€   d Z ddlZddlZddlZddlmZ ddlmZ ddl	m
Z
 dZg Zdd„ Zdd
d„Zddd„Zdd„ Zdd„ Zdd„ ZdS )zà
This package contains functions for reading and writing Parquet
tables that are not meant to be used directly, but instead are
available as readers/writers in `astropy.table`.  See
:ref:`astropy:table_io` for more details.
é    N)Ú
minversion)ÚAstropyUserWarning)ÚNOT_OVERWRITING_MSGs   PAR1c                 O   sb   |dk	rHz|  ¡ }W n tk
r*   Y dS X | d¡}| |¡ |tkS |dk	rZ| d¡S dS dS )a†  Checks if input is in the Parquet format.

    Parameters
    ----------
    origin : Any
    filepath : str or None
    fileobj : `~pyarrow.NativeFile` or None
    *args, **kwargs

    Returns
    -------
    is_parquet : bool
        True if 'fileobj' is not None and is a pyarrow file, or if
        'filepath' is a string ending with '.parquet' or '.parq'.
        False otherwise.
    NFé   )z.parquetz.parq)ÚtellÚAttributeErrorÚreadÚseekÚPARQUET_SIGNATUREÚendswith)ÚoriginÚfilepathÚfileobjÚargsÚkwargsÚposÚ	signature© r   úV/var/www/html/bwave/python/venv/lib/python3.8/site-packages/astropy/io/misc/parquet.pyÚparquet_identify   s    


r   Fc                     sˆ  t ƒ \}}}t| ttjfƒs.t| dƒs.tdƒ‚| | ¡}|jdk	rXdd„ |j 	¡ D ƒ}	ni }	ddl
m}
m}m} i }d|	kr¨|	 d¡ d	¡}| |¡}d
|kr¬|d
 }nd}dd„ |jD ƒ}d}d|krød}|d }|D ]}t|| ƒD ]‰ ||ˆ < qèqØt| ¡ ƒ‰|dk	�rˆ |¡ |dk	�r,ˆ |¡ tt ‡fdd„| ¡ D ƒ¡ƒ‰g }ˆD ]&‰ ‡ fdd„| 	¡ D ƒ}| |¡ �qT|�sŠtdƒ‚|�rÀt|d  ¡ ƒD ]}|ˆk�r |d  |¡ �q |�sÞ|j| ||d�}|j}nd}g }|D ]à‰ | ˆ ¡j | !¡ | "¡ fk�r&| #| ˆ ¡j  $¡ ¡ �qêdˆ › �}||	k�rHt%|	| ƒ}nP|�rhd}t& 'd|› d�t(¡ n0t)dd„ |ˆ  D ƒƒ}t& 'd|› d|› d�t(¡ | #| ˆ ¡j | !¡ k�r¼d|› �nd|› �¡ �qêt*j+|tt,||ƒƒd�}|�s|D ]‰ |ˆ   -¡ |ˆ  dd…< �qî|
||d�}|dk	�rzd d„ |d! D ƒ}|j. ¡ D ]8}d"D ],}|||j/ k�rHt0||||j/ | ƒ �qH�q@| 1|¡}|S )#a	  
    Read a Table object from a Parquet file.

    This requires `pyarrow <https://arrow.apache.org/docs/python/>`_
    to be installed.

    The ``filters`` parameter consists of predicates that are expressed
    in disjunctive normal form (DNF), like ``[[('x', '=', 0), ...], ...]``.
    DNF allows arbitrary boolean logical combinations of single column
    predicates. The innermost tuples each describe a single column predicate.
    The list of inner predicates is interpreted as a conjunction (AND),
    forming a more selective and multiple column predicate. Finally, the most
    outer list combines these filters as a disjunction (OR).

    Predicates may also be passed as List[Tuple]. This form is interpreted
    as a single conjunction. To express OR in predicates, one must
    use the (preferred) List[List[Tuple]] notation.

    Each tuple has format: (``key``, ``op``, ``value``) and compares the
    ``key`` with the ``value``.
    The supported ``op`` are:  ``=`` or ``==``, ``!=``, ``<``, ``>``, ``<=``,
    ``>=``, ``in`` and ``not in``. If the ``op`` is ``in`` or ``not in``, the
    ``value`` must be a collection such as a ``list``, a ``set`` or a
    ``tuple``.

    Examples:

    .. code-block:: python

        ('x', '=', 0)
        ('y', 'in', ['a', 'b', 'c'])
        ('z', 'not in', {'a','b'})

    Parameters
    ----------
    input : str or path-like or file-like object
        If a string or path-like object, the filename to read the table from.
        If a file-like object, the stream to read data.
    include_names : list [str], optional
        List of names to include in output. If not supplied, then
        include all columns.
    exclude_names : list [str], optional
        List of names to exclude from output (applied after ``include_names``).
        If not supplied then no columns are excluded.
    schema_only : bool, optional
        Only read the schema/metadata with table information.
    filters : list [tuple] or list [list [tuple] ] or None, optional
        Rows which do not match the filter predicate will be removed from
        scanned data.  See `pyarrow.parquet.read_table()` for details.

    Returns
    -------
    table : `~astropy.table.Table`
        Table will have zero rows and only metadata information
        if schema_only is True.
    r   z5pyarrow can only open path-like or file-like objects.Nc                 S   s"   i | ]\}}|  d ¡|  d ¡“qS ©zUTF-8)Údecode©Ú.0ÚkÚvr   r   r   Ú
<dictcomp>ƒ   s      z&read_table_parquet.<locals>.<dictcomp>r   )ÚTableÚmetaÚ	serializeÚtable_meta_yamlÚ
r   c                 S   s   i | ]
}||“qS r   r   ©r   Únamer   r   r   r   ”   s      FZ__serialized_columns__Tc                    s   g | ]}|ˆ kr|‘qS r   r   ©r   Úx)Ú	use_namesr   r   Ú
<listcomp>¦   s      z&read_table_parquet.<locals>.<listcomp>c                    s   g | ]\}}ˆ |kr|‘qS r   r   )r   ÚnÚcol©r#   r   r   r'   ­   s      z3No include_names specified were found in the table.)ÚcolumnsÚfiltersútable::len::é
   zNo z1 found in metadata. Guessing {strlen} for schema.c                 s   s   | ]}t | ¡ ƒV  qd S )N)ÚlenZas_py)r   Úrowr   r   r   Ú	<genexpr>Ú   s     z%read_table_parquet.<locals>.<genexpr>z* found in metadata. Using longest string (z characters).ÚUz|S)Údtype)Údatar   c                 S   s   i | ]}|d  |“qS r*   r   r$   r   r   r   r   ñ   s      Údatatype)ÚdescriptionÚformatÚunitr   )2Úget_pyarrowÚ
isinstanceÚstrÚosÚPathLikeÚhasattrÚ	TypeErrorZread_schemaÚmetadataÚitemsÚastropy.tabler   r   r   ÚpopÚsplitZget_header_from_yamlÚnamesÚ
_get_namesÚsetÚvaluesÚintersection_updateÚdifference_updateÚlistÚdictÚfromkeysÚextendÚ
ValueErrorÚkeysZ
read_tableÚnum_rowsÚfieldÚtypeÚstringÚbinaryÚappendZto_pandas_dtypeÚintÚwarningsÚwarnr   ÚmaxÚnpZzerosÚzipZto_numpyr+   r#   ÚsetattrZ_construct_mixins_from_columns) ÚinputZinclude_namesZexclude_namesZschema_onlyr,   ÚpaÚparquetÚ_ÚschemaÚmdr   r   r   Z	meta_dictÚ	meta_yamlZmeta_hdrZfull_table_columnsZhas_serialized_columnsZserialized_columnsZscolZnames_to_readrE   Úpa_tablerQ   r3   Zmd_nameZstrlenr4   ÚtableZheader_colsr)   Úattrr   )r#   r&   r   Úread_table_parquet;   s¢    ;








ÿ



þý(ÿ

rh   c              	      sœ  ddl m}m} ddlm} tƒ \‰}}t|ttj	fƒsFt
d|› �ƒ‚|dƒ� | | ¡‰ W 5 Q R X | ˆ ¡}d |¡}	i }
ˆ j ¡ D ]^\}}|jjtjkr¸t|jjd ƒ|
d|› �< n$|jjtjkrÜt|jjƒ|
d|› �< |	|
d	< q†d
d„ |
 ¡ D ƒ}‡ ‡fdd„ˆ jjD ƒ}ˆj||d�}tj |¡�rL|�r>t |¡ ntt |¡ƒ‚|j|||d��6}‡fdd„ˆ  ¡ D ƒ}ˆj j!||d�}| "|¡ W 5 Q R X dS )a¹  
    Write a Table object to a Parquet file

    This requires `pyarrow <https://arrow.apache.org/docs/python/>`_
    to be installed.

    Parameters
    ----------
    table : `~astropy.table.Table`
        Data table that is to be written to file.
    output : str or path-like
        The filename to write the table to.
    overwrite : bool, optional
        Whether to overwrite any existing file without warning. Default `False`.
    r   )r   r   )Úserialize_context_asz.`output` should be a string or path-like, not r`   r!   r   r-   r    c                 S   s"   i | ]\}}|  d ¡|  d ¡“qS r   )Úencoder   r   r   r   r   -  s     z'write_table_parquet.<locals>.<dictcomp>c                    s"   g | ]}|ˆ  ˆ j| j¡f‘qS r   )Zfrom_numpy_dtyper3   rS   r"   ©Zencode_tabler_   r   r   r'   3  s   ÿz'write_table_parquet.<locals>.<listcomp>)r@   )Úversionc                    s   g | ]}ˆ   |¡‘qS r   )Úarray)r   r)   )r_   r   r   r'   C  s     )rb   N)#rB   r   r   Zastropy.utils.data_infori   r9   r:   r;   r<   r=   r?   Zrepresent_mixins_as_columnsZget_yaml_from_tableÚjoinr+   rA   r3   rS   r[   Zstr_ÚitemsizeÚbytes_rE   rb   ÚpathÚexistsÚremoveÚOSErrorr   r7   ZParquetWriterZitercolsr   Zfrom_arraysZwrite_table)rf   ÚoutputÚ	overwriter   r   ri   r`   Úwriter_versionrd   Zmeta_yaml_strr@   r#   r)   Zmetadata_encodeZ	type_listrb   ÚwriterZarraysre   r   rk   r   Úwrite_table_parquetþ   s>    



ÿþry   c                 C   sF   g }|   ¡ D ]4\}}t|tƒr.| t|ƒ¡ q|dkr| |¡ q|S )a"  Recursively find the names in a serialized column dictionary.

    Parameters
    ----------
    _dict : `dict`
        Dictionary from astropy __serialized_columns__

    Returns
    -------
    all_names : `list` [`str`]
        All the column names mentioned in _dict and sub-dicts.
    r#   )rA   r:   rL   rN   rF   rV   )Ú_dictZ	all_namesr   r   r   r   r   rF   J  s    
rF   c                  C   sF   ddl m}  ddlm} |  d|t¡ |  d|t¡ |  d|t	¡ dS )z,
    Register Parquet with Unified I/O.
    r   )Úregistry)r   r`   N)
Z
astropy.ior{   rB   r   Zregister_readerrh   Zregister_writerry   Zregister_identifierr   )Zio_registryr   r   r   r   Úregister_parquet`  s
    r|   c                  C   sT   zdd l } ddl m} W n tk
r4   tdƒ‚Y nX t| dƒrFd}nd}| ||fS )Nr   )r`   z3pyarrow is required to read and write parquet filesz6.0.0z2.4z2.0)Zpyarrowr`   ÚImportErrorÚ	Exceptionr   )r_   r`   rw   r   r   r   r9   l  s    
r9   )NNFN)F)Ú__doc__r<   rX   Únumpyr[   Zastropy.utilsr   Zastropy.utils.exceptionsr   Zastropy.utils.miscr   r
   Ú__all__r   rh   ry   rF   r|   r9   r   r   r   r   Ú<module>   s&   "       ÿ
 D
L