<s> import functools <EOL> import logging <EOL> import socket <EOL> import time <EOL> class AutoRetryConfig ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , retry_attempts , retry_wait , retry_backoff ) : <EOL> if retry_backoff <= <NUM_LIT:1> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> self . retry_backoff = retry_backoff <EOL> if retry_wait <= <NUM_LIT:0> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> self . retry_wait = retry_wait <EOL> if retry_attempts < <NUM_LIT:0> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> self . retry_attempts = retry_attempts <EOL> class AutoRetryCandidate ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , retry_config , logger = None , * args , ** kwargs ) : <EOL> self . _retry_config = retry_config <EOL> if not logger : <EOL> logger = logging . getLogger ( self . __class__ . __module__ ) <EOL> self . _retry_logger = logger <EOL> super ( AutoRetryCandidate , self ) . __init__ ( * args , ** kwargs ) <EOL> def auto_retry ( fun ) : <EOL> """<STR_LIT>""" <EOL> @ functools . wraps ( fun ) <EOL> def decorated ( instance , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> cfg = instance . _retry_config <EOL> remaining_tries = cfg . retry_attempts <EOL> current_wait = cfg . retry_wait <EOL> retry_backoff = cfg . retry_backoff <EOL> last_error = None <EOL> while remaining_tries >= <NUM_LIT:0> : <EOL> try : <EOL> return fun ( instance , * args , ** kwargs ) <EOL> except socket . error as e : <EOL> last_error = e <EOL> instance . _retry_logger . warning ( '<STR_LIT>' , e ) <EOL> remaining_tries -= <NUM_LIT:1> <EOL> if remaining_tries == <NUM_LIT:0> : <EOL> break <EOL> time . sleep ( current_wait ) <EOL> current_wait *= retry_backoff <EOL> raise last_error <EOL> return decorated <EOL> def extract_pattern ( fmt ) : <EOL> """<STR_LIT>""" <EOL> class FakeDict ( object ) : <EOL> def __init__ ( self ) : <EOL> self . seen_keys = set ( ) <EOL> def __getitem__ ( self , key ) : <EOL> self . seen_keys . add ( key ) <EOL> return '<STR_LIT>' <EOL> def keys ( self ) : <EOL> return self . seen_keys <EOL> fake = FakeDict ( ) <EOL> try : <EOL> fmt % fake <EOL> except TypeError : <EOL> pass <EOL> return list ( fake . keys ( ) ) </s>
