a
    ï½„g—  ã                   @  s2  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 d dlmZ d d	lmZ d d
lmZ e
r°d dlmZ d dlmZmZmZ eddddœdd„ƒZedddœdd„ƒZed+ddddœdd„ƒZedddœd d!„ƒZ ed,dd"œd#d$„ƒZ!d-d'd(„Z"d.d)d*„Z#dS )/é    )Úannotations)ÚcontextmanagerN)ÚPath)ÚIOÚTYPE_CHECKINGÚAny)Úusing_copy_on_write)ÚPYPY)ÚChainedAssignmentError)Ú
set_option)Ú
get_handle)Ú	Generator)Ú
BaseBufferÚCompressionOptionsÚFilePathzFilePath | BaseBufferr   z Generator[IO[bytes], None, None])ÚpathÚcompressionÚreturnc                 c  s<   t | d|dd��}|jV  W d  ƒ n1 s.0    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   ú[/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/pandas/_testing/contexts.pyÚdecompress_file!   s    r   ÚstrzGenerator[None, None, None])Útzr   c                 #  sP   ddl ‰ ddœ‡ fdd„}tj d¡}|| ƒ zdV  W ||ƒ n
||ƒ 0 dS )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   NÚNone©r   c                   s@   | d u r*zt jd= W q< ty&   Y q<0 n| t jd< ˆ  ¡  d S )NÚTZ)ÚosÚenvironÚKeyErrorÚtzset)r   ©Útimer   r   ÚsetTZP   s    
zset_timezone.<locals>.setTZr   )r#   r   r   Úget)r   r$   Zorig_tzr   r"   r   Úset_timezone8   s    
r&   FÚboolr   zGenerator[Any, None, None])Úreturn_filelikeÚkwargsr   c              	   k  sÞ   t t ¡ ƒ}| du rd} tt ¡ ƒ|  } ||  }| ¡  t|ƒ}| dd¡}|rˆ| dd¡ |du rtd|d vrtd}t	|fd|i|¤Ž}z,|V  W t
|tƒs¤| ¡  | ¡ rÚ| ¡  n$t
|tƒsÈ| ¡  | ¡ rØ| ¡  0 dS )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   ÚtempfileÚ
gettempdirr   ÚuuidÚuuid4ÚtouchÚpopÚ
setdefaultÚopenÚ
isinstanceÚcloseÚis_fileÚunlink)Úfilenamer(   r)   Úfolderr   Zhandle_or_strr+   r   r   r   Úensure_cleanb   s.    

ý
r<   )Únamer   c              	   k  sX   ddl }h d£}| |v r tdƒ‚|j| fi |¤Ž zdV  W | | ¡ n| | ¡ 0 dS )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>   ÚexcelÚunixz	excel-tabz Cannot override builtin dialect.)ÚcsvÚ
ValueErrorÚregister_dialectÚunregister_dialect)r=   r)   r@   Z_BUILTIN_DIALECTSr   r   r   Úwith_csv_dialect’   s    rD   r   c              	   c  sh   ddl m} |d u r|j}|j}|j}td| ƒ ||_zd V  W ||_td|ƒ n||_td|ƒ 0 d S )Nr   )Úexpressionszcompute.use_numexpr)Zpandas.core.computationrE   Z_MIN_ELEMENTSZUSE_NUMEXPRr   )ZuseZmin_elementsÚexprZolduseZoldminr   r   r   Úuse_numexpr´   s    
ÿrG   Tr   c                 C  sœ   ddl m} | s"ddlm} |ƒ S tr<|s<ddlm} |ƒ S trV|rV||d |¡d�S tƒ rft}d}nt}d}|r~|g|¢R }||d |g|¢R ¡d�S 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._testingrI   Ú
contextlibrK   r	   Újoinr   r
   ÚFutureWarning)ÚwarnZextra_warningsZextra_matchrI   rK   ÚwarningrM   r   r   r   Úraises_chained_assignment_errorÆ   s0    þÿþrT   c                 K  s>   ddl m} | s"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   rH   rJ   zSetting a value on a viewrM   )rN   rI   rO   rK   rQ   )rR   rM   r)   rI   rK   r   r   r   Úassert_cow_warningê   s    rU   )NF)N)Tr   r   )TN)$Ú
__future__r   rO   r   r   Úpathlibr   r.   Útypingr   r   r   r0   Zpandas._configr   Zpandas.compatr	   Zpandas.errorsr
   Zpandasr   Zpandas.io.commonr   Úcollections.abcr   Zpandas._typingr   r   r   r   r&   r<   rD   rG   rT   rU   r   r   r   r   Ú<module>   s6   ) ÿ/!
$