From 46d56c703e489a00f9c3856407c470b26920462e Mon Sep 17 00:00:00 2001 From: Kashiko Koibumi Date: Sat, 25 May 2024 11:39:46 +0900 Subject: [PATCH] use six.itervalues(), six.iteritems() --- src/bitmessageqt/__init__.py | 15 ++++++++------- src/bitmessageqt/retranslateui.py | 3 ++- src/network/dandelion.py | 19 ++++++++++--------- src/network/downloadthread.py | 3 ++- src/network/knownnodes.py | 7 ++++--- src/network/objectracker.py | 3 ++- src/network/tcp.py | 3 ++- src/tests/core.py | 13 +++++++------ 8 files changed, 37 insertions(+), 29 deletions(-) diff --git a/src/bitmessageqt/__init__.py b/src/bitmessageqt/__init__.py index 40113b5a..b4ce8678 100644 --- a/src/bitmessageqt/__init__.py +++ b/src/bitmessageqt/__init__.py @@ -14,6 +14,7 @@ import threading import time from datetime import datetime, timedelta from sqlite3 import register_adapter +import six from PyQt4 import QtCore, QtGui from PyQt4.QtNetwork import QLocalSocket, QLocalServer @@ -468,7 +469,7 @@ class MyForm(settingsmixin.SMainWindow): # add missing folders if len(db[toAddress]) > 0: j = 0 - for f, c in db[toAddress].iteritems(): + for f, c in six.iteritems(db[toAddress]): try: subwidget = Ui_FolderWidget(widget, j, toAddress, f, c['count']) except KeyError: @@ -598,7 +599,7 @@ class MyForm(settingsmixin.SMainWindow): # add missing folders if len(db[toAddress]) > 0: j = 0 - for f, c in db[toAddress].iteritems(): + for f, c in six.iteritems(db[toAddress]): if toAddress is not None and tab == 'messages' and folder == "new": continue subwidget = Ui_FolderWidget(widget, j, toAddress, f, c) @@ -1075,15 +1076,15 @@ class MyForm(settingsmixin.SMainWindow): for i in range(root.childCount()): addressItem = root.child(i) if addressItem.type == AccountMixin.ALL: - newCount = sum(totalUnread.itervalues()) + newCount = sum(six.itervalues(totalUnread)) self.drawTrayIcon(self.currentTrayIconFileName, newCount) else: try: - newCount = sum(( + newCount = sum(six.itervalues(( broadcastsUnread if addressItem.type == AccountMixin.SUBSCRIPTION else normalUnread - )[addressItem.address].itervalues()) + )[addressItem.address])) except KeyError: newCount = 0 if newCount != addressItem.unreadCount: @@ -2871,7 +2872,7 @@ class MyForm(settingsmixin.SMainWindow): QtCore.QEventLoop.AllEvents, 1000 ) self.saveSettings() - for attr, obj in self.ui.__dict__.iteritems(): + for attr, obj in six.iteritems(self.ui.__dict__): if hasattr(obj, "__class__") \ and isinstance(obj, settingsmixin.SettingsMixin): saveMethod = getattr(obj, "saveSettings", None) @@ -4209,7 +4210,7 @@ class MyForm(settingsmixin.SMainWindow): def initSettings(self): self.loadSettings() - for attr, obj in self.ui.__dict__.iteritems(): + for attr, obj in six.iteritems(self.ui.__dict__): if hasattr(obj, "__class__") and \ isinstance(obj, settingsmixin.SettingsMixin): loadMethod = getattr(obj, "loadSettings", None) diff --git a/src/bitmessageqt/retranslateui.py b/src/bitmessageqt/retranslateui.py index c7676f77..1b4b7716 100644 --- a/src/bitmessageqt/retranslateui.py +++ b/src/bitmessageqt/retranslateui.py @@ -1,4 +1,5 @@ from os import path +import six from PyQt4 import QtGui from debug import logger import widgets @@ -7,7 +8,7 @@ class RetranslateMixin(object): def retranslateUi(self): defaults = QtGui.QWidget() widgets.load(self.__class__.__name__.lower() + '.ui', defaults) - for attr, value in defaults.__dict__.iteritems(): + for attr, value in six.iteritems(defaults.__dict__): setTextMethod = getattr(value, "setText", None) if callable(setTextMethod): getattr(self, attr).setText(getattr(defaults, attr).text()) diff --git a/src/network/dandelion.py b/src/network/dandelion.py index 35e70c95..e2232082 100644 --- a/src/network/dandelion.py +++ b/src/network/dandelion.py @@ -6,6 +6,7 @@ from collections import namedtuple from random import choice, expovariate, sample from threading import RLock from time import time +import six import connectionpool import state @@ -99,12 +100,12 @@ class Dandelion: # pylint: disable=old-style-class with self.lock: if len(self.stem) < MAX_STEMS: self.stem.append(connection) - for k in (k for k, v in self.nodeMap.iteritems() if v is None): + for k in (k for k, v in six.iteritems(self.nodeMap) if v is None): self.nodeMap[k] = connection - for k, v in { - k: v for k, v in self.hashMap.iteritems() + for k, v in six.iteritems({ + k: v for k, v in six.iteritems(self.hashMap) if v.child is None - }.iteritems(): + }): self.hashMap[k] = Stem( connection, v.stream, self.poissonTimeout()) invQueue.put((v.stream, k, v.child)) @@ -120,14 +121,14 @@ class Dandelion: # pylint: disable=old-style-class self.stem.remove(connection) # active mappings to pointing to the removed node for k in ( - k for k, v in self.nodeMap.iteritems() + k for k, v in six.iteritems(self.nodeMap) if v == connection ): self.nodeMap[k] = None - for k, v in { - k: v for k, v in self.hashMap.iteritems() + for k, v in six.iteritems({ + k: v for k, v in six.iteritems(self.hashMap) if v.child == connection - }.iteritems(): + }): self.hashMap[k] = Stem( None, v.stream, self.poissonTimeout()) @@ -168,7 +169,7 @@ class Dandelion: # pylint: disable=old-style-class with self.lock: deadline = time() toDelete = [ - [v.stream, k, v.child] for k, v in self.hashMap.iteritems() + [v.stream, k, v.child] for k, v in six.iteritems(self.hashMap) if v.timeout < deadline ] diff --git a/src/network/downloadthread.py b/src/network/downloadthread.py index 4f108c72..15013a3b 100644 --- a/src/network/downloadthread.py +++ b/src/network/downloadthread.py @@ -3,6 +3,7 @@ """ import time import state +import six import addresses import helper_random import protocol @@ -28,7 +29,7 @@ class DownloadThread(StoppableThread): deadline = time.time() - self.requestExpires try: toDelete = [ - k for k, v in missingObjects.iteritems() + k for k, v in six.iteritems(missingObjects) if v < deadline] except RuntimeError: pass diff --git a/src/network/knownnodes.py b/src/network/knownnodes.py index c53be2cd..f0fd6d90 100644 --- a/src/network/knownnodes.py +++ b/src/network/knownnodes.py @@ -14,6 +14,7 @@ try: from collections.abc import Iterable except ImportError: from collections import Iterable +import six import state from bmconfigparser import config @@ -54,8 +55,8 @@ def json_serialize_knownnodes(output): Reorganize knownnodes dict and write it as JSON to output """ _serialized = [] - for stream, peers in knownNodes.iteritems(): - for peer, info in peers.iteritems(): + for stream, peers in six.iteritems(knownNodes): + for peer, info in six.iteritems(peers): info.update(rating=round(info.get('rating', 0), 2)) _serialized.append({ 'stream': stream, 'peer': peer._asdict(), 'info': info @@ -87,7 +88,7 @@ def pickle_deserialize_old_knownnodes(source): global knownNodes knownNodes = pickle.load(source) # nosec B301 for stream in knownNodes.keys(): - for node, params in knownNodes[stream].iteritems(): + for node, params in six.iteritems(knownNodes[stream]): if isinstance(params, (float, int)): addKnownNode(stream, node, params) diff --git a/src/network/objectracker.py b/src/network/objectracker.py index a458e5d2..f1cfa131 100644 --- a/src/network/objectracker.py +++ b/src/network/objectracker.py @@ -3,6 +3,7 @@ Module for tracking objects """ import time from threading import RLock +import six import state import connectionpool @@ -75,7 +76,7 @@ class ObjectTracker(object): with self.objectsNewToThemLock: self.objectsNewToThem = { k: v - for k, v in self.objectsNewToThem.iteritems() + for k, v in six.iteritems(self.objectsNewToThem) if v >= deadline} self.lastCleaned = time.time() diff --git a/src/network/tcp.py b/src/network/tcp.py index 139715a6..a31544fd 100644 --- a/src/network/tcp.py +++ b/src/network/tcp.py @@ -8,6 +8,7 @@ import math import random import socket import time +import six # magic imports! import addresses @@ -191,7 +192,7 @@ class TCPConnection(BMProto, TLSDispatcher): # only if more recent than 3 hours # and having positive or neutral rating filtered = [ - (k, v) for k, v in nodes.iteritems() + (k, v) for k, v in six.iteritems(nodes) if v["lastseen"] > int(time.time()) - maximumAgeOfNodesThatIAdvertiseToOthers and v["rating"] >= 0 and not k.host.endswith('.onion') diff --git a/src/tests/core.py b/src/tests/core.py index f1a11a06..e24293b0 100644 --- a/src/tests/core.py +++ b/src/tests/core.py @@ -15,6 +15,7 @@ import sys import threading import time import unittest +import six import protocol import state @@ -137,8 +138,8 @@ class TestCore(unittest.TestCase): @staticmethod def _outdate_knownnodes(): with knownnodes.knownNodesLock: - for nodes in knownnodes.knownNodes.itervalues(): - for node in nodes.itervalues(): + for nodes in six.itervalues(knownnodes.knownNodes): + for node in six.itervalues(nodes): node['lastseen'] -= 2419205 # older than 28 days def test_knownnodes_pickle(self): @@ -146,9 +147,9 @@ class TestCore(unittest.TestCase): pickle_knownnodes() self._wipe_knownnodes() knownnodes.readKnownNodes() - for nodes in knownnodes.knownNodes.itervalues(): + for nodes in six.itervalues(knownnodes.knownNodes): self_count = n = 0 - for n, node in enumerate(nodes.itervalues()): + for n, node in enumerate(six.itervalues(nodes)): if node.get('self'): self_count += 1 self.assertEqual(n - self_count, 2) @@ -202,7 +203,7 @@ class TestCore(unittest.TestCase): while c > 0: time.sleep(1) c -= 2 - for peer, con in connectionpool.pool.outboundConnections.iteritems(): + for peer, con in six.iteritems(connectionpool.pool.outboundConnections): if ( peer.host.startswith('bootstrap') or peer.host == 'quzwelsuziwqgpt2.onion' @@ -223,7 +224,7 @@ class TestCore(unittest.TestCase): 'Failed to connect during %.2f sec' % (time.time() - _started)) def _check_knownnodes(self): - for stream in knownnodes.knownNodes.itervalues(): + for stream in six.itervalues(knownnodes.knownNodes): for peer in stream: if peer.host.startswith('bootstrap'): self.fail(