
    HR-e?                         d dl Z d dlZd dlmZ d dlmZ d dlmZ ddlm	Z	m
Z
  G d de	j        	          Z G d
 de          Z G d de          Z G d de          Z G d de          Z G d de          ZdS )    N)OrderedDict)Table)_set_locale   )corecparserc                   \    e Zd ZdZdZdZdZdZdZdZ	i fdZ
d Zd Zd	 Zd
 Zd Z	 ddZdS )	FastBasicaF  
    This class is intended to handle the same format addressed by the
    ordinary :class:`Basic` writer, but it acts as a wrapper for underlying C
    code and is therefore much faster. Unlike the other ASCII readers and
    writers, this class is not very extensible and is restricted
    by optimization requirements.
    
fast_basicz9Basic table with custom delimiter using the fast C engineTFc                 .   |                     dd          %|                     dd          t          d          t          j        |          }|                    t          j        |                     |                    dd          }|t          |          nd | _        |                     dd          | _        |                    dd          | _	        | j	        t          | j	                  | _	        t          |                    d	d
                    | _
        |                    dd          | _        |                     d| j        
| j        dz   nd          }|                    d|          | _        || _        d| _        d| _        d S )Nheader_startr   z2header_start cannot be set to None for this Reader	delimiter commentz# #	quotechar"
data_startr   T)get
ValueErrorcopydeepcopyupdatepopstrr   write_commentr   r   r   r   kwargsstrip_whitespace_linesstrip_whitespace_fields)selfdefault_kwargsuser_kwargsr   r   data_start_defaults         :lib/python3.11/site-packages/astropy/io/ascii/fastbasic.py__init__zFastBasic.__init__   sp   
 ~q11=22:QRRR ~..dmK00111JJ{C00	+4+@Yd#ZZ	488zz)S11<#t|,,DLVZZS99::"JJ~q99 )__43D3P$+a//VW
 
 !**\3EFF&*#'+$$$    c                 8    | j                                          d S )N)engineread_header)r    s    r$   _read_headerzFastBasic._read_header@   s     	!!!!!r&   c           
      J   | j         ,t          | j                   dk    rt          j        d          | j        t          j        d          | j        4| j        dk     r)t          | t                    st          j        d          | j        dk     rt          j        d          t          | j                  dk    rt          j        d          t          | j	                  dk    rt          j        d	          d
| j
        v rt          j        d          d| j
        v rt          j        d          d| j
        v rt          j        d          d| j
        v rt          j        d          d| j
        v s	d| j
        v rt          j        d          | j
                            dd          | _        | j
                            dd          }t          |t                    si }|                    dd           |                    dd          | _        || j
        d<   t!          j        || j        | j        f| j        | j        | j         | j	        | j        | j        d| j
        | _        |                                 }|                                  ||\  }}}ni }i }i }t1          d          5  | j                            |||          \  }}ddd           n# 1 swxY w Y   |                     ||          }	| j        r| j        j        |	j        d<   |	S )z
        Read input data (file-like object, filename, list of strings, or
        single string) into a Table and return the result.
        Nr   z-The C reader does not support a comment regexz1The C reader does not allow data_start to be Noner   zYThe C reader does not allow header_start to be negative except for commented-header filesz5The C reader does not allow data_start to be negativez,The C reader only supports 1-char delimitersz5The C reader only supports a length-1 quote character
convertersz<The C reader does not support passing specialized convertersencodingz0The C reader does not use the encoding parameter	Outputterz1The C reader does not use the Outputter parameterInputterz0The C reader does not use the Inputter parameterdata_Splitterheader_Splitterz*The C reader does not use a Splitter classstrict_namesFfast_readerTenablereturn_header_chars)r   r   r   r   r   fill_extra_colsC"__ascii_fast_reader_header_chars__)r   lenr   ParameterErrorr   r   
isinstanceFastCommentedHeaderr   r   r   r   r2   r   dictr5   r   CParserr   r   r6   r(   r*   check_headerr   read
make_tableheader_charsmeta)
r    tabler3   conversion_infotry_int	try_float
try_stringdatacommentsouts
             r$   r@   zFastBasic.readE   s   
 <#DL(9(9Q(>(>%&UVVV_$%C   )!A%%t%899 & %=   _q  %G     A%%%&TUUU  A%%%G   T[((%N   4;&&%B   DK''%C   4;&&%B   ++/@DK/O/O%&RSSS KOONEBB koomT::+t,, 	K$'''#.??3H%#P#P %0M"o'(
 n*Ln 0
 
 k
 
 ++--&-<*GY

GIJ 	N 	N![--gy*MMND(	N 	N 	N 	N 	N 	N 	N 	N 	N 	N 	N 	N 	N 	N 	NoodH--# 	V=A[=UCH9:
s   ; K''K+.K+c                     t                      }|r||d<   t          j        | j                                                  }t          |||          S )z:Actually make the output table give the data and comments.rJ   namesrC   )r   r   _deduplicate_namesr(   	get_namesr   )r    rI   rJ   rC   rN   s        r$   rA   zFastBasic.make_table   sP    }} 	('D'(=(=(?(?@@TT2222r&   c                    | j                                         p| j                                         }| j        rWg d}|D ]P}t	          j        |          s't          |          dk    s|d         |v s
|d         |v rt          d|d          Q| j        r%t          |          dk    rt          d|           d S d S )N)r   ,|	'r   r   zColumn name z' does not meet strict name requirementsr   z9Table format guessing requires at least two columns, got )	r(   get_header_namesrP   r2   r   
_is_numberr9   r   guessing)r    rN   badsnames       r$   r?   zFastBasic.check_header   s    ,,..I$+2G2G2I2I 	222D 	 	OD))4yyA~~Aw$Bx4''$VtVVV   ( = 	SZZ1__SESS  	 	__r&   c                 4    |                      ||i            dS )z
        Use a fast Cython method to write table data to output,
        where output is a filename or file-like object.
        N_writer    rD   outputs      r$   writezFastBasic.write   s     
 	E62&&&&&r&   c                    t          j        |d           | j        | j        | j        | j        d}|                    |           |                    | j                   t          j	        |fi |}|
                    |||           d S )Nr   )max_ndim)r   r   strip_whitespacer   )r   _check_multidim_tabler   r   r   r   r   r   r   
FastWriterra   )r    rD   r`   r!   header_outputoutput_typeswrite_kwargswriters           r$   r^   zFastBasic._write   s     	"515555  $ <)	
 
 	N+++DK(((#E::\::V]L99999r&   N)TF)__name__
__module____qualname____doc___format_name_description_fastr6   rY   r2   r%   r*   r@   rA   r?   ra   r^    r&   r$   r
   r
      s           LNLEOHL&( !, !, !, !,F" " "
Z Z Zx3 3 3  *' ' ' OT: : : : : :r&   r
   )	metaclassc                   8     e Zd ZdZdZdZdZdZ fdZd Z	 xZ
S )FastCsva	  
    A faster version of the ordinary :class:`Csv` writer that uses the
    optimized C parsing engine. Note that this reader will append empty
    field values to the end of any row with not enough columns, while
    :class:`FastBasic` simply raises an error.
    fast_csvz4Comma-separated values table using the fast C engineTc                 B     t                      j        dd dfi | d S )NrR   )r   r   superr%   r    r   	__class__s     r$   r%   zFastCsv.__init__   s0    st<<GGGGGGGr&   c                 R    |                      ||dt          j        dfgi           dS )zs
        Override the default write method of `FastBasic` to
        output masked values as empty fields.
        fill_values N)r^   r   maskedr_   s      r$   ra   zFastCsv.write   s/    
 	E6MT["4E3F#GHHHHHr&   )rk   rl   rm   rn   ro   rp   rq   r6   r%   ra   __classcell__r{   s   @r$   ru   ru      ss          LILEOH H H H HI I I I I I Ir&   ru   c                   .     e Zd ZdZdZdZdZ fdZ xZS )FastTabzl
    A faster version of the ordinary :class:`Tab` reader that uses
    the optimized C parsing engine.
    fast_tabz2Tab-separated values table using the fast C engineTc                 \     t                      j        ddifi | d| _        d| _        d S )Nr   rT   Fry   r%   r   r   rz   s     r$   r%   zFastTab.__init__   s=    +t,77777&+#',$$$r&   )	rk   rl   rm   rn   ro   rp   rq   r%   r   r   s   @r$   r   r      sS         
 LGLE- - - - - - - - -r&   r   c                   4     e Zd ZdZdZdZdZ fdZd Z xZ	S )FastNoHeaderz
    This class uses the fast C engine to read tables with no header line. If
    the names parameter is unspecified, the columns will be autonamed with
    "col{}".
    fast_no_headerz3Basic table with no headers using the fast C engineTc                 B     t                      j        d ddfi | d S )Nr   )r   r   rx   rz   s     r$   r%   zFastNoHeader.__init__  s0    $a@@KKFKKKKKr&   c                 8    |                      ||i d           dS )z
        Override the default writing behavior in `FastBasic` so
        that columns names are not included in output.
        Nrg   r]   r_   s      r$   ra   zFastNoHeader.write  s$    
 	E62T:::::r&   )
rk   rl   rm   rn   ro   rp   rq   r%   ra   r   r   s   @r$   r   r     sg          $LHLEL L L L L; ; ; ; ; ; ;r&   r   c                   @     e Zd ZdZdZdZdZ fdZd Zd Z	d Z
 xZS )	r<   z
    A faster version of the :class:`CommentedHeader` reader, which looks for
    column names in a commented line. ``header_start`` denotes the index of
    the header line among all commented lines and is 0 by default.
    fast_commented_headerz8Columns name in a commented line using the fast C engineTc                 V     t                      j        i fi | d|vr	d| _        d S d S )Nr   r   )ry   r%   r   rz   s     r$   r%   zFastCommentedHeader.__init__"  sB    &&v&&& v%%DOOO &%r&   c                 &   t                      }|rE| j        }|dk     rt          |          |z   }|d|         ||dz   d         z   |d<   |d         s|d= t          j        | j                                                  }t          |||          S )z
        Actually make the output table give the data and comments.  This is
        slightly different from the base FastBasic method in the way comments
        are handled.
        r   Nr   rJ   rM   )r   r   r9   r   rO   r(   rP   r   )r    rI   rJ   rC   idxrN   s         r$   rA   zFastCommentedHeader.make_table)  s     }} 	%#CQww(mmc)'~q0CCD
# %$'(=(=(?(?@@TT2222r&   c                 4   | j         j        }g }|                                D ]c}|                                }|rK|d         | j        k    r:|                    |dd                     t          |          | j        dz   k    r ndt          |          | j        k    rt          j	        d          | j         
                    || j                 g           d| j         _        | j                                          | j         
                    |           d S )Nr   r   znot enough commented lines)r(   source
splitlineslstripr   appendr9   r   r   CParserErrorsetup_tokenizerr)   )r    tmpcommented_lineslines       r$   r*   z FastCommentedHeader._read_header;  s   k NN$$ 	 	D;;==D Q4<//&&tABBx000''4+<q+@@@E4#444&'CDDD##_T5F%G$HIII#$ !!!##C(((((r&   c                 8    |                      ||i d           dS )zr
        Override the default writing behavior in `FastBasic` so
        that column names are commented.
        r   r   Nr]   r_   s      r$   ra   zFastCommentedHeader.writeN  s$    
 	E62Y?????r&   )rk   rl   rm   rn   ro   rp   rq   r%   rA   r*   ra   r   r   s   @r$   r<   r<     s          +LMLE         3 3 3$) ) )&@ @ @ @ @ @ @r&   r<   c                   :     e Zd ZdZdZdZdZ fdZd Zd Z	 xZ
S )FastRdbz
    A faster version of the :class:`Rdb` reader. This format is similar to
    tab-delimited, but it also contains a header line after the column
    name line denoting the type of each column (N for numeric, S for string).
    fast_rdbz0Tab-separated with a type definition header lineTc                 ^     t                      j        dddfi | d| _        d| _        d S )NrT      )r   r   Fr   rz   s     r$   r%   zFastRdb.__init__a  s@    t1==HHHHH&+#',$$$r&   c                    
 | j         j        }d}d}|                                D ]{}|s:|                                r&|                                d         | j        k    r|}>|s;|                                r'|                                d         | j        k    r|} n|t          d          | j                             |g           d| j         _        | j         	                    dd           | j         
                                | j                                         k    r| j                             g            | j                             |g           | j         	                    dd           | j                                         
| j         	                    d           t          
          t                    k    rt          j        d          t          | j                                                   t                    k    r&
fd	| j                                         D             t!          d
 D                       rt          j        d           i }i }i }t#          | j                                                   D ]B\  }}	|	d                                         dk    rd||<   d||<   d||<   3d||<   d||<   d||<   C| j                             |           |||fS )Nr~   r   zRDB header requires 2 linesF)deduplicatefilter_namesT)r   zCRDB header mismatch between number of column names and column typesc                 F    g | ]}                     |                   S rr   )index).0n	col_namestypess     r$   
<listcomp>z(FastRdb._read_header.<locals>.<listcomp>  s*    PPP1U9??1--.PPPr&   c              3   X   K   | ]%}t          j        d |t           j                   V  &dS )z	\d*(N|S)$N)rematch
IGNORECASE)r   xs     r$   	<genexpr>z'FastRdb._read_header.<locals>.<genexpr>  s5      KK28L!R];;;KKKKKKr&   z2RDB type definitions do not all match [num](N|S): rV   sr   )r(   r   r   stripr   r   r   r   r   r)   rW   rP   	set_namesr9   r   InconsistentTableErroranyziplower)r    r   line1line2r   rF   rG   rH   r[   col_typer   r   s             @@r$   r*   zFastRdb._read_headerf  s   k NN$$ 	< 	<D TZZ\\ dkkmmA.>$,.N.N tzz|| a0@DL0P0P:;;; 	##UG,,,#$ EFFF,,.. DK))++++K!!"%%%##UG,,, 	DuEEEK))++	E222y>>SZZ''-U   t{$$&&''3u::55PPPPP8M8M8O8OPPPEKKUKKKKK 	-LULL   	
!$+"7"7"9"95AA 	% 	%ND(|!!##s** !"#	$#$
4   !"#	$#$
4  ##C(((J//r&   c                 8    |                      ||i d           dS )z
        Override the default writing behavior in `FastBasic` to
        output a line with column types after the column name line.
        T)rh   Nr]   r_   s      r$   ra   zFastRdb.write  s$    
 	E62D99999r&   )rk   rl   rm   rn   ro   rp   rq   r%   r*   ra   r   r   s   @r$   r   r   V  sr          LELE- - - - -
>0 >0 >0@: : : : : : :r&   r   )r   r   collectionsr   astropy.tabler   astropy.utils.miscr   r~   r   r   MetaBaseReaderr
   ru   r   r   r<   r   rr   r&   r$   <module>r      s    				 # # # # # #       * * * * * *        I: I: I: I: I:$- I: I: I: I:XI I I I Ii I I I0- - - - -i - - - ; ; ; ; ;9 ; ; ;,<@ <@ <@ <@ <@) <@ <@ <@~U: U: U: U: U:i U: U: U: U: U:r&   