#NEWLINE# def enforce_service_mode( services , FetcherClass , kwargs , modes ) : #NEWLINE# #INDENT# "\n    Fetches the value according to the mode of execution desired.\n    `FetcherClass` must be a class that is subclassed from AutoFallbackFetcher.\n    `services` must be a list of Service classes.\n    `kwargs` is a list of arguments used to make the service call, usually\n      something like {crypto: 'btc', address: '1HwY...'} or\n      {crypto: 'ltc', fiat: 'rur'}, (depends on the what FetcherClass.action takes)\n\n    Modes can be:\n\n         average = positive int. 1 by default. Takes average of n fetches.\n         verbose = [True|False] False by default. Extra output.\n         random = [True|False] False by default. Randomizes service order.\n         paranoid = positive int. 1 by default. Redundant Fetching.\n         fast = positive int. 0 by default. Return as soon as recieved first n results.\n\n    " #NEWLINE# fast_level = modes . get ( 'fast' , 0 ) #NEWLINE# average_level = modes . get ( 'average' , 0 ) #NEWLINE# paranoid_level = modes . get ( 'paranoid' , 0 ) #NEWLINE# private_level = modes . get ( 'private' , 0 ) #NEWLINE# verbose = modes . get ( 'verbose' , False ) #NEWLINE# timeout = modes . get ( 'timeout' , None ) #NEWLINE# if modes . get ( 'random' , False ) : #NEWLINE# #INDENT# random . shuffle ( services ) #NEWLINE# #UNINDENT# if ( private_level > 0 ) : #NEWLINE# #INDENT# results = _do_private_mode ( FetcherClass , services , kwargs , random_wait_seconds = private_level , verbose = verbose , timeout = timeout ) #NEWLINE# return results #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( ( average_level <= 1 ) and ( paranoid_level <= 1 ) and ( fast_level == 0 ) ) : #NEWLINE# #INDENT# fetcher = FetcherClass ( services = services , verbose = verbose , timeout = timeout ) #NEWLINE# consensus_results = fetcher . action ( ** kwargs ) #NEWLINE# used_services = [ fetcher . _successful_service ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( average_level > 1 ) : #NEWLINE# #INDENT# results = _get_results ( FetcherClass , services , kwargs , num_results = average_level , verbose = verbose , timeout = timeout ) #NEWLINE# ( to_compare , used_services ) = _prepare_consensus ( FetcherClass , results ) #NEWLINE# consensus_results = ( sum ( to_compare ) / len ( to_compare ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( paranoid_level > 1 ) : #NEWLINE# #INDENT# results = _get_results ( FetcherClass , services , kwargs , num_results = paranoid_level , verbose = verbose , timeout = timeout ) #NEWLINE# ( to_compare , used_services ) = _prepare_consensus ( FetcherClass , results ) #NEWLINE# if ( not ( len ( set ( to_compare ) ) == 1 ) ) : #NEWLINE# #INDENT# ServiceDisagreement ( ( 'Differing values returned: %s' % results ) ) #NEWLINE# #UNINDENT# consensus_results = results [ 0 ] [ 1 ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( fast_level >= 1 ) : #NEWLINE# #INDENT# raise Exception ( 'Fast mode not yet working' ) #NEWLINE# results = _get_results ( FetcherClass , services , kwargs , fast = fast_level , verbose = verbose ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# raise Exception ( 'No mode specified.' ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# if modes . get ( 'report_services' ) : #NEWLINE# #INDENT# return ( used_services , consensus_results ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# return consensus_results