'Vfi dZddlmZddlmZmZddlZddlmZddl m Z m Z m Z ddl Z ddlZddlZddlmZmZmZmZmZmZddlZddlZddlmZddlZddlZdd lmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'dd l(m)Z)m*Z+m,Z,m-Z-m.Z.ddl/Z/ddl0Z0dd l1m2Z2m3Z3dd l4m5Z5m6Z6dd l7m8Z8ddl9m:Z:ddl;mZ>m?Z?m@Z@mAZAddlBmCZCddlDmEZEeFe.e,ze-zZGeGHdejIdZJe%de2ZKerddlLmMZMddl1mNZNmOZOmPZPmQZQmRZRmSZSddlTmUZUe jVGddZWe jVGdde#e!ZXdd"ZYe'dd%ZZe'dd&ZZdd(ZZdd+Z[e'ddd/Z\e' ddd0Z\ ddd3Z\d4Z]dd6Z^e:eEd7eEd8d#z9 dddAZ_ddCZ`dDdDdDdDdEdFdGdHdIdJ ZaeFeabZcddMZde:eEd8d#zNddPZeddRZfe'd,d,d,d,d,dSdd[Zge'd,d,d,d,d,d,d\dd_Zge'd,d,d,d,d,d,d\ddaZge:eEd8dTzNddd1dbddd\ddcZgGdddeeeZhGdfdgehZiGdhdiehZjGdjdkZkGdldmZlddqZmddrZnddsZoejpdduZq dddzZrdd~ZsdS)zCommon IO api utilities) annotations)ABCabstractmethodN) defaultdict)HashableMappingSequence)BufferedIOBaseBytesIO RawIOBaseStringIO TextIOBase TextIOWrapper)Path) IO TYPE_CHECKINGAnyAnyStr DefaultDictGenericLiteralTypeVarcastoverload)urljoinurlparse uses_netloc uses_params uses_relative) BaseBuffer ReadCsvBuffer) get_bz2_file get_lzma_file)import_optional_dependency)doc)find_stack_level)is_bool is_file_like is_integer is_list_like) ABCMultiIndex) _shared_docsz^[A-Za-z][A-Za-z0-9+\-+.]*:// BaseBufferT)bound) TracebackType)CompressionDictCompressionOptionsFilePath ReadBufferStorageOptions WriteBuffer) MultiIndexcJeZdZUdZded<ded<ded<ded<d Zd ed <d S) IOArgsz? Return value of io/common.py:_get_filepath_or_buffer. str | BaseBufferfilepath_or_bufferstrencodingmoder1 compressionFbool should_closeN)__name__ __module__ __qualname____doc____annotations__rA?c:\PYTHON\DbComparer\venv\Lib\site-packages\pandas/io/common.pyr9r9asY)(((MMM III    LrHr9czeZdZUdZded<ded<ejeZded<d Z d ed <ddZ ddZ ddZ dS) IOHandlesau Return value of io/common.py:get_handle Can be used as a context manager. This is used to easily close created buffers and to handle corner cases when TextIOWrapper is inserted. handle: The file handle to be used. created_handles: All file handles that are created by get_handle is_wrapped: Whether a TextIOWrapper needs to be detached. z IO[AnyStr]handler1r?)default_factoryzlist[IO[bytes] | IO[str]]created_handlesFr@ is_wrappedreturnNonecF|jrmt|jtsJ|j|j|j|j|jD]}|g|_d|_dS)z Close all created buffers. Note: If a TextIOWrapper was inserted, it is flushed and detached to avoid closing the potentially user-created buffer. FN) rO isinstancerLrflushdetachrNremoveclose)selfrLs rIrWzIOHandles.closes ? 5dk=99 9 99 K      K    ' ' 4 4 4*  F LLNNNN!rHIOHandles[AnyStr]c|SNrGrXs rI __enter__zIOHandles.__enter__s rHexc_typetype[BaseException] | None exc_valueBaseException | None tracebackTracebackType | Nonec.|dSr[)rW)rXr^r`rbs rI__exit__zIOHandles.__exit__s rHNrPrQ)rPrY)r^r_r`rarbrcrPrQ) rBrCrDrErF dataclassesfieldlistrNrOrWr]rerGrHrIrKrKns      1B1BSW1X1X1XOXXXXJ    "rHrKurlobjectrPr@cft|tsdSt|jtvS)z Check to see if a URL has a valid protocol. Parameters ---------- url : str or unicode Returns ------- isurl : bool If `url` has a valid protocol return True otherwise False. F)rSr< parse_urlscheme _VALID_URLSrjs rIis_urlrqs/ c3  u S>> K //rHr;r<cdSr[rGr;s rI _expand_userrtCrHcdSr[rGrss rIrtrtrurHstr | BaseBufferTcnt|trtj|S|S)a] Return the argument with an initial component of ~ or ~user replaced by that user's home directory. Parameters ---------- filepath_or_buffer : object to be converted if possible Returns ------- expanded_filepath_or_buffer : an expanded filepath or the input if not expandable )rSr<ospath expanduserrss rIrtrts4$c**6w!!"4555 rHheaderrQc|dSt|r,tt|}|dkrtddSt |drptt |}t tt|stdtd|DrtddSt|rtdtd) NrzUPassing negative integer to header is invalid. For no header, use header=None insteadF) allow_setsz*header must be integer or list of integersc3"K|] }|dkV dS)rNrG).0is rI z&validate_header_arg..s&%%q1u%%%%%%rHz8cannot specify multi-index header with negative integerszPassing a bool to header is invalid. Use header=None for no header or header=int or list-like of ints to specify the row(s) making up the column names) r)rint ValueErrorr*r allmapanyr' TypeError)r|s rIvalidate_header_argrs  ~&c6"" A::9  Fu---h''3z6**++ KIJJ J %%f%%% % % YWXX Xv  4   A B BBrH.r3convert_file_likecdSr[rGr;rs rIstringify_pathrrurHcdSr[rGrs rIrrs CrHFFilePath | BaseBufferTc|s$t|rtt|St|tjr|}t|S)a Attempt to convert a path-like object to a string. Parameters ---------- filepath_or_buffer : object to be converted Returns ------- str_filepath_or_buffer : maybe a string version of the object Notes ----- Objects supporting the fspath protocol are coerced according to its __fspath__ method. Any other object is passed through unchanged, which includes bytes, strings, buffers, or anything else that's not even path-like. )r(rr.rSryPathLike __fspath__rtrs rIrrse. 5.@!A!A5K!3444$bk22=/::<< * + ++rHc.ddl}|jj|i|S)z` Lazy-import wrapper for stdlib urlopen, as that imports a big chunk of the stdlib. rN)urllib.requestrequesturlopen)argskwargsurllibs rIrrs*  !6> !4 26 2 22rHFilePath | BaseBufferct|toStorageOptions | Nonec(t|}t|\}}t||}|r>t|dr.d|vr*t jdt td}t||}d|vr5|dvr1|d vr-t j|d |tt|}d |vr d|vr|dz }t|trt|r|pi}d dl }|j|| }t!|5} | jdd} | dkrddi}t'| } dddn #1swxYwYt+| ||d|St-|rBt|tsJ|dr|dd}|dr|dd}t3d} g} t3dd dlm}m}||t:g} n#t<$rYnwxYw | j|fd|i|pi}nW#tA| $rB|ddi}nt|}d|d<| j|fd|i|pi}YnwxYwt+|||d|S|rtCdt|ttDtFj#fr!t+tI|||d|St|ds1t|ds!dtK|}tC|t+|||d|S)a If the filepath_or_buffer is a url, translate and return the buffer. Otherwise passthrough. Parameters ---------- filepath_or_buffer : a url, filepath (str, py.path.local or pathlib.Path), or buffer {compression_options} .. versionchanged:: 1.4.0 Zstandard support. encoding : the encoding to use to decode bytes, default is 'utf-8' mode : str, optional {storage_options} Returns the dataclass IOArgs. writebzDcompression has no effect when passing a non-binary object as input.) stacklevelN)methodw)bz2xz)zutf-16zutf-32z( will not write the byte order mark for tr)headerszContent-EncodinggziprT)r;r=r?rAr>zs3a://zs3://zs3n://fsspecbotocore) ClientErrorNoCredentialsErrorr>anonz?storage_options passed with file object or non-fsspec file pathFreadz)Invalid file path or buffer object type: )&rget_compression_methodinfer_compressionhasattrwarningswarnRuntimeWarningr&dictUnicodeWarningrSr<rqrrRequestrrgetr rr9rrreplacer$botocore.exceptionsrrPermissionError ImportErroropentuplerbytesmmaprttype)r;r=r?r>rcompression_method fsspec_moderreq_inforeqcontent_encodingreaderrerr_types_to_retry_with_anonrrfile_objmsgs rI_get_filepath_or_bufferr0s5>((:;;'=[&I&I# *+=?QRR"g&8'BB"sRV R '))    "{+=>>>K t - / / , , ,  N NH N N '))    K +#["8"8s $c** v6H/I/I */R >))*> +,>??#      "F++/67I7/S/SU$?Q:R:RTToo -     s8"AE55E9<E9#!I II$I;;AKKrzc@ddlm}td||S)z converts an absolute native path to a FILE URL. Parameters ---------- path : a path in native format Returns ------- a valid FILE URL r) pathname2urlzfile:)rrr)rzrs rIfile_path_to_urlrs0,+++++ 7LL.. / //rHtarrrziprzstd) .tar.tar.gz.tar.bz2.tar.xz.gz.bz2.zip.xzz.zstr2"tuple[str | None, CompressionDict]ct|trHt|} |d}n&#t$r}t d|d}~wwxYwi}|}||fS)a Simplifies a compression argument to a compression method string and a mapping containing additional arguments. Parameters ---------- compression : str or mapping If string, specifies the compression method. If mapping, value at key 'method' specifies compression method. Returns ------- tuple of ({compression method}, Optional[str] {compression arguments}, Dict[str, Any]) Raises ------ ValueError on mapping missing 'method' key rz.If mapping, compression must have key 'method'N)rSrrpopKeyErrorr)r?compression_argsrerrs rIrrs.+w'') ,, X!1!5!5h!?!?   X X XMNNTW W X( / //s< AAA)r str | Nonect|dS|dkrtt|d}t|tsdStD]0\}}||r|cS1dS|tvr|Sddgttz}d|d|}t|)a/ Get the compression method for filepath_or_buffer. If compression='infer', the inferred compression method is returned. Otherwise, the input compression method is returned unchanged, unless it's invalid, in which case an error is raised. Parameters ---------- filepath_or_buffer : str or file handle File path or object. {compression_options} .. versionchanged:: 1.4.0 Zstandard support. Returns ------- string or None Raises ------ ValueError on invalid compression specified. NinferT)rUnrecognized compression type: z Valid compression types are ) rrSr<extension_to_compressionitemslowerendswith_supported_compressionssortedr)r;r? extensionvalidrs rIrr&s4tg+,>RVWWW,c22 4'?&D&D&F&F # # "I{!''))229== #"""" #t--- dOf%<== =E /+ / /', / / S//rH Path | strc|t|j}|std|ddS)z Check if parent directory of a file exists, raise OSError if it does not Parameters ---------- path: Path or str Path to check parent directory of z1Cannot save file into a non-existent directory: ''N)rparentis_dirOSError)rzrs rIcheck_parent_directoryr]sI$ZZ F ==??VT6TTTUUUVVrH)r=r? memory_maperrorsr path_or_bufris_textLiteral[False]rr5IOHandles[bytes]cdSr[rGrr>r=r?rrrrs rI get_handlerk CrH)r=r?rrrr Literal[True]IOHandles[str]cdSr[rGrs rIrrzrrH!IOHandles[str] | IOHandles[bytes]cdSr[rGrs rIrrrrHTcF |pd}|pd}t||r d|vr|dz }tj|t|trtj|t |||||}|j} t| |\} }} t| t} t|j } | d}d|vr| rtt | |r9|dkr!|j dd |_ n|dkrd|j vr|xj dz c_ |d krKt| trtjd*| |j d | } ntjd*| |j d | } nu|d krt#| fd|j i| } nQ|dkrt%| |j fi| } | jj dkr| | | j} t-| dkr.| j| } n| st1d|t1d| n|dkr| d|j t| trt5d*d| i| } nt5d*d| i| } t| t4sJd| jj vr| | | j}t-|dkr'| j|d}|J|} n|st1d|t1d|n|dkrt;| |j fi| } ng|dkrMt=d}d|j vrd|jd*i| i}nd|j d*i| i}|j| fd|j i|} nd|}t1|t| trJ| | nnt| trY|j!r(d|j vrt/| |j |j!|d } nt/| |j } | | d }|s7|j d!kr,t| tDrtG| |j!"} n|r|s|st| |j rtI| d#r tI| d$rtI| d%stK| } tM| |j!|d } | | t|jtp|j' }d|j vr5tI| d&s%tQd'tS|jd(| *|j'r6t|jtrJ| |jtW| | ||j )S)+a# Get file handle for given path/buffer and mode. Parameters ---------- path_or_buf : str or file handle File path or object. mode : str Mode to open path_or_buf with. encoding : str or None Encoding to use. {compression_options} May be a dict with key 'method' as compression mode and other keys as compression options if compression mode is 'zip'. Passing compression options as keys in dict is supported for compression modes 'gzip', 'bz2', 'zstd' and 'zip'. .. versionchanged:: 1.4.0 Zstandard support. memory_map : bool, default False See parsers._parser_params for more information. Only used by read_csv. is_text : bool, default True Whether the type of the content passed to the file/buffer is string or bytes. This is not the same as `"b" not in mode`. If a string content is passed to a binary file/buffer, a wrapper is inserted. errors : str, default 'strict' Specifies how encoding and decoding errors are to be handled. See the errors argument for :func:`open` for a full list of options. storage_options: StorageOptions = None Passed to _get_filepath_or_buffer Returns the dataclass IOHandles rstrictr)r=r?r>rrrrrr-r)filenamer>)fileobjr>rr>rzZero files found in ZIP file z9Multiple files found in ZIP file. Only one file per ZIP: rnamer rNz Zero files found in TAR archive zDMultiple files found in TAR archive. Only one file per TAR archive: r zstandarddctxcctxr)r=rnewlineFrb)r=readablewritableseekablerz1Expected file path name or file-like object, got z type)rLrNrOr?rG),_is_binary_modecodecslookuprSr< lookup_errorrr;_maybe_memory_maprr?rrr>rrGzipFiler" _BytesZipFilebufferappendnamelistlenrr setdefault _BytesTarFilegetnames extractfiler#r$ZstdDecompressorZstdCompressorr=r_BytesIOWrapperr _IOWrapperrrArrreverserK)rr>r=r?rrrrioargsrLhandlesis_pathr zip_namesfilesfiler open_argsrrOs rIrrsd"7H  xF{D))coo   M(&#$F###% ' F &F #4FJ"G"GFJ%%GF.//"&&x00K $7s6{{+++D &  +--c266FKK F " "s&+'='= KK3 KK & &#&& #' #  ' E ! !$\^^[#FFE ! !# '7F}!S((v&&&"M2244 y>>Q&&#]// @@FF"$%R[%R%RSSS$>2;>>)E ! !  ' ' < < <&#&& &GGFG6FGG '"&6fm44 4 44fm(((v&&& ..00u::??!=44U1X>>D+++!FF$%U %U%UVVV$B:?BB) D %]__ '7FF F " "-k::Dfk!!#%:T%:%N%N=M%N%NO #%8T%8%L%L;K%L%LM TY[FFBKAACS// !fc*****v FC  ? /s&+55  FF&&+..FvJ  v{d**z&*/M/M* _      ! %4VV[%I%I  ++ (6:.. (6:.. (  ''F _     v v0# 6 6 M&:M  fk'&&"9"9 :122 : : :    OO 2f7=====v0111  &    rHcPeZdZdZeZeddZdfd ZxZ S)_BufferedWriterz Some objects do not support multiple .write() calls (TarFile and ZipFile). This wrapper writes to the underlying buffer on close. rPrQcdSr[rGr\s rIwrite_to_bufferz_BufferedWriter.write_to_buffers rHcP|jrdS|jrI|d|j5|dddn #1swxYwYn|jtdS)Nr)closed getbuffernbytesseekrr6rWsuper)rX __class__s rIrWz_BufferedWriter.closes ;  F >>   " IIaLLL ' '$$&&& ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' K       sA!!A%(A%rf) rBrCrDrEr rrr6rW __classcell__r=s@rIr4r4sr WYYF   ^           rHr4cBeZdZ ddfd ZddZddZddZxZS)r$Nrrrr>Literal['r', 'a', 'w', 'x']r -ReadBuffer[bytes] | WriteBuffer[bytes] | None archive_namerPrQc t||_||_t jjd||||d||_dS)N)rr>r rG) r<__init__rCrtarfileTarFiler extend_moder)rXrr>r rCrr=s rIrEz_BytesTarFile.__init__sq ( (/';( !!$''( (  ( (  rHr<c|dd}|dkr|S|j,t|jj}|dvr|d|dd}|S)Nrr-r)rrr:r)rrrsuffix)rXr>rKs rIrHz_BytesTarFile.extend_modesg||C$$ 3;;K 9 $)__+F///---- rHc|jdSt|j}|jdkr|djS|jdvr-|ddjS|jS)z If an explicit archive_name is not given, we still want the file inside the zip file not to be named something.tar, because that causes confusion (GH39465). Nrr-)rrr)rrrK with_suffixrXr s rIinfer_filenamez_BytesTarFile.infer_filenames} 9 4 ?? ?f $ $''++0 0 _ B B B''++77;;@ @}rHc|jp|pd}tj|}t ||_|j||dS)Nr)r) rCrOrFTarInfor"getvaluesizeraddfile)rXrCtarinfos rIr6z_BytesTarFile.write_to_buffersg(JD,?,?,A,AJU /|4444==??++  GT*****rH)NrNN) rrr>rAr rBrCrrPrQ)r>r<rPr<rPrrf)rBrCrDrErHrOr6r>r?s@rIr$r$s ,/AE#'        (    ++++++++rHr$c4eZdZ d dfd Zdd Zdd ZxZS)rNr11FilePath | ReadBuffer[bytes] | WriteBuffer[bytes]r>r<rCrrPrQc t|dd}||_|dt jt j||fi||_dS)Nrr-r?) r<rErrCr#zipfile ZIP_DEFLATEDZipFiler)rXr1r>rCrr=s rIrEz_BytesZipFile.__init__sz ||C$$(-)=>>>(/ $( (  ( (  rHct|jjtjt frEt |jj}|jdkr|dj S|j SdS)z If an explicit archive_name is not given, we still want the file inside the zip file not to be named something.zip, because that causes confusion (GH39465). rr-N) rSrr ryrr<rrKrMrrNs rIrOz_BytesZipFile.infer_filenamese dk*R[#,> ? ? !DK011H&((++B//44= trHc|jp|pd}|j||dS)Nr)rCrOrwritestrrR)rXrCs rIr6z_BytesZipFile.write_to_buffersF(JD,?,?,A,AJU  \4==??;;;;;rHr[)r1rXr>r<rCrrPrQrVrf)rBrCrDrErOr6r>r?s@rIrrso $(        $    <<<<<<<> "mj8 9Ac*=&>&>!>!>DM& &+BQB/I/3DM rHN)r)rrlr=r<rPrQ)ror<)rq)rrrsrPr)rBrCrDrErerrGrHrIr)r)@sZ****       rHr)rLr:/tuple[str | BaseBuffer, bool, list[BaseBuffer]]c4g}|t|dpt|tz}|s|||fStt|}t|tr%t |d}|| ttj| dtj }t|D]}| n+#t|D]}| wxYw|||gfS)zTry to memory map file/buffer.filenorr)access) rrSr<rr!rr r*rr{ ACCESS_READreversedrW)rLrr-wrappeds rIrr]s/!#G'&(++Fz&#/F/FFJ +z7**- ( (F&#fd##v  I 4+;      w''  F LLNNNN hw''  F LLNNNN  J ))s AC))(Dcd}t|}t|ts|S tj|}n#t tf$rYnwxYw|S)zTest whether file exists.F)rrSr<ryrzexistsrr)r;rs rI file_existsrss F'(:;; (# . .   233 z "     MsA AAcd|vsd|vrd|vStjtjtjf}t t ||rdSt |tpdt|d|vS)z+Whether the handle is opened in binary moderrFr>) r StreamWriter StreamReaderStreamReaderWriter issubclassrrS_get_binary_io_classesrd)rLr> text_classess rIrrs d{{cTkkd{ ! L $v,, --u f466 7 7 3'CC<rHtuple[type, ...]cttf}tdd}|R|d5}|t |fz }dddn #1swxYwY|S)z!IO classes that that expect bytesrignore)rNrH)r r r$r' stream_readerr)binary_classesrrs rIrrs)7 'BN &k( C C CD   " " $ $ 2 23 7 7 .6 tF||o -N . . . . . . . . . . . . . . . s A))A-0A-columnsSequence[Hashable] | MultiIndex index_colbool | Sequence[int] | Nonecttrgtt|o0t|t ot fd|DS)a Check whether or not the `columns` parameter could be converted into a MultiIndex. Parameters ---------- columns : array-like Object which may or may not be convertible into a MultiIndex index_col : None, bool or list, optional Column or columns to use as the (possibly hierarchical) index Returns ------- bool : Whether or not columns could become a MultiIndex Nc3dK|]*}|tvt|tV+dSr[)rirSr)rcrs rIrz+is_potential_multi_index..s=RR$y//9Q9Q 1e$$9Q9Q9Q9QRRrH)rSr@r"r+r)rrs `rIis_potential_multi_indexrsv&Jy$77  G  S7M22 2 S RRRRgRRR R R  rHnamesSequence[Hashable]is_potential_multiindexcLt|}tt}t|D]p\}}||}|dkrP|dz||<|r1t |t sJ|dd|dd|fz}n|d|}||}|dkP|||<|dz||<q|S)a: Rename column names if duplicates exist. Currently the renaming is done by appending a period and an autonumeric, but a custom pattern may be supported in the future. Examples -------- >>> dedup_names(["x", "y", "x", "x"], is_potential_multiindex=False) ['x', 'y', 'x.1', 'x.2'] rrNrq.)rirr enumeraterSr)rrcountsrcol cur_counts rI dedup_namesrs KKE)4S)9)9FE""$$33K !mm#a-F3K& +!#u-----#2#hSW":":y":":!<<**y**s I!mma!ms LrH)rjrkrPr@)r;r<rPr<)r;r.rPr.)r;rwrPrw)r|rkrPrQ).)r;r3rr@rPr<)r;r.rr@rPr.)F)r;rrr@rPrw)rjrrPr@)rNrN) r;rr=r<r?rr>r<rrrPr9)rzr<rPr<)r?r2rPr)r;rr?rrPr)rzrrPrQ)rrr>r<r=rr?r2rr@rrrrrr5rPr)rrr>r<r=rr?r2rr@rrrrrr5rPr)rrr>r<r=rr?r2rr@rr@rrrr5rPr)rrr>r<r=rr?rrr@rr@rrrrrPr)rLr:rr@rPry)r;rrPr@)rLrr>r<rPr@)rPrr[)rrrrrPr@)rrrr@rPr)trE __future__rabcrrr collectionsrcollections.abcrrr rg functoolsrior r r r rrrrypathlibrrerFtypingrrrrrrrrrr urllib.parserrrmrrrrrZpandas._typingr r! pandas.compatr"r#pandas.compat._optionalr$pandas.util._decoratorsr%pandas.util._exceptionsr&pandas.core.dtypes.commonr'r(r)r*pandas.core.dtypes.genericr+pandas.core.shared_docsr,setrodiscardcompilerr.typesr0r1r2r3r4r5r6pandasr7 dataclassr9rKrqrtrrrrrrrvaluesrrrrrr4r$rr*r)rrr lru_cacherrrrGrHrIrs """""" ######                            ?>>>>>''''''444444 544444000000c-+- ;<< BBJ?@@gm:666  "######"!!!!!          ........b0000$    &CCCC:   ?B $,,,,,D333     !23$%:;>RR -1-1 llll l^0000&        #6==??@@ 0 0 0 0F&;