Skip to content

Commit

Permalink
fix: use defaultdict
Browse files Browse the repository at this point in the history
  • Loading branch information
dtdang committed Jan 14, 2025
1 parent 947d907 commit 2eee7e1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
15 changes: 9 additions & 6 deletions silverback/_cli.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import asyncio
import os
from collections import defaultdict
from datetime import datetime, timedelta, timezone
from pathlib import Path
from typing import TYPE_CHECKING, Optional
Expand Down Expand Up @@ -1062,18 +1063,20 @@ def list_bots(cluster: "ClusterClient"):
"""List all bots in a CLUSTER (Regardless of status)"""

if bot_names := cluster.bots_list():
grouped_bots: dict[str, dict[str, list[Bot]]] = {}
grouped_bots: defaultdict[str, defaultdict[str, list[Bot]]] = defaultdict(
lambda: defaultdict(list)
)
for bot_list in bot_names.values():
for bot in bot_list:
ecosystem, network, provider = bot.network.split("-")
network_key = f"{network}-{provider}"
grouped_bots.setdefault(ecosystem, {}).setdefault(network_key, []).append(bot)
grouped_bots[ecosystem][network_key].append(bot)

for ecosystem in sorted(grouped_bots.keys()):
grouped_bots[ecosystem] = {
network: sorted(bots, key=lambda b: b.name)
for network, bots in sorted(grouped_bots[ecosystem].items())
}
for network in grouped_bots[ecosystem]:
grouped_bots[ecosystem][network] = sorted(
grouped_bots[ecosystem][network], key=lambda b: b.name
)

output = ""
for ecosystem in grouped_bots:
Expand Down
2 changes: 1 addition & 1 deletion silverback/cluster/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ def bots_list(self) -> dict[str, list[Bot]]:
bots_dict = defaultdict(list)
for bot in map(Bot.model_validate, response.json()):
bots_dict[bot.name].append(bot)
return dict(bots_dict)
return bots_dict

def new_bot(
self,
Expand Down

0 comments on commit 2eee7e1

Please sign in to comment.