Browse Source

Refactor sorting to use attrgetter

pull/10109/head
dolfies 1 year ago
parent
commit
2dbb3693dc
  1. 5
      discord/abc.py
  2. 19
      discord/channel.py
  3. 15
      discord/guild.py
  4. 3
      discord/read_state.py

5
discord/abc.py

@ -27,6 +27,7 @@ from __future__ import annotations
import copy import copy
import asyncio import asyncio
from datetime import datetime from datetime import datetime
from operator import attrgetter
from typing import ( from typing import (
Any, Any,
AsyncIterator, AsyncIterator,
@ -663,7 +664,7 @@ class GuildChannel:
bucket = self._sorting_bucket bucket = self._sorting_bucket
channels: List[GuildChannel] = [c for c in self.guild.channels if c._sorting_bucket == 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: try:
# remove ourselves from the channel list # remove ourselves from the channel list
@ -1433,7 +1434,7 @@ class GuildChannel:
] ]
# fmt: on # fmt: on
channels.sort(key=lambda c: (c.position, c.id)) channels.sort(key=attrgetter('position', 'id'))
try: try:
# Try to remove ourselves from the channel list # Try to remove ourselves from the channel list

19
discord/channel.py

@ -43,6 +43,7 @@ from typing import (
overload, overload,
) )
import datetime import datetime
from operator import attrgetter
import discord.abc import discord.abc
from .scheduled_event import ScheduledEvent from .scheduled_event import ScheduledEvent
@ -2072,14 +2073,14 @@ class CategoryChannel(discord.abc.GuildChannel, Hashable):
def text_channels(self) -> List[TextChannel]: def text_channels(self) -> List[TextChannel]:
"""List[:class:`TextChannel`]: Returns the text channels that are under this category.""" """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 = [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 return ret
@property @property
def voice_channels(self) -> List[VoiceChannel]: def voice_channels(self) -> List[VoiceChannel]:
"""List[:class:`VoiceChannel`]: Returns the voice channels that are under this category.""" """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 = [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 return ret
@property @property
@ -2089,7 +2090,7 @@ class CategoryChannel(discord.abc.GuildChannel, Hashable):
.. versionadded:: 1.7 .. versionadded:: 1.7
""" """
ret = [c for c in self.guild.channels if c.category_id == self.id and isinstance(c, StageChannel)] 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 return ret
@property @property
@ -2098,9 +2099,9 @@ class CategoryChannel(discord.abc.GuildChannel, Hashable):
.. versionadded:: 2.1 .. versionadded:: 2.1
""" """
r = [c for c in self.guild.channels if c.category_id == self.id and isinstance(c, ForumChannel)] ret = [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)) ret.sort(key=attrgetter('position', 'id'))
return r return ret
@property @property
def directory_channels(self) -> List[DirectoryChannel]: def directory_channels(self) -> List[DirectoryChannel]:
@ -2108,9 +2109,9 @@ class CategoryChannel(discord.abc.GuildChannel, Hashable):
.. versionadded:: 2.1 .. versionadded:: 2.1
""" """
r = [c for c in self.guild.channels if c.category_id == self.id and isinstance(c, DirectoryChannel)] ret = [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)) ret.sort(key=attrgetter('position', 'id'))
return r return ret
@property @property
def directories(self) -> List[DirectoryChannel]: def directories(self) -> List[DirectoryChannel]:

15
discord/guild.py

@ -26,6 +26,7 @@ from __future__ import annotations
import copy import copy
from datetime import datetime from datetime import datetime
from operator import attrgetter
import unicodedata import unicodedata
from typing import ( from typing import (
Any, Any,
@ -764,7 +765,7 @@ class Guild(Hashable):
This is sorted by the position and are in UI order from top to bottom. 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 = [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 return r
@property @property
@ -776,7 +777,7 @@ class Guild(Hashable):
This is sorted by the position and are in UI order from top to bottom. 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 = [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 return r
@property @property
@ -841,7 +842,7 @@ class Guild(Hashable):
This is sorted by the position and are in UI order from top to bottom. 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 = [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 return r
@property @property
@ -851,7 +852,7 @@ class Guild(Hashable):
This is sorted by the position and are in UI order from top to bottom. 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 = [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 return r
@property @property
@ -863,7 +864,7 @@ class Guild(Hashable):
.. versionadded:: 2.0 .. versionadded:: 2.0
""" """
r = [ch for ch in self._channels.values() if isinstance(ch, ForumChannel)] 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 return r
@property @property
@ -875,7 +876,7 @@ class Guild(Hashable):
.. versionadded:: 2.1 .. versionadded:: 2.1
""" """
r = [ch for ch in self._channels.values() if isinstance(ch, DirectoryChannel)] 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 return r
@property @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: List[ByCategoryItem] = [(_get(k), v) for k, v in grouped.items()] # type: ignore
as_list.sort(key=key) as_list.sort(key=key)
for _, channels in as_list: 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 return as_list
def _resolve_channel(self, id: Optional[int], /) -> Optional[Union[GuildChannel, Thread]]: def _resolve_channel(self, id: Optional[int], /) -> Optional[Union[GuildChannel, Thread]]:

3
discord/read_state.py

@ -25,6 +25,7 @@ DEALINGS IN THE SOFTWARE.
from __future__ import annotations from __future__ import annotations
from datetime import date from datetime import date
from operator import attrgetter
from typing import TYPE_CHECKING, Optional, Union from typing import TYPE_CHECKING, Optional, Union
from .channel import PartialMessageable from .channel import PartialMessageable
@ -188,7 +189,7 @@ class ReadState:
if self.type == ReadStateType.channel: if self.type == ReadStateType.channel:
return resource.last_message_id or 0 # type: ignore return resource.last_message_id or 0 # type: ignore
elif self.type == ReadStateType.scheduled_events: 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 return 0
@property @property

Loading…
Cancel
Save