From 2eee7e11d41b8bd3d1bb7fa49b1f2b3da117800d Mon Sep 17 00:00:00 2001 From: Dalena Date: Tue, 14 Jan 2025 13:52:05 -0500 Subject: [PATCH] fix: use defaultdict --- silverback/_cli.py | 15 +++++++++------ silverback/cluster/client.py | 2 +- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/silverback/_cli.py b/silverback/_cli.py index f3b5b8f2..1e99af74 100644 --- a/silverback/_cli.py +++ b/silverback/_cli.py @@ -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 @@ -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: diff --git a/silverback/cluster/client.py b/silverback/cluster/client.py index ad85910e..729d06d3 100644 --- a/silverback/cluster/client.py +++ b/silverback/cluster/client.py @@ -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,