a
    Sic                     @  s  d dl mZ d dlmZ d dlZd dlm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Zd dlmZ d dlmZ ed	d
ddZedddddZed#dddddZedd
ddZedd
ddZedd
ddZed$dd
dd ZG d!d" d"ZdS )%    )annotations)contextmanagerN)Path)rmtree)IOAnyIterator)
set_option)
get_handlezIterator[IO[bytes]]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)compressionis_textN)r
   handle)pathr   r    r   T/var/www/html/django/DPS/env/lib/python3.9/site-packages/pandas/_testing/contexts.pydecompress_file   s    r   strzIterator[None])tzr   c                 #  sT   ddl  ddl fdd} 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   Nc                   s@   | d u r*z j d= W q< ty&   Y q<0 n|  j d<   d S )NTZ)environKeyErrortzset)r   ostimer   r   setTZD   s    
zset_timezone.<locals>.setTZr   )r   r   r   get)r   r   Zorig_tzr   r   r   set_timezone+   s    
r    Fboolr   )return_filelikekwargsc              	   k  s   t t }| du rd} tt |  } ||  }|  t|}|r`|dd t|f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 modezw+b)r   tempfile
gettempdirr   uuiduuid4touch
setdefaultopen
isinstancecloseis_fileunlink)filenamer"   r#   folderr   Zhandle_or_strr   r   r   ensure_cleanV   s(    


r3   zIterator[str]c                  c  s^   t jdd} z*| V  W zt|  W qZ ty4   Y qZ0 n"zt|  W n tyV   Y n0 0 dS )z{
    Get a temporary directory path and agrees to remove on close.

    Yields
    ------
    Temporary directory path
    r$   )suffixN)r&   mkdtempr   OSError)Zdirectory_namer   r   r   ensure_clean_dir   s    	r7   c               	   c  sH   t tj} z dV  W tj  tj|  ntj  tj|  0 dS )z
    Get a context manager to safely set environment variables

    All changes will be undone on close, hence environment variables set
    within this contextmanager will neither persist nor change global state.
    N)dictr   r   clearupdate)Zsaved_environr   r   r   !ensure_safe_environment_variables   s    


r;   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>   unixexcelz	excel-tabz Cannot override builtin dialect.)csv
ValueErrorregister_dialectunregister_dialect)namer#   r>   Z_BUILTIN_DIALECTSr   r   r   with_csv_dialect   s    rC   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)pandas.core.computationrD   _MIN_ELEMENTSUSE_NUMEXPRr	   )useZmin_elementsexprZolduseZoldminr   r   r   use_numexpr   s    
rJ   c                   @  s:   e Zd ZdZddddZddddZdddd	Zd
S )
RNGContexta,  
    Context manager to set the numpy random number generator speed. Returns
    to the original value upon exiting the context manager.

    Parameters
    ----------
    seed : int
        Seed for numpy.random.seed

    Examples
    --------
    with RNGContext(42):
        np.random.randn()
    Noner   c                 C  s
   || _ d S N)seed)selfrN   r   r   r   __init__   s    zRNGContext.__init__c                 C  s   t j | _t j| j d S rM   )nprandom	get_statestart_staterN   )rO   r   r   r   	__enter__   s    zRNGContext.__enter__c                 C  s   t j| j d S rM   )rQ   rR   	set_staterT   )rO   exc_type	exc_value	tracebackr   r   r   __exit__   s    zRNGContext.__exit__N)__name__
__module____qualname____doc__rP   rU   rZ   r   r   r   r   rK      s   rK   )NF)N)
__future__r   
contextlibr   r   pathlibr   shutilr   r&   typingr   r   r   r(   numpyrQ   pandasr	   pandas.io.commonr
   r   r    r3   r7   r;   rC   rJ   rK   r   r   r   r   <module>   s2   **!