B
     9-et+                 @   sp   d dl Z d dlZd dlZd dlZd dlZdZdd Zdd ZG dd deZ	G d	d
 d
eZ
G dd deZdS )    Ni?c                s    fdd}|S )Nc                s
   t |  S )N)
isinstance)	exception)retryable_types G:\My Drive\STUDY\EPAT\09 TBP - Trading & Back-testing Platforms\TBP04 - Backtesting & Live Trading\IB IBridgePy API\02 Python 3.7 IBridgePy_Win_Anaconda37_64\BasicPyLib\retrying.py_retry_if_exception_these_types   s    zD_retry_if_exception_of_type.<locals>._retry_if_exception_these_typesr   )r   r   r   )r   r   _retry_if_exception_of_type   s    r   c                 sB   t  dkr,t d r,dd }| d S  fdd}|S dS )z
    Decorator function that instantiates the Retrying object
    @param dargs: positional arguments passed to Retrying object
    @param dkw: keyword arguments passed to the Retrying object
       r   c                s   t   fdd}|S )Nc                 s   t  j f| |S )N)Retryingcall)argskw)fr   r   	wrapped_f,   s    z-retry.<locals>.wrap_simple.<locals>.wrapped_f)sixwraps)r   r   r   )r   r   wrap_simple*   s    zretry.<locals>.wrap_simplec                s   t   fdd}|S )Nc                 s   t  jf| |S )N)r
   r   )r   r   )dargsdkwr   r   r   r   7   s    z&retry.<locals>.wrap.<locals>.wrapped_f)r   r   )r   r   )r   r   )r   r   wrap5   s    zretry.<locals>.wrapN)lencallable)r   r   r   r   r   )r   r   r   retry"   s
    r   c               @   sz   e Zd ZdddZdd Zdd Zed	d
 Zdd Zdd Z	dd Z
dd Zedd Zedd Zdd Zdd ZdS )r
   NFc                s@  |d krdn|| _ |d krdn|| _|d kr0dn|| _|d krBdn|| _|d krTdn|| _|d krfdn|| _|	d krxdn|	| _|d krdn|| _|d krtn|| _	|
d krtn|
| _
|d krdn|| _|| _|| _g  |d k	r | j |d k	r | j |d k	r|| _n&|d kr* fdd| _nt| || _dd g|d k	rV| j |d k	sj|d k	rv| j |d k	s|	d k	r| j |d k	s|d k	r| j |d k	r|| _n&|d krfd	d| _nt| || _|d kr| j| _nt|trt|}|| _|d kr0| j| _n|| _|| _d S )
N   d   i  r   r	   c                s   t  fddD S )Nc             3   s   | ]}| V  qd S )Nr   ).0r   )attemptsdelayr   r   	<genexpr>o   s    z6Retrying.__init__.<locals>.<lambda>.<locals>.<genexpr>)any)r   r   )
stop_funcs)r   r   r   <lambda>o       z#Retrying.__init__.<locals>.<lambda>c              _   s   dS )Nr   r   )r   kwargsr   r   r   r!   v   r"   c                s   t  fddD S )Nc             3   s   | ]}| V  qd S )Nr   )r   r   )r   r   r   r   r      s    z6Retrying.__init__.<locals>.<lambda>.<locals>.<genexpr>)max)r   r   )
wait_funcs)r   r   r   r!      r"   ) _stop_max_attempt_number_stop_max_delay_wait_fixed_wait_random_min_wait_random_max_wait_incrementing_start_wait_incrementing_increment_wait_exponential_multiplierMAX_WAIT_wait_exponential_max_wait_incrementing_max_wait_jitter_max_before_attempts_after_attemptsappendstop_after_attemptstop_after_delaystopgetattrfixed_sleeprandom_sleepincrementing_sleepexponential_sleepwaitalways_reject_retry_on_exceptionr   tupler   never_reject_retry_on_result_wrap_exception)selfr7   r=   stop_max_attempt_numberZstop_max_delay
wait_fixedZwait_random_minZwait_random_maxZwait_incrementing_startZwait_incrementing_incrementZwait_incrementing_maxZwait_exponential_multiplierZwait_exponential_maxZretry_on_exceptionZretry_on_resultwrap_exceptionZ	stop_funcZ	wait_funcZwait_jitter_maxZbefore_attemptsZafter_attemptsr   )r    r%   r   __init__B   s^    









