From 2dbb3693dc3783c61b0ea0a5431d2f54c618ccbb Mon Sep 17 00:00:00 2001 From: dolfies Date: Thu, 28 Dec 2023 14:48:55 -0500 Subject: [PATCH] Refactor sorting to use attrgetter --- discord/abc.py | 5 +++-- discord/channel.py | 19 ++++++++++--------- discord/guild.py | 15 ++++++++------- discord/read_state.py | 3 ++- 4 files changed, 23 insertions(+), 19 deletions(-) diff --git a/discord/abc.py b/discord/abc.py index 387e79335..2b24c74f9 100644 --- a/discord/abc.py +++ b/discord/abc.py @@ -27,6 +27,7 @@ from __future__ import annotations import copy import asyncio from datetime import datetime +from operator import attrgetter from typing import ( Any, AsyncIterator, @@ -663,7 +664,7 @@ class GuildChannel: bucket = self._sorting_bucket channels: List[GuildChannel] = [c for c in self.guild.channels if c._sorting_bucket == bucket] - channels.sort(key=lambda c: c.position) + channels.sort(key=attrgetter('position')) try: # remove ourselves from the channel list @@ -1433,7 +1434,7 @@ class GuildChannel: ] # fmt: on - channels.sort(key=lambda c: (c.position, c.id)) + channels.sort(key=attrgetter('position', 'id')) try: # Try to remove ourselves from the channel list diff --git a/discord/channel.py b/discord/channel.py index 868d79dfb..463b03831 100644 --- a/discord/channel.py +++ b/discord/channel.py @@ -43,6 +43,7 @@ from typing import ( overload, ) import datetime +from operator import attrgetter import discord.abc from .scheduled_event import ScheduledEvent @@ -2072,14 +2073,14 @@ class CategoryChannel(discord.abc.GuildChannel, Hashable): def text_channels(self) -> List[TextChannel]: """List[:class:`TextChannel`]: Returns the text channels that are under this category.""" ret = [c for c in self.guild.channels if c.category_id == self.id and isinstance(c, TextChannel)] - ret.sort(key=lambda c: (c.position, c.id)) + ret.sort(key=attrgetter('position', 'id')) return ret @property def voice_channels(self) -> List[VoiceChannel]: """List[:class:`VoiceChannel`]: Returns the voice channels that are under this category.""" ret = [c for c in self.guild.channels if c.category_id == self.id and isinstance(c, VoiceChannel)] - ret.sort(key=lambda c: (c.position, c.id)) + ret.sort(key=attrgetter('position', 'id')) return ret @property @@ -2089,7 +2090,7 @@ class CategoryChannel(discord.abc.GuildChannel, Hashable): .. versionadded:: 1.7 """ ret = [c for c in self.guild.channels if c.category_id == self.id and isinstance(c, StageChannel)] - ret.sort(key=lambda c: (c.position, c.id)) + ret.sort(key=attrgetter('position', 'id')) return ret @property @@ -2098,9 +2099,9 @@ class CategoryChannel(discord.abc.GuildChannel, Hashable): .. versionadded:: 2.1 """ - r = [c for c in self.guild.channels if c.category_id == self.id and isinstance(c, ForumChannel)] - r.sort(key=lambda c: (c.position, c.id)) - return r + ret = [c for c in self.guild.channels if c.category_id == self.id and isinstance(c, ForumChannel)] + ret.sort(key=attrgetter('position', 'id')) + return ret @property def directory_channels(self) -> List[DirectoryChannel]: @@ -2108,9 +2109,9 @@ class CategoryChannel(discord.abc.GuildChannel, Hashable): .. versionadded:: 2.1 """ - r = [c for c in self.guild.channels if c.category_id == self.id and isinstance(c, DirectoryChannel)] - r.sort(key=lambda c: (c.position, c.id)) - return r + ret = [c for c in self.guild.channels if c.category_id == self.id and isinstance(c, DirectoryChannel)] + ret.sort(key=attrgetter('position', 'id')) + return ret @property def directories(self) -> List[DirectoryChannel]: diff --git a/discord/guild.py b/discord/guild.py index 20b24d0bd..8f890c81c 100644 --- a/discord/guild.py +++ b/discord/guild.py @@ -26,6 +26,7 @@ from __future__ import annotations import copy from datetime import datetime +from operator import attrgetter import unicodedata from typing import ( Any, @@ -764,7 +765,7 @@ class Guild(Hashable): This is sorted by the position and are in UI order from top to bottom. """ r = [ch for ch in self._channels.values() if isinstance(ch, VoiceChannel)] - r.sort(key=lambda c: (c.position, c.id)) + r.sort(key=attrgetter('position', 'id')) return r @property @@ -776,7 +777,7 @@ class Guild(Hashable): This is sorted by the position and are in UI order from top to bottom. """ r = [ch for ch in self._channels.values() if isinstance(ch, StageChannel)] - r.sort(key=lambda c: (c.position, c.id)) + r.sort(key=attrgetter('position', 'id')) return r @property @@ -841,7 +842,7 @@ class Guild(Hashable): This is sorted by the position and are in UI order from top to bottom. """ r = [ch for ch in self._channels.values() if isinstance(ch, TextChannel)] - r.sort(key=lambda c: (c.position, c.id)) + r.sort(key=attrgetter('position', 'id')) return r @property @@ -851,7 +852,7 @@ class Guild(Hashable): This is sorted by the position and are in UI order from top to bottom. """ r = [ch for ch in self._channels.values() if isinstance(ch, CategoryChannel)] - r.sort(key=lambda c: (c.position, c.id)) + r.sort(key=attrgetter('position', 'id')) return r @property @@ -863,7 +864,7 @@ class Guild(Hashable): .. versionadded:: 2.0 """ r = [ch for ch in self._channels.values() if isinstance(ch, ForumChannel)] - r.sort(key=lambda c: (c.position, c.id)) + r.sort(key=attrgetter('position', 'id')) return r @property @@ -875,7 +876,7 @@ class Guild(Hashable): .. versionadded:: 2.1 """ r = [ch for ch in self._channels.values() if isinstance(ch, DirectoryChannel)] - r.sort(key=lambda c: (c.position, c.id)) + r.sort(key=attrgetter('position', 'id')) return r @property @@ -920,7 +921,7 @@ class Guild(Hashable): as_list: List[ByCategoryItem] = [(_get(k), v) for k, v in grouped.items()] # type: ignore as_list.sort(key=key) for _, channels in as_list: - channels.sort(key=lambda c: (c._sorting_bucket, c.position, c.id)) + channels.sort(key=attrgetter('_sorting_bucket', 'position', 'id')) return as_list def _resolve_channel(self, id: Optional[int], /) -> Optional[Union[GuildChannel, Thread]]: diff --git a/discord/read_state.py b/discord/read_state.py index f142fbc42..7df571df0 100644 --- a/discord/read_state.py +++ b/discord/read_state.py @@ -25,6 +25,7 @@ DEALINGS IN THE SOFTWARE. from __future__ import annotations from datetime import date +from operator import attrgetter from typing import TYPE_CHECKING, Optional, Union from .channel import PartialMessageable @@ -188,7 +189,7 @@ class ReadState: if self.type == ReadStateType.channel: return resource.last_message_id or 0 # type: ignore elif self.type == ReadStateType.scheduled_events: - return max(resource.scheduled_events, key=lambda e: e.id).id # type: ignore + return max(resource.scheduled_events, key=attrgetter('id')).id # type: ignore return 0 @property