[autotest] Simplify bucket_hosts_by_shard with defaultdict
Beside making the code cleaner, this is slightly more performant since
we dont create and throw away an empty list for setdefault() in every
loop.
BUG=None
TEST=None
Change-Id: I05aa81c47cbc604a1378794dd214ee58629a0c3e
Reviewed-on: https://chromium-review.googlesource.com/430207
Commit-Ready: Allen Li <ayatane@chromium.org>
Tested-by: Allen Li <ayatane@chromium.org>
Reviewed-by: Allen Li <ayatane@chromium.org>
diff --git a/frontend/afe/rpc_utils.py b/frontend/afe/rpc_utils.py
index 14207ae..897d8fe 100644
--- a/frontend/afe/rpc_utils.py
+++ b/frontend/afe/rpc_utils.py
@@ -6,6 +6,7 @@
__author__ = 'showard@google.com (Steve Howard)'
+import collections
import datetime
from functools import wraps
import inspect
@@ -864,12 +865,12 @@
@return: A map of shard hostname: list of hosts on the shard.
"""
- shard_host_map = {}
+ shard_host_map = collections.defaultdict(list)
for host in host_objs:
if host.shard:
shard_name = (host.shard.rpc_hostname() if rpc_hostnames
else host.shard.hostname)
- shard_host_map.setdefault(shard_name, []).append(host.hostname)
+ shard_host_map[shard_name].append(host.hostname)
return shard_host_map