From 27e74a15d77739abdfd42f66ff396301549a9a8e Mon Sep 17 00:00:00 2001 From: Dmitri Bogomolov <4glitch@gmail.com> Date: Mon, 29 Jul 2019 15:54:19 +0300 Subject: [PATCH] flake8: network.connectionchooser --- src/network/connectionchooser.py | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/src/network/connectionchooser.py b/src/network/connectionchooser.py index e29185e9..e116ec53 100644 --- a/src/network/connectionchooser.py +++ b/src/network/connectionchooser.py @@ -1,16 +1,16 @@ -from queues import Queue -import random +import random # nosec -from bmconfigparser import BMConfigParser import knownnodes import protocol -from queues import portCheckerQueue import state -import helper_random +from bmconfigparser import BMConfigParser +from debug import logger +from queues import Queue, portCheckerQueue + def getDiscoveredPeer(): try: - peer = helper_random.randomchoice(state.discoveredPeers.keys()) + peer = random.choice(state.discoveredPeers.keys()) except (IndexError, KeyError): raise ValueError try: @@ -19,8 +19,10 @@ def getDiscoveredPeer(): pass return peer + def chooseConnection(stream): - haveOnion = BMConfigParser().safeGet("bitmessagesettings", "socksproxytype")[0:5] == 'SOCKS' + haveOnion = BMConfigParser().safeGet( + "bitmessagesettings", "socksproxytype")[0:5] == 'SOCKS' if state.trustedPeer: return state.trustedPeer try: @@ -30,15 +32,15 @@ def chooseConnection(stream): except Queue.Empty: pass # with a probability of 0.5, connect to a discovered peer - if helper_random.randomchoice((False, True)) and not haveOnion: + if random.choice((False, True)) and not haveOnion: # discovered peers are already filtered by allowed streams return getDiscoveredPeer() for _ in range(50): - peer = helper_random.randomchoice(knownnodes.knownNodes[stream].keys()) + peer = random.choice(knownnodes.knownNodes[stream].keys()) try: - rating = knownnodes.knownNodes[stream][peer]["rating"] + rating = knownnodes.knownNodes[stream][peer]['rating'] except TypeError: - print "Error in %s" % (peer) + logger.warning('Error in %s', peer) rating = 0 if haveOnion: # onion addresses have a higher priority when SOCKS @@ -52,7 +54,7 @@ def chooseConnection(stream): if rating > 1: rating = 1 try: - if 0.05/(1.0-rating) > random.random(): + if 0.05 / (1.0 - rating) > random.random(): return peer except ZeroDivisionError: return peer