o
    Nrf                     @  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  s`   t | d\}}t |d\}}|r|dd|dd|ddS |r*|dd|ddS |d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s)divmod)tmr   h r   b/var/www/html/software/conda/envs/catlas/lib/python3.10/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)self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 d S N)r    r   flush)r   keyr)   stater   r   r   _pretaskg   s   
zProgressBar._pretaskc                 C  st   d| _ | j  t | j }|| _|| jk rd S |s!| d| n| | | j	d ur8| j	
d | j	  d S 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  s@   | j rt | j }|| jkr| | t| j | j sdS dS )z/Background thread for updating the progress barN)r"   r   r!   r   r4   timesleepr   )r   r6   r   r   r   r%   {   s   

zProgressBar._timer_funcc                   sh   | j   s| d| d S t d }t fdddD | }||k r2| |r,|| nd| d S d S )Nr   finishedc                 3  s    | ]	}t  | V  qd S r+   )len).0kr   r   r   	<genexpr>   s    z*ProgressBar._update_bar.<locals>.<genexpr>)readyZwaitingrunning)r    r3   r;   sum)r   r6   ZndoneZntasksr   r>   r   r4      s   zProgressBar._update_barc                 C  s   ddl m} dt| j|  }td| }||}d|| j||}tt! | jd ur@| j	| | j
  W d    d S W d    d S 1 sKw   Y  d S )Nr   )r   #d   z"[{0:<{1}}] | {2}% Completed | {3})
dask.utilsr   intr   format
contextlibsuppress
ValueErrorr   r5   r,   )r   fracr6   r   barpercentmsgr   r   r   r3      s   

"zProgressBar._draw_bar)r   r   r   N)__name__
__module____qualname____doc__r   r*   r/   r7   r%   r4   r3   r   r   r   r   r       s    
1	
r   )
__future__r   rH   r   r#   r8   Ztimeitr   Zdask.callbacksr   rE   r   r   r   r   r   r   r   <module>   s    

