|
13 | 13 | # limitations under the License. |
14 | 14 | import random |
15 | 15 |
|
16 | | -from collections import namedtuple |
| 16 | +from collections import namedtuple, defaultdict |
17 | 17 | from functools import lru_cache |
18 | 18 | from itertools import islice, cycle, groupby, repeat |
19 | 19 | import logging |
@@ -254,11 +254,9 @@ def _dc(self, host): |
254 | 254 |
|
255 | 255 | def populate(self, cluster, hosts): |
256 | 256 | # Group hosts by datacenter without relying on groupby which only groups consecutive items |
257 | | - dc_hosts_dict = {} |
| 257 | + dc_hosts_dict = defaultdict(list) |
258 | 258 | for host in hosts: |
259 | 259 | dc = self._dc(host) |
260 | | - if dc not in dc_hosts_dict: |
261 | | - dc_hosts_dict[dc] = [] |
262 | 260 | dc_hosts_dict[dc].append(host) |
263 | 261 |
|
264 | 262 | # Convert lists to tuples with unique hosts |
@@ -383,22 +381,17 @@ def _dc(self, host): |
383 | 381 |
|
384 | 382 | def populate(self, cluster, hosts): |
385 | 383 | # Group hosts by (dc, rack) and by dc without relying on groupby which only groups consecutive items |
386 | | - rack_hosts_dict = {} |
387 | | - dc_hosts_dict = {} |
| 384 | + rack_hosts_dict = defaultdict(list) |
| 385 | + dc_hosts_dict = defaultdict(list) |
388 | 386 |
|
389 | 387 | for host in hosts: |
390 | 388 | dc = self._dc(host) |
391 | 389 | rack = self._rack(host) |
392 | 390 |
|
393 | 391 | # Group by (dc, rack) |
394 | | - key = (dc, rack) |
395 | | - if key not in rack_hosts_dict: |
396 | | - rack_hosts_dict[key] = [] |
397 | | - rack_hosts_dict[key].append(host) |
| 392 | + rack_hosts_dict[(dc, rack)].append(host) |
398 | 393 |
|
399 | 394 | # Group by dc |
400 | | - if dc not in dc_hosts_dict: |
401 | | - dc_hosts_dict[dc] = [] |
402 | 395 | dc_hosts_dict[dc].append(host) |
403 | 396 |
|
404 | 397 | # Convert lists to tuples with unique hosts |
|
0 commit comments