a
    wDf                     @  sx   d dl mZ d dlZd dlZd dlZd dlZd dlmZ d dlm	Z	 d dl
mZ edddd	d
 ZG dd de	ZdS )    )annotationsN)default_timer)Callback)_deprecatedz2022.6.0zdask.utils.format_time)Zafter_versionZuse_insteadc                 C  sd   t | d\}}t |d\}}|r<|dd|dd|ddS |rT|dd|ddS |ddS dS )zFormat seconds into a human readable form.

    >>> format_time(10.4)  # doctest: +SKIP
    '10.4s'
    >>> format_time(1000.4)  # doctest: +SKIP
    '16min 40.4s'
    <   z2.0fzhr zmin z4.1fsN)divmod)tmr   h r   f/nfs/NAS7/SABIOD/METHODE/ermites/ermites_venv/lib/python3.9/site-packages/dask/diagnostics/progress.pyformat_time   s    	r   c                   @  sJ   e Zd ZdZdddZdd	 Zd
d Zdd Zdd Zdd Z	dd Z
dS )ProgressBara%  A progress bar for dask.

    Parameters
    ----------
    minimum : int, optional
        Minimum time threshold in seconds before displaying a progress bar.
        Default is 0 (always display)
    width : int, optional
        Width of the bar
    dt : float, optional
        Update resolution in seconds, default is 0.1 seconds
    out : file object, optional
        File object to which the progress bar will be written
        It can be ``sys.stdout``, ``sys.stderr`` or any other file object able to write ``str`` objects
        Default is ``sys.stdout``

    Examples
    --------

    Below we create a progress bar with a minimum threshold of 1 second before
    displaying. For cheap computations nothing is shown:

    >>> with ProgressBar(minimum=1.0):      # doctest: +SKIP
    ...     out = some_fast_computation.compute()

    But for expensive computations a full progress bar is displayed:

    >>> with ProgressBar(minimum=1.0):      # doctest: +SKIP
    ...     out = some_slow_computation.compute()
    [########################################] | 100% Completed | 10.4 s

    The duration of the last computation is available as an attribute

    >>> pbar = ProgressBar()                # doctest: +SKIP
    >>> with pbar:                          # doctest: +SKIP
    ...     out = some_computation.compute()
    [########################################] | 100% Completed | 10.4 s
    >>> pbar.last_duration                  # doctest: +SKIP
    10.4

    You can also register a progress bar so that it displays for all
    computations:

    >>> pbar = ProgressBar()                # doctest: +SKIP
    >>> pbar.register()                     # doctest: +SKIP
    >>> some_slow_computation.compute()     # doctest: +SKIP
    [########################################] | 100% Completed | 10.4 s
    r   (   皙?Nc                 C  s0   |d u rt j}|| _|| _|| _|| _d| _d S )Nr   )sysstdout_minimum_width_dt_filelast_duration)selfZminimumwidthdtoutr   r   r   __init__R   s    zProgressBar.__init__c                 C  s:   d | _ t | _d| _tj| jd| _d| j_| j	  d S )NT)target)
_stater   _start_time_running	threadingThread_timer_func_timerdaemonstart)r   dskr   r   r   _start^   s    zProgressBar._startc                 C  s   || _ | jd ur| j  d S N)r   r   flush)r   keyr(   stater   r   r   _pretaskg   s    
zProgressBar._pretaskc                 C  sp   d| _ | j  t | j }|| _|| jk r0d S |sB| d| n
| | | j	d url| j	
d | j	  d S )NF   
)r!   r%   joinr   r    r   r   	_draw_bar_update_barr   writer+   )r   r(   r-   Zerroredelapsedr   r   r   _finishl   s    



zProgressBar._finishc                 C  s8   | j r4t | j }|| jkr&| | t| j q dS )z/Background thread for updating the progress barN)r!   r   r    r   r3   timesleepr   )r   r5   r   r   r   r$   {   s
    

zProgressBar._timer_funcc                   sd   | j   s| d| d S t d }t fdddD | }||k r`| |rX|| nd| d S )Nr   finishedc                 3  s   | ]}t  | V  qd S r*   )len).0kr   r   r   	<genexpr>       z*ProgressBar._update_bar.<locals>.<genexpr>)readyZwaitingZrunning)r   r2   r:   sum)r   r5   ZndoneZntasksr   r=   r   r3      s    zProgressBar._update_barc                 C  s   ddl m} dt| j|  }td| }||}d|| j||}tt0 | jd urp| j	| | j
  W d    n1 s0    Y  d S )Nr   )r   #d   z"[{0:<{1}}] | {2}% Completed | {3})
dask.utilsr   intr   format
contextlibsuppress
ValueErrorr   r4   r+   )r   fracr5   r   barpercentmsgr   r   r   r2      s    

zProgressBar._draw_bar)r   r   r   N)__name__
__module____qualname____doc__r   r)   r.   r6   r$   r3   r2   r   r   r   r   r       s   1
	
r   )
__future__r   rG   r   r"   r7   Ztimeitr   Zdask.callbacksr   rD   r   r   r   r   r   r   r   <module>   s   