zRetrying.__init__c             C   s
   || j kS )z;Stop after the previous attempt >= stop_max_attempt_number.)r&   )rD   previous_attempt_numberdelay_since_first_attempt_msr   r   r   r5      s    zRetrying.stop_after_attemptc             C   s
   || j kS )z=Stop after the time from the first attempt >= stop_max_delay.)r'   )rD   rI   rJ   r   r   r   r6      s    zRetrying.stop_after_delayc             C   s   dS )z#Don't sleep at all before retrying.r   r   )rI   rJ   r   r   r   no_sleep   s    zRetrying.no_sleepc             C   s   | j S )z0Sleep a fixed amount of time between each retry.)r(   )rD   rI   rJ   r   r   r   r9      s    zRetrying.fixed_sleepc             C   s   t | j| jS )zISleep a random amount of time between wait_random_min and wait_random_max)randomrandintr)   r*   )rD   rI   rJ   r   r   r   r:      s    zRetrying.random_sleepc             C   s4   | j | j|d   }|| jkr$| j}|dk r0d}|S )z
        Sleep an incremental amount of time after each attempt, starting at
        wait_incrementing_start and incrementing by wait_incrementing_increment
        r	   r   )r+   r,   r0   )rD   rI   rJ   resultr   r   r   r;      s    
zRetrying.incrementing_sleepc             C   s2   d| }| j | }|| jkr"| j}|dk r.d}|S )N   r   )r-   r/   )rD   rI   rJ   exprN   r   r   r   r<      s    

zRetrying.exponential_sleepc             C   s   dS )NFr   )rN   r   r   r   rA      s    zRetrying.never_rejectc             C   s   dS )NTr   )rN   r   r   r   r>      s    zRetrying.always_rejectc             C   s4   d}|j r || |jd O }n|| |jO }|S )NFr	   )has_exceptionr?   valuerB   )rD   attemptZrejectr   r   r   should_reject   s
    zRetrying.should_rejectc             O   s  t tt d }d}x| jr*| | yt||||d}W n    t }t||d}Y nX | |sx|| j	S | j
r| 
| t tt d | }| ||r| j	s|jr| nt|n<| ||}	| jrt | j }
|	td|
 }	t|	d  |d7 }qW d S )Ni  r	   FTr   g     @@)introundtimer2   Attemptsysexc_inforT   getrC   r3   r7   rQ   
RetryErrorr=   r1   rL   r$   sleep)rD   fnr   r#   
start_timeattempt_numberrS   tbrJ   r]   jitterr   r   r   r      s2    




zRetrying.call)NNNNNNNNNNNNNNFNNNNN)__name__
__module____qualname__rH   r5   r6   staticmethodrK   r9   r:   r;   r<   rA   r>   rT   r   r   r   r   r   r
   @   s6                  
N		r
   c               @   s*   e Zd ZdZdd Zd
ddZdd Zd	S )rX   z
    An Attempt encapsulates a call to a target function that may end as a
    normal return value from the function or an Exception depending on what
    occurred during the execution.
    c             C   s   || _ || _|| _d S )N)rR   r`   rQ   )rD   rR   r`   rQ   r   r   r   rH     s    zAttempt.__init__Fc             C   s@   | j r6|rt| q<t| jd | jd | jd  n| jS dS )z
        Return the return value of this Attempt instance or raise an Exception.
        If wrap_exception is true, this Attempt is wrapped inside of a
        RetryError before being raised.
        r   r	   rO   N)rQ   r\   r   reraiserR   )rD   rG   r   r   r   r[     s
    
"zAttempt.getc          	   C   s:   | j r&d| jdt| jd S d| j| jS d S )NzAttempts: {0}, Error:
{1} rO   zAttempts: {0}, Value: {1})rQ   formatr`   join	traceback	format_tbrR   )rD   r   r   r   __repr__  s     zAttempt.__repr__N)F)rc   rd   re   __doc__rH   r[   rm   r   r   r   r   rX      s   
rX   c               @   s    e Zd ZdZdd Zdd ZdS )r\   zU
    A RetryError encapsulates the last Attempt instance right before giving up.
    c             C   s
   || _ d S )N)last_attempt)rD   ro   r   r   r   rH   %  s    zRetryError.__init__c             C   s   d | jS )NzRetryError[{0}])ri   ro   )rD   r   r   r   __str__(  s    zRetryError.__str__N)rc   rd   re   rn   rH   rp   r   r   r   r   r\      s   r\   )rL   r   rY   rW   rk   r.   r   r   objectr
   rX   	Exceptionr\   r   r   r   r   <module>   s    @!