o
    EhQ                     @  s  d dl mZ d dlmZ d dlZd dlmZ d dlZd dlm	Z	m
Z
mZ d dlZd dlmZ d dlmZmZ d dlmZ d d	lmZ d d
lmZ e
rZd dlmZ d dlmZmZmZ ed-ddZed.ddZe	d/d0d d!Z ed1d#d$Z!ed2d3d%d&Z"d4d)d*Z#d5d+d,Z$dS )6    )annotations)contextmanagerN)Path)IOTYPE_CHECKINGAny)using_copy_on_write)PYPYWARNING_CHECK_DISABLED)ChainedAssignmentError)
set_option)
get_handle)	Generator)
BaseBufferCompressionOptionsFilePathpathFilePath | BaseBuffercompressionr   return Generator[IO[bytes], None, None]c                 c  s@    t | d|dd}|jV  W d   dS 1 sw   Y  dS )a&  
    Open a compressed file and return a file object.

    Parameters
    ----------
    path : str
        The path where the file is read from.

    compression : {'gzip', 'bz2', 'zip', 'xz', 'zstd', None}
        Name of the decompression to use

    Returns
    -------
    file object
    rbF)r   Zis_textN)r   handle)r   r   r    r   8lib/python3.10/site-packages/pandas/_testing/contexts.pydecompress_file$   s   
"r   tzstrGenerator[None, None, None]c                 #  sL    ddl  d fdd}tjd}||  z
dV  W || dS || w )	a  
    Context manager for temporarily setting a timezone.

    Parameters
    ----------
    tz : str
        A string representing a valid timezone.

    Examples
    --------
    >>> from datetime import datetime
    >>> from dateutil.tz import tzlocal
    >>> tzlocal().tzname(datetime(2021, 1, 1))  # doctest: +SKIP
    'IST'

    >>> with set_timezone('US/Eastern'):
    ...     tzlocal().tzname(datetime(2021, 1, 1))
    ...
    'EST'
    r   Nr   Nonec                   sP   t  dr&| d u rztjd= W d S  ty   Y d S w | tjd<    d S d S )NtzsetTZ)hasattrosenvironKeyErrorr    )r   timer   r   setTZS   s   

zset_timezone.<locals>.setTZr!   )r   r   )r'   r#   r$   get)r   r(   Zorig_tzr   r&   r   set_timezone;   s   r*   Freturn_filelikeboolkwargsr   Generator[Any, None, None]c              	   k  s    t t }| du rd} tt |  } ||  }|  t|}|dd}|rE|dd |du r;d|d vr;d}t	|fd|i|}z|V  W t
|tsS|  | r]|  dS dS t
|tsh|  | rq|  w w )az  
    Gets a temporary path and agrees to remove on close.

    This implementation does not use tempfile.mkstemp to avoid having a file handle.
    If the code using the returned path wants to delete the file itself, windows
    requires that no program has a file handle to it.

    Parameters
    ----------
    filename : str (optional)
        suffix of the created file.
    return_filelike : bool (default False)
        if True, returns a file-like which is *always* cleaned. Necessary for
        savefig and other functions which want to append extensions.
    **kwargs
        Additional keywords are passed to open().

    N encodingmodezw+bbzutf-8)r   tempfileZ
gettempdirr   uuidZuuid4Ztouchpop
setdefaultopen
isinstancecloseis_fileunlink)filenamer+   r-   Zfolderr   Zhandle_or_strr0   r   r   r   ensure_cleanf   s4   


r=   namec              	   k  sX    ddl }h d}| |v rtd|j| fi | zdV  W ||  dS ||  w )au  
    Context manager to temporarily register a CSV dialect for parsing CSV.

    Parameters
    ----------
    name : str
        The name of the dialect.
    kwargs : mapping
        The parameters for the dialect.

    Raises
    ------
    ValueError : the name of the dialect conflicts with a builtin one.

    See Also
    --------
    csv : Python's CSV library.
    r   N>   ZexcelZunixz	excel-tabz Cannot override builtin dialect.)csv
ValueErrorZregister_dialectZunregister_dialect)r>   r-   r?   Z_BUILTIN_DIALECTSr   r   r   with_csv_dialect   s   rA   c              	   c  sh    ddl m} |d u r|j}|j}|j}td|  ||_zd V  W ||_td| d S ||_td| w )Nr   )expressionszcompute.use_numexpr)Zpandas.core.computationrB   Z_MIN_ELEMENTSZUSE_NUMEXPRr   )ZuseZmin_elementsexprZolduseZoldminr   r   r   use_numexpr   s   
rD   Tr   c                 C  s   ddl m} | sddlm} | S tstr |s ddlm} | S ts$tr/|r/||d|dS t r7t}d}nt	}d}|rC|g|R }||d|g|R dS )Nr   assert_produces_warningnullcontext|)matchzYA value is trying to be set on a copy of a DataFrame or Series through chained assignmentr   )
pandas._testingrF   
contextlibrH   r	   r
   joinr   r   FutureWarning)warnZextra_warningsZextra_matchrF   rH   ZwarningrJ   r   r   r   raises_chained_assignment_error   s0   rP   c                 K  sB   ddl m} | r
trddlm} | S |sd}|tfd|i|S )aq  
    Assert that a warning is raised in the CoW warning mode.

    Parameters
    ----------
    warn : bool, default True
        By default, check that a warning is raised. Can be turned off by passing False.
    match : str
        The warning message to match against, if different from the default.
    kwargs
        Passed through to assert_produces_warning
    r   rE   rG   zSetting a value on a viewrJ   )rK   rF   r
   rL   rH   rN   )rO   rJ   r-   rF   rH   r   r   r   assert_cow_warning   s   rQ   )r   r   r   r   r   r   )r   r   r   r   )NF)r+   r,   r-   r   r   r.   )r>   r   r   r   )N)r   r   )Tr   r   )TN)%Z
__future__r   rL   r   r#   Zpathlibr   r3   typingr   r   r   r4   Zpandas._configr   Zpandas.compatr	   r
   Zpandas.errorsr   Zpandasr   Zpandas.io.commonr   Zcollections.abcr   Zpandas._typingr   r   r   r   r*   r=   rA   rD   rP   rQ   r   r   r   r   <module>   s8    */!
$