Replace (check and) del pattern with pop method

This commit is contained in:
NIC619
2019-11-20 23:06:37 +08:00
parent 74198c70b1
commit 19907e18ec
8 changed files with 20 additions and 34 deletions

View File

@ -144,8 +144,7 @@ class GossipSub(IPubsubRouter):
elif peer_id in self.peers_floodsub:
self.peers_floodsub.remove(peer_id)
if peer_id in self.peers_to_protocol:
del self.peers_to_protocol[peer_id]
self.peers_to_protocol.pop(peer_id, None)
async def handle_rpc(self, rpc: rpc_pb2.RPC, sender_peer_id: ID) -> None:
"""
@ -274,8 +273,7 @@ class GossipSub(IPubsubRouter):
self.mesh[topic].append(peer)
await self.emit_graft(topic, peer)
if topic_in_fanout:
del self.fanout[topic]
self.fanout.pop(topic, None)
async def leave(self, topic: str) -> None:
# Note: the comments here are the near-exact algorithm description from the spec
@ -294,7 +292,7 @@ class GossipSub(IPubsubRouter):
await self.emit_prune(topic, peer)
# Forget mesh[topic]
del self.mesh[topic]
self.mesh.pop(topic, None)
# Heartbeat
async def heartbeat(self) -> None:
@ -355,8 +353,8 @@ class GossipSub(IPubsubRouter):
# TODO: there's no way time_since_last_publish gets set anywhere yet
if self.time_since_last_publish[topic] > self.time_to_live:
# Remove topic from fanout
del self.fanout[topic]
del self.time_since_last_publish[topic]
self.fanout.pop(topic, None)
self.time_since_last_publish.pop(topic, None)
else:
num_fanout_peers_in_topic = len(self.fanout[topic])

View File

@ -96,8 +96,7 @@ class MessageCache:
last_entries: List[CacheEntry] = self.history[len(self.history) - 1]
for entry in last_entries:
if entry.mid in self.msgs:
del self.msgs[entry.mid]
self.msgs.pop(entry.mid)
i: int = len(self.history) - 2

View File

@ -232,8 +232,7 @@ class Pubsub:
:param topic: the topic to remove validator from
"""
if topic in self.topic_validators:
del self.topic_validators[topic]
self.topic_validators.pop(topic, None)
def get_msg_validators(self, msg: rpc_pb2.Message) -> Tuple[TopicValidator, ...]:
"""
@ -283,7 +282,7 @@ class Pubsub:
await stream.write(encode_varint_prefixed(hello.SerializeToString()))
except StreamClosed:
logger.debug("Fail to add new peer %s: stream closed", peer_id)
del self.peers[peer_id]
self.peers.pop(peer_id, None)
return
# TODO: Check EOF of this stream.
# TODO: Check if the peer in black list.
@ -291,7 +290,7 @@ class Pubsub:
self.router.add_peer(peer_id, stream.get_protocol())
except Exception as error:
logger.debug("fail to add new peer %s, error %s", peer_id, error)
del self.peers[peer_id]
self.peers.pop(peer_id, None)
return
logger.debug("added new peer %s", peer_id)
@ -299,7 +298,7 @@ class Pubsub:
def _handle_dead_peer(self, peer_id: ID) -> None:
if peer_id not in self.peers:
return
del self.peers[peer_id]
self.peers.pop(peer_id, None)
for topic in self.peer_topics:
if peer_id in self.peer_topics[topic]:
@ -411,7 +410,7 @@ class Pubsub:
if topic_id not in self.my_topics:
return
# Remove topic_id from map if present
del self.my_topics[topic_id]
self.my_topics.pop(topic_id, None)
# Create unsubscribe message
packet: rpc_pb2.RPC = rpc_pb2.RPC()