<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import , unicode_literals <EOL> from itertools import count <EOL> from kombu . five import python_2_unicode_compatible <EOL> from . import symbol_by_name <EOL> __all__ = [ <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> ] <EOL> @ python_2_unicode_compatible <EOL> class FairCycle ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , fun , resources , predicate = Exception ) : <EOL> self . fun = fun <EOL> self . resources = resources <EOL> self . predicate = predicate <EOL> self . pos = <NUM_LIT:0> <EOL> def _next ( self ) : <EOL> while <NUM_LIT:1> : <EOL> try : <EOL> resource = self . resources [ self . pos ] <EOL> self . pos += <NUM_LIT:1> <EOL> return resource <EOL> except IndexError : <EOL> self . pos = <NUM_LIT:0> <EOL> if not self . resources : <EOL> raise self . predicate ( ) <EOL> def get ( self , ** kwargs ) : <EOL> for tried in count ( <NUM_LIT:0> ) : <EOL> resource = self . _next ( ) <EOL> try : <EOL> return self . fun ( resource , ** kwargs ) , resource <EOL> except self . predicate : <EOL> if tried >= len ( self . resources ) - <NUM_LIT:1> : <EOL> raise <EOL> def close ( self ) : <EOL> pass <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' . format ( <EOL> self = self , size = len ( self . resources ) ) <EOL> class round_robin_cycle ( object ) : <EOL> def __init__ ( self , it = None ) : <EOL> self . items = it if it is not None else [ ] <EOL> def update ( self , it ) : <EOL> self . items [ : ] = it <EOL> def consume ( self , n ) : <EOL> return self . items [ : n ] <EOL> def rotate ( self , last_used ) : <EOL> """<STR_LIT>""" <EOL> items = self . items <EOL> try : <EOL> items . append ( items . pop ( items . index ( last_used ) ) ) <EOL> except ValueError : <EOL> pass <EOL> return last_used <EOL> class priority_cycle ( round_robin_cycle ) : <EOL> def rotate ( self , last_used ) : <EOL> pass <EOL> class sorted_cycle ( priority_cycle ) : <EOL> def consume ( self , n ) : <EOL> return sorted ( self . items [ : n ] ) <EOL> CYCLE_ALIASES = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> def cycle_by_name ( name ) : <EOL> return symbol_by_name ( name , CYCLE_ALIASES ) </s>
