B
    ÛT•\Ã  ã               @   sl   d dl mZmZm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 dd„ ZG d	d
„ d
e
ƒZdS )é    )Úabsolute_importÚdivisionÚprint_functionN)Údefault_timeré   )ÚCallback)Úignoringc             C   sL   t | dƒ\}}t |dƒ\}}|r.d |||¡S |r>d ||¡S d |¡S dS )z„Format seconds into a human readable form.

    >>> format_time(10.4)
    '10.4s'
    >>> format_time(1000.4)
    '16min 40.4s'
    é<   z {0:2.0f}hr {1:2.0f}min {2:4.1f}sz{0:2.0f}min {1:4.1f}sz	{0:4.1f}sN)ÚdivmodÚformat)ÚtÚmÚsÚh© r   ú8lib/python3.7/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

    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()
    >>> 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 krt j}|| _|| _|| _|| _d| _d S )Nr   )ÚsysÚstdoutÚ_minimumÚ_widthÚ_dtÚ_fileÚlast_duration)ÚselfZminimumÚwidthZdtÚoutr   r   r   Ú__init__L   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Ú	threadingZThreadÚ_timer_funcÚ_timerZdaemonÚstart)r   Údskr   r   r   Ú_startU   s    zProgressBar._startc             C   s   || _ | j ¡  d S )N)r"   r   Úflush)r   Úkeyr)   Ústater   r   r   Ú_pretask^   s    zProgressBar._pretaskc             C   sf   d| _ | j ¡  tƒ | j }|| _|| jk r0d S |sB|  d|¡ n
|  |¡ | 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   Ú_finishb   s    


zProgressBar._finishc             C   s<   x6| j r6tƒ | j }|| jkr(|  |¡ t | j¡ qW dS )z/Background thread for updating the progress barN)r$   r   r#   r   r3   ÚtimeZsleepr   )r   r5   r   r   r   r&   p   s
    

zProgressBar._timer_funcc                s\   | j ‰ ˆ s|  d|¡ d S tˆ d ƒ}t‡ fdd„dD ƒƒ| }|  |rP|| nd|¡ d S )Nr   Zfinishedc             3   s   | ]}t ˆ | ƒV  qd S )N)Úlen)Ú.0Úk)r   r   r   ú	<genexpr>~   s    z*ProgressBar._update_bar.<locals>.<genexpr>)ZreadyZwaitingZrunning)r"   r2   r8   Úsum)r   r5   ZndoneZntasksr   )r   r   r3   x   s    zProgressBar._update_barc          	   C   sf   dt | j| ƒ }t d| ƒ}t|ƒ}d || j||¡}ttƒ | j |¡ | j ¡  W d Q R X d S )Nú#éd   z"[{0:<{1}}] | {2}% Completed | {3})	Úintr   r   r   r   Ú
ValueErrorr   r4   r+   )r   Zfracr5   ZbarZ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   ,
			r   )Z
__future__r   r   r   r   r%   r7   Ztimeitr   Z	callbacksr   Zutilsr   r   r   r   r   r   r   Ú<module>   s   