#NEWLINE# def fetch_wallet_balances( wallets , fiat , **modes ) : #NEWLINE# #INDENT# "\n    Wallets must be list of two item lists. First item is crypto, second item\n    is the address. example:\n\n    [\n        ['btc', '1PZ3Ps9RvCmUW1s1rHE25FeR8vtKUrhEai'],\n        ['ltc', 'Lb78JDGxMcih1gs3AirMeRW6jaG5V9hwFZ']\n    ]\n    " #NEWLINE# price_fetch = set ( [ x [ 0 ] for x in wallets ] ) #NEWLINE# balances = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# prices = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# fetch_length = ( len ( wallets ) + len ( price_fetch ) ) #NEWLINE# if ( not modes . get ( 'async' , False ) ) : #NEWLINE# #INDENT# for crypto in price_fetch : #NEWLINE# #INDENT# prices [ crypto ] = get_current_price ( crypto , fiat , report_services = True , ** modes ) #NEWLINE# #UNINDENT# for ( crypto , address ) in wallets : #NEWLINE# #INDENT# balances [ address ] = get_address_balance ( crypto , address . strip ( ) , ** modes ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if modes . get ( 'verbose' , False ) : #NEWLINE# #INDENT# print ( 'Need to make' , fetch_length , 'external calls' ) #NEWLINE# #UNINDENT# with futures . ThreadPoolExecutor ( max_workers = int ( ( fetch_length / 2 ) ) ) as executor : #NEWLINE# #INDENT# future_to_key = dict ( ( ( executor . submit ( get_current_price , crypto , fiat , report_services = True , ** modes ) , crypto ) for crypto in price_fetch ) ) #NEWLINE# future_to_key . update ( dict ( ( ( executor . submit ( get_address_balance , crypto , address . strip ( ) , ** modes ) , address ) for ( crypto , address ) in wallets ) ) ) #NEWLINE# ( done , not_done ) = futures . wait ( future_to_key , return_when = futures . FIRST_EXCEPTION ) #NEWLINE# if ( len ( not_done ) > 0 ) : #NEWLINE# #INDENT# raise not_done . pop ( ) . exception ( ) #NEWLINE# #UNINDENT# for future in done : #NEWLINE# #INDENT# key = future_to_key [ future ] #NEWLINE# if ( len ( key ) > 5 ) : #NEWLINE# #INDENT# which = balances #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# which = prices #NEWLINE# #UNINDENT# res = future . result ( ) #NEWLINE# which [ key ] = res #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# ret = [ ] #NEWLINE# for ( crypto , address ) in wallets : #NEWLINE# #INDENT# crypto_value = balances [ address ] #NEWLINE# ( sources , fiat_price ) = prices [ crypto ] #NEWLINE# ret . append ( { #NEWLINE# #INDENT# 'crypto' : crypto , #NEWLINE# 'address' : address , #NEWLINE# 'crypto_value' : crypto_value , #NEWLINE# 'fiat_value' : ( crypto_value * fiat_price ) , #NEWLINE# 'conversion_price' : fiat_price , #NEWLINE# 'price_source' : sources [ 0 ] . name , #NEWLINE# #UNINDENT# } ) #NEWLINE# #UNINDENT# return ret