diff options
| author | 2020-07-29 14:25:00 +0200 | |
|---|---|---|
| committer | 2020-07-29 14:25:00 +0200 | |
| commit | 222cdce0b9771b0c121da39b5f38363baf8bce09 (patch) | |
| tree | df25f169e8c13068c2dd88c426e0b27b8e5b6b6c | |
| parent | Expect status 400 for duplicates. (diff) | |
Use a defaultdict(list) for filter_list_cache.
| -rw-r--r-- | bot/bot.py | 5 | ||||
| -rw-r--r-- | bot/cogs/filter_lists.py | 6 | ||||
| -rw-r--r-- | bot/cogs/filtering.py | 2 |
3 files changed, 7 insertions, 6 deletions
diff --git a/bot/bot.py b/bot/bot.py index 3dfb4e948..a309e7192 100644 --- a/bot/bot.py +++ b/bot/bot.py @@ -2,6 +2,7 @@ import asyncio import logging import socket import warnings +from collections import defaultdict from typing import Optional import aiohttp @@ -34,7 +35,7 @@ class Bot(commands.Bot): self.redis_ready = asyncio.Event() self.redis_closed = False self.api_client = api.APIClient(loop=self.loop) - self.filter_list_cache = {} + self.filter_list_cache = defaultdict(list) self._connector = None self._resolver = None @@ -64,7 +65,7 @@ class Bot(commands.Bot): "created_at": item.get("created_at"), "updated_at": item.get("updated_at"), } - self.filter_list_cache.setdefault(f"{type_}.{allowed}", []).append(metadata) + self.filter_list_cache[f"{type_}.{allowed}"].append(metadata) async def _create_redis_session(self) -> None: """ diff --git a/bot/cogs/filter_lists.py b/bot/cogs/filter_lists.py index 9bd2da330..63d74e421 100644 --- a/bot/cogs/filter_lists.py +++ b/bot/cogs/filter_lists.py @@ -88,7 +88,7 @@ class FilterLists(Cog): "created_at": item.get("created_at"), "updated_at": item.get("updated_at"), } - self.bot.filter_list_cache.setdefault(f"{type_}.{allowed}", []).append(metadata) + self.bot.filter_list_cache[f"{type_}.{allowed}"].append(metadata) await ctx.message.add_reaction("✅") async def _delete_data(self, ctx: Context, allowed: bool, list_type: ValidFilterListType, content: str) -> None: @@ -110,7 +110,7 @@ class FilterLists(Cog): # Find the content and delete it. log.trace(f"Trying to delete the {content} item from the {list_type} {allow_type}") - for allow_list in self.bot.filter_list_cache.get(f"{list_type}.{allowed}", []): + for allow_list in self.bot.filter_list_cache[f"{list_type}.{allowed}"]: if content == allow_list.get("content"): item = allow_list break @@ -125,7 +125,7 @@ class FilterLists(Cog): async def _list_all_data(self, ctx: Context, allowed: bool, list_type: ValidFilterListType) -> None: """Paginate and display all items in a filterlist.""" allow_type = "whitelist" if allowed else "blacklist" - result = self.bot.filter_list_cache.get(f"{list_type}.{allowed}", []) + result = self.bot.filter_list_cache[f"{list_type}.{allowed}"] # Build a list of lines we want to show in the paginator lines = [] diff --git a/bot/cogs/filtering.py b/bot/cogs/filtering.py index 652af5ff5..9f9bcc464 100644 --- a/bot/cogs/filtering.py +++ b/bot/cogs/filtering.py @@ -101,7 +101,7 @@ class Filtering(Cog): def _get_filterlist_items(self, list_type: str, *, allowed: bool, compiled: Optional[bool] = False) -> list: """Fetch items from the filter_list_cache.""" - items = self.bot.filter_list_cache.get(f"{list_type.upper()}.{allowed}", []) + items = self.bot.filter_list_cache[f"{list_type.upper()}.{allowed}"] if compiled: return [re.compile(fr'{item["content"]}', flags=re.IGNORECASE) for item in items] |