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 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

19
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]:

15
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]]:

3
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

Loading…
Cancel
Save