Skip to content

Commit 6c57d5e

Browse files
committed
♻️ move find function from __init__.py to public.py
1 parent c29cdc2 commit 6c57d5e

File tree

2 files changed

+32
-29
lines changed

2 files changed

+32
-29
lines changed

discord/utils/__init__.py

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
basic_autocomplete,
5050
generate_snowflake,
5151
utcnow,
52+
find,
5253
snowflake_time,
5354
oauth_url,
5455
Undefined,
@@ -210,32 +211,6 @@ def decorator(overridden: T) -> T:
210211
return decorator
211212

212213

213-
def find(predicate: Callable[[T], Any], seq: Iterable[T]) -> T | None:
214-
"""A helper to return the first element found in the sequence
215-
that meets the predicate. For example: ::
216-
217-
member = discord.utils.find(lambda m: m.name == "Mighty", channel.guild.members)
218-
219-
would find the first :class:`~discord.Member` whose name is 'Mighty' and return it.
220-
If an entry is not found, then ``None`` is returned.
221-
222-
This is different from :func:`py:filter` due to the fact it stops the moment it finds
223-
a valid entry.
224-
225-
Parameters
226-
----------
227-
predicate
228-
A function that returns a boolean-like result.
229-
seq: :class:`collections.abc.Iterable`
230-
The iterable to search through.
231-
"""
232-
233-
for element in seq:
234-
if predicate(element):
235-
return element
236-
return None
237-
238-
239214
async def get_or_fetch(obj, attr: str, id: int, *, default: Any = MISSING) -> Any:
240215
"""|coro|
241216

discord/utils/public.py

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,18 @@
66
from enum import Enum, auto
77
import itertools
88
from collections.abc import Awaitable, Callable, Iterable
9-
from typing import TYPE_CHECKING, Any, Literal, Iterable
10-
9+
from typing import TYPE_CHECKING, Any, Literal, Iterable, TypeVar
1110

1211
if TYPE_CHECKING:
13-
from .abc import Snowflake
12+
from ..abc import Snowflake
1413
from ..commands.context import AutocompleteContext
1514
from ..commands.options import OptionChoice
1615
from ..permissions import Permissions
1716

1817

18+
T = TypeVar("T")
19+
20+
1921
class Undefined(Enum):
2022
MISSING = auto()
2123

@@ -506,3 +508,29 @@ def replacement(match):
506508
else:
507509
text = re.sub(r"\\", r"\\\\", text)
508510
return _MARKDOWN_ESCAPE_REGEX.sub(r"\\\1", text)
511+
512+
513+
def find(predicate: Callable[[T], Any], seq: Iterable[T]) -> T | None:
514+
"""A helper to return the first element found in the sequence
515+
that meets the predicate. For example: ::
516+
517+
member = discord.utils.find(lambda m: m.name == "Mighty", channel.guild.members)
518+
519+
would find the first :class:`~discord.Member` whose name is 'Mighty' and return it.
520+
If an entry is not found, then ``None`` is returned.
521+
522+
This is different from :func:`py:filter` due to the fact it stops the moment it finds
523+
a valid entry.
524+
525+
Parameters
526+
----------
527+
predicate
528+
A function that returns a boolean-like result.
529+
seq: :class:`collections.abc.Iterable`
530+
The iterable to search through.
531+
"""
532+
533+
for element in seq:
534+
if predicate(element):
535+
return element
536+
return None

0 commit comments

Comments
 (0)