o
    _~#g)                     @   s   d Z ddlmZmZ ddlZddlZddlm  m	Z
 ddlmZ ddlmZ ddlmZ ddlmZ ddlmZmZ ddlmZ G d	d
 d
Zdd ZdS )z\
Tests the TextReader class in parsers.pyx, which
is integral to the C engine in parsers.py
    )BytesIOStringION)
TextReader)ParserWarning)	DataFrame)TextFileReaderread_csv)ensure_dtype_objsc                   @   s.  e Zd Zejdd Zdd Zdd Zdd Zd	d
 Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zejd)d*d+d,ifd-d.d/ifd0d+d,ifd1d+d,ifd2d.d/ifd3d.d/ifgd4d5 Zd6d7 Zejd8ed9d:d; Zd<d= Zd>S )?TestTextReaderc                 C   s   |ddddS )Niodatacsvz	test1.csv )selfdatapathr   r   /var/www/static.ux5.de/https/Moving-Object-Detection-with-OpenCV/env/lib/python3.10/site-packages/pandas/tests/io/parser/test_textreader.pycsv_path   s   zTestTextReader.csv_pathc                 C   s@   t |d}t|}|  W d    d S 1 sw   Y  d S )Nrbopenr   readr   r   freaderr   r   r   test_file_handle    s   
"zTestTextReader.test_file_handlec                 C   sD   t |d}t|d d}|  W d    d S 1 sw   Y  d S Nr   headerr   r   r   r   r   test_file_handle_mmap%   s   
"z$TestTextReader.test_file_handle_mmapc                 C   sR   t |d}| }W d    n1 sw   Y  t|}t|d d}|  d S r   )r   r   r   r   )r   r   r   textsrcr   r   r   r   test_StringIO+   s   
zTestTextReader.test_StringIOc                 C   s>   d}t t|d d}| }tttt|d dksJ d S )Nz	a
b
a
b
ar   r      )r   r   r   lensetmapidr   r   r   resultr   r   r   test_string_factorize2   s   "z$TestTextReader.test_string_factorizec                 C   b   d}t t|dd d}| }t|d tjg dtjd t|d tjg dtjd d S )	Nza,   b
a,   b
a,   b
a,   bT)skipinitialspacer   r   )ar,   r,   r,   dtype   )br0   r0   r0   r   r   r   tmassert_numpy_array_equalnparrayobject_r'   r   r   r   test_skipinitialspace9      z$TestTextReader.test_skipinitialspacec                 C   s4   d}t t|d d}| }|d jtjksJ d S )NzTrue
False
True
Truer   r   )r   r   r   r.   r4   bool_r'   r   r   r   test_parse_booleansF   s   z"TestTextReader.test_parse_booleansc                 C   r*   )	Nza  b
a		 "b"
"a"	 	 bT)delim_whitespacer   r   )r,   r,   r,   r-   r/   )r0   r0   r0   r1   r'   r   r   r   test_delimit_whitespaceN   r8   z&TestTextReader.test_delimit_whitespacec                 C   sD   d}t t|d d}| }tjg dtjd}t|d | d S )Nza
"hello
there"
thisr   )r,   zhello
therethisr-   r   )r   r   r   r4   r5   r6   r2   r3   r   r   r   r(   expectedr   r   r   test_embedded_newline[   s
   z$TestTextReader.test_embedded_newlinec                 C   sB   d}t t|ddd d}| }tddg}t|d | d S )Nz12345,67
345,678:,)	delimiterdecimalr   g)\@g+ٚu@r   )r   r   r   r4   r5   r2   assert_almost_equalr>   r   r   r   test_euro_decimald   s
   z TestTextReader.test_euro_decimalc                 C   sH   d}t t|ddd d}| }tjddgtjd}t|d | d S )	Nz123,456
12,500rA   rB   rC   	thousandsr   @ 0  r-   r   )r   r   r   r4   r5   int64r2   rE   r>   r   r   r   test_integer_thousandsm   s
   z%TestTextReader.test_integer_thousandsc                 C   s<   d}t t|ddd d}| }tddg}t|| d S )Nz123.456
12.500rA   .rG   rI   rJ   )r   r   r   r   r2   assert_frame_equalr>   r   r   r   test_integer_thousands_altv   s   z)TestTextReader.test_integer_thousands_altc                 C   s   d}t t|dd d}d}tjtj|d |  W d    n1 s$w   Y  t t|dd dd}| }tjg dt	d	tjg d
t	d	tjg dt	d	d}t
|| tjtdd t t|dd dd}|  W d    d S 1 sxw   Y  d S )Nz'a:b:c
d:e:f
g:h:i
j:k:l:m
l:m:n
o:p:q:rrA   rC   r   zCError tokenizing data\. C error: Expected 3 fields in line 4, saw 4)matchr"   )rC   r   on_bad_lines)r,   dglr-   )r0   ehm)cr   inr   r/   r"   zSkipping liner/   )r   r   pytestraisesparserParserErrorr   r4   r5   objectassert_array_dicts_equalr2   assert_produces_warningr   )r   r   r   msgr(   r?   r   r   r   test_skip_bad_lines   s*   


"z"TestTextReader.test_skip_bad_linesc                 C   s   d}t t|ddd}|j}g dg}||ksJ | }tjddgtjdtjdd	gtjdtjd
dgtjdd}t|| d S )Nz%skip this
skip this
a,b,c
1,2,3
4,5,6rB   r"   rP   r,   r0   rY   r/      r-            r\   )r   r   r   r   r4   r5   rK   rb   )r   r   r   r   r?   recsr   r   r   test_header_not_enough_lines   s   
z+TestTextReader.test_header_not_enough_linesc                 C   sF   d}t t|dd dd}| }dtjdgd tdi}t|| d S )	Nz,\"hello world"
\"hello world"
\"hello world"rB   \)rC   r   
escapecharr   z"hello world"ri   r-   )r   r   r   r4   r5   ra   rb   r>   r   r   r   test_escapechar   s
   zTestTextReader.test_escapecharc                 C      d S Nr   r   r   r   r   test_eof_has_eol   s   zTestTextReader.test_eof_has_eolc                 C   rp   rq   r   rr   r   r   r   test_na_substitution   s   z#TestTextReader.test_na_substitutionc                    s   d  fdd}|dd}|  }|d jdksJ tjg ddd}|d |k s-J |d	 jd
ks6J |dd}|  }|d jdksHJ tjg ddd}|d |k s[J |d	 jdksdJ d S )Nza,1
aa,2
aaa,3
aaaa,4
aaaaa,5c                     s2   d| v rt | d | d< tt fdd d| S )Nr.   rB   rP   r	   r   r   kwdsr   r   r   _make_reader   s   z<TestTextReader.test_numpy_string_dtype.<locals>._make_readerzS5,i4r-   r   S5)r,   aaaaaaaaaaaaaar/   i4S4)r,   r{   r|   r}   r}   )r   r.   r4   r5   all)r   ry   r   r(   	ex_valuesr   rx   r   test_numpy_string_dtype   s   

z&TestTextReader.test_numpy_string_dtypec                    s   d  fdd}|dddd}|  }|d jdksJ |d	 jdks&J |tjtdd}|  }|d jdks<J |d	 jd
ksEJ |tdtd
dd}|  }|d jdks`J |d	 jd
ksiJ d S )Nzone,two
1,a
2,b
3,c
4,dc                     s0   d| v rt | d | d< tt fddi| S )Nr.   rC   rB   ru   rv   rx   r   r   ry      s   z4TestTextReader.test_pass_dtype.<locals>._make_readeru1S1)oner/   r-   r   r/   O)r   r.   r4   uint8ra   )r   ry   r   r(   r   rx   r   test_pass_dtype   s   zTestTextReader.test_pass_dtypec                    sp   d  fdd}|dd}|  }|   }t|dksJ |d |d k s*J |d |d k s6J d S )Nz a,b,c
1,2,3
4,5,6
7,8,9
10,11,12c                     s   t t fddi| S )NrC   rB   )r   r   rv   rx   r   r   ry      s   z1TestTextReader.test_usecols.<locals>._make_reader)r/   r"   )usecolsr"   r/   )r   r#   r   )r   ry   r   r(   expr   rx   r   test_usecols   s   

zTestTextReader.test_usecolsztext, kwargsz a,b,c1,2,34,5,67,8,910,11,12rC   rB   z*a  b  c1  2  34  5  67  8  910  11  12r;   Tz a,b,c1,2,34,5,6,88,910,11,12zdA,B,C,D,E,F,G,H,I,J,K,L,M,N,OAAAAA,BBBBB,0,0,0,0,0,0,0,0,0,0,0,0,0,BBBBB,0,0,0,0,0,0,0,0,0,0,0,0,0zA  B  C  2  34  5  6zA B C2 34 5 6c                 C   sJ   | dd}tt|fi | }tt|fi | }t|| d S )Nz
)replacer   r   r   rb   )r   r   kwargs	nice_textr(   r?   r   r   r   test_cr_delimited  s   z TestTextReader.test_cr_delimitedc                 C   s^   d}t t|dd }tjddgtjdtjddgtdtjd	dgtdd
}t|| d S )Nza,b,c
1,2,3
4,,rB   )rC   r/   rg   r-   2 3r\   )r   r   r   r4   r5   rK   ra   rb   )r   r   r(   r?   r   r   r   test_empty_field_eof!  s   z#TestTextReader.test_empty_field_eofrepeat
   c                 C   s   t dgtjggdgddgd}t g dg dgtdddgd}t g dd	tjtjtjgg d
ddtjtjggtdg dd}ttdddgdd}t|| ttdtddd}t|| ttdtddd}t|| d S )Nr0   r,   rY   )columnsindex)r/   r/   r/   r   abcdr/   )r/   r"   ri   rg   rj   )   	   r            )r   rh         za,b
c
r   )skiprowsnamesenginez1,1,1,1,0
1,1,1,1,0


)r   r   z"0,1,2,3,4
5,6
7,8,9,10,11
12,13,14)r   r4   nanlistr   r   r2   rN   )r   r   r,   r0   rY   dfr   r   r   #test_empty_field_eof_mem_access_bug-  s.   "z2TestTextReader.test_empty_field_eof_mem_access_bugc                 C   sJ   t t dd g dd}t|tsJ W d    d S 1 sw   Y  d S )N   rf   )	chunksizer   r   )r   r   
isinstancer   )r   r   r   r   r   test_empty_csv_inputL  s   "z#TestTextReader.test_empty_csv_inputN) __name__
__module____qualname__r]   fixturer   r   r   r!   r)   r7   r:   r<   r@   rF   rL   rO   re   rl   ro   rs   rt   r   r   r   markparametrizer   r   ranger   r   r   r   r   r   r
      sR    
			





r
   c                 C   s2   |   D ]\}}tt|t||  qd S rq   )itemsr2   r3   r4   asarray)leftrightkvr   r   r   rb   T  s   rb   )__doc__r   r   r   numpyr4   r]   pandas._libs.parsers_libsparsersr_   r   pandas.errorsr   pandasr   pandas._testing_testingr2   pandas.io.parsersr   r   "pandas.io.parsers.c_parser_wrapperr	   r
   rb   r   r   r   r   <module>   s      ;