From ca42b4be63b0c2b0edba98962cc95b3150fee1aa Mon Sep 17 00:00:00 2001 From: Dmitri Bogomolov <4glitch@gmail.com> Date: Thu, 3 May 2018 13:05:49 +0300 Subject: [PATCH] flake8 for knownnodes --- src/knownnodes.py | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/src/knownnodes.py b/src/knownnodes.py index aa080128..71b74771 100644 --- a/src/knownnodes.py +++ b/src/knownnodes.py @@ -1,9 +1,9 @@ -import pickle import os +import pickle import threading -from bmconfigparser import BMConfigParser import state +from bmconfigparser import BMConfigParser knownNodesLock = threading.Lock() knownNodes = {} @@ -13,37 +13,51 @@ knownNodesTrimAmount = 2000 # forget a node after rating is this low knownNodesForgetRating = -0.5 -def saveKnownNodes(dirName = None): + +def saveKnownNodes(dirName=None): if dirName is None: dirName = state.appdata with knownNodesLock: with open(os.path.join(dirName, 'knownnodes.dat'), 'wb') as output: pickle.dump(knownNodes, output) + def increaseRating(peer): increaseAmount = 0.1 maxRating = 1 with knownNodesLock: for stream in knownNodes.keys(): try: - knownNodes[stream][peer]["rating"] = min(knownNodes[stream][peer]["rating"] + increaseAmount, maxRating) + knownNodes[stream][peer]["rating"] = min( + knownNodes[stream][peer]["rating"] + increaseAmount, + maxRating + ) except KeyError: pass + def decreaseRating(peer): decreaseAmount = 0.1 minRating = -1 with knownNodesLock: for stream in knownNodes.keys(): try: - knownNodes[stream][peer]["rating"] = max(knownNodes[stream][peer]["rating"] - decreaseAmount, minRating) + knownNodes[stream][peer]["rating"] = max( + knownNodes[stream][peer]["rating"] - decreaseAmount, + minRating + ) except KeyError: pass -def trimKnownNodes(recAddrStream = 1): - if len(knownNodes[recAddrStream]) < int(BMConfigParser().get("knownnodes", "maxnodes")): + +def trimKnownNodes(recAddrStream=1): + if len(knownNodes[recAddrStream]) < \ + BMConfigParser().safeGetInt("knownnodes", "maxnodes"): return with knownNodesLock: - oldestList = sorted(knownNodes[recAddrStream], key=lambda x: x['lastseen'])[:knownNodesTrimAmount] + oldestList = sorted( + knownNodes[recAddrStream], + key=lambda x: x['lastseen'] + )[:knownNodesTrimAmount] for oldest in oldestList: del knownNodes[recAddrStream][oldest]