Skip to content

Commit

Permalink
Use normal dict instead of defaultdict
Browse files Browse the repository at this point in the history
After working with this a bit, the defaultdict seemed to make things
less clear than a using a normal dict with `get` and `setdefault`.
  • Loading branch information
karlb committed Nov 5, 2020
1 parent 6cd56d3 commit 97a68f5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 15 deletions.
6 changes: 3 additions & 3 deletions raiden/blockchain/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,9 @@ def new_filters_from_events(events: List[DecodedEvent]) -> RaidenContractFilter:
)
for entry in events:
if entry.event_data["event"] == ChannelEvent.OPENED:
new_filter.channels_of_token_network[
TokenNetworkAddress(entry.originating_contract)
].add(entry.event_data["args"]["channel_identifier"])
new_filter.channels_of_token_network.setdefault(
TokenNetworkAddress(entry.originating_contract), set()
).add(entry.event_data["args"]["channel_identifier"])
# Now that we have a channel, we need to watch for registered secrets.
new_filter.ignore_secret_registry_until_channel_found = False

Expand Down
21 changes: 9 additions & 12 deletions raiden/blockchain/filters.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from collections import defaultdict
from dataclasses import dataclass, field
from typing import Dict, List, Optional, Set, cast

Expand Down Expand Up @@ -122,7 +121,7 @@ class RaidenContractFilter:
token_network_registry_addresses: Set[TokenNetworkRegistryAddress] = field(default_factory=set)
token_network_addresses: Set[TokenNetworkAddress] = field(default_factory=set)
channels_of_token_network: Dict[TokenNetworkAddress, Set[ChannelID]] = field(
default_factory=lambda: defaultdict(set)
default_factory=dict
)
secret_registry_address: Optional[SecretRegistryAddress] = None
ignore_secret_registry_until_channel_found: bool = False
Expand Down Expand Up @@ -262,16 +261,14 @@ def union(self, other: "RaidenContractFilter") -> "RaidenContractFilter":
token_network_registry_addresses=self.token_network_registry_addresses
| other.token_network_registry_addresses,
token_network_addresses=self.token_network_addresses | other.token_network_addresses,
channels_of_token_network=defaultdict(
set,
{
tn: self.channels_of_token_network[tn] | other.channels_of_token_network[tn]
for tn in {
*self.channels_of_token_network.keys(),
*other.channels_of_token_network.keys(),
}
},
),
channels_of_token_network={
tn: self.channels_of_token_network.get(tn, set())
| other.channels_of_token_network.get(tn, set())
for tn in {
*self.channels_of_token_network.keys(),
*other.channels_of_token_network.keys(),
}
},
ignore_secret_registry_until_channel_found=(
self.ignore_secret_registry_until_channel_found
and other.ignore_secret_registry_until_channel_found
Expand Down

0 comments on commit 97a68f5

Please sign in to comment.