pylav.utils.vendor.redbot package#
Module contents#
This module contains various utilities for working with asyncio taken from https://github.com/Cog-Creators/Red-DiscordBot/blob/V3/develop/redbot/core/utils/predicates.py https://github.com/Cog-Creators/Red-DiscordBot/blob/V3/develop/redbot/core/utils/__init__.py
Original license can be found at: https://github.com/Cog-Creators/Red-DiscordBot/blob/V3/develop/LICENSE
- class pylav.utils.vendor.redbot.AsyncFilter(func, iterable)[source]#
Bases:
AsyncIterator
[_T
],Awaitable
[list
[_T
]]Class returned by async_filter. See that function for details.
We do not recommend instantiating this class directly.
- class pylav.utils.vendor.redbot.AsyncIter(iterable, delay=0, steps=100)[source]#
Bases:
AsyncIterator
[_T
],Awaitable
[list
[_T
]]Asynchronous iterator yielding items from
iterable
that sleeps fordelay
seconds everysteps
items.- Parameters:
iterable (
Iterable
) – The iterable to make async.delay (
Union[float
,int]
) – The amount of time in seconds to sleep.steps (
int
) – The number of iterations between sleeps.
- Raises:
ValueError – When
steps
is lower than 1.
Examples
>>> from pylav.utils.vendor.redbot import AsyncIter >>> async for value in AsyncIter(range(3)): ... print(value) 0 1 2
- __await__()[source]#
Returns a list of the iterable.
Examples
>>> from pylav.utils.vendor.redbot import AsyncIter >>> iterator = AsyncIter(range(5)) >>> await iterator [0, 1, 2, 3, 4]
- enumerate(start=0)[source]#
Async iterable version of enumerate.
- Parameters:
start (
int
) – The index to start from. Defaults to 0.- Returns:
An async iterator of tuples in the form of
(index, item)
.- Return type:
AsyncIterator[Tuple[int
,T]]
Examples
>>> from pylav.utils.vendor.redbot import AsyncIter >>> iterator = AsyncIter(['one', 'two', 'three']) >>> async for i in iterator.enumerate(start=10): ... print(i) (10, 'one') (11, 'two') (12, 'three')
- filter(function)[source]#
Filter the iterable with an (optionally async) predicate.
- Parameters:
function (
Callable[[T]
,Union[bool
,Awaitable[bool]]]
) – A function or coroutine function which takes one item ofiterable
as an argument, and returnsTrue
orFalse
.- Returns:
An object which can either be awaited to yield a list of the filtered items, or can also act as an async iterator to yield items one by one.
- Return type:
AsyncFilter[T]
Examples
>>> from pylav.utils.vendor.redbot import AsyncIter >>> def predicate(value): ... return value <= 5 >>> iterator = AsyncIter([1, 10, 5, 100]) >>> async for i in iterator.filter(predicate): ... print(i) 1 5
>>> from pylav.utils.vendor.redbot import AsyncIter >>> def predicate(value): ... return value <= 5 >>> iterator = AsyncIter([1, 10, 5, 100]) >>> await iterator.filter(predicate) [1, 5]
- async find(predicate, default=None)[source]#
Calls
predicate
over items in iterable and return first value to match.- Parameters:
predicate (
Union[Callable
,Coroutine]
) – A function that returns a boolean-like result. The predicate provided can be a coroutine.default (
Optional[Any]
) – The value to return if there are no matches.
- Raises:
TypeError – When
predicate
is not a callable.
Examples
>>> from pylav.utils.vendor.redbot import AsyncIter >>> await AsyncIter(range(3)).find(lambda x: x == 1) 1
- async flatten()[source]#
Returns a list of the iterable.
Examples
>>> from pylav.utils.vendor.redbot import AsyncIter >>> iterator = AsyncIter(range(5)) >>> await iterator.flatten() [0, 1, 2, 3, 4]
- map(func)[source]#
Set the mapping callable for this instance of AsyncIter.
Important
This should be called after AsyncIter initialization and before any other of its methods.
- Parameters:
func (
Union[Callable
,Coroutine]
) – The function to map values to. The function provided can be a coroutine.- Raises:
TypeError – When
func
is not a callable.
Examples
>>> from pylav.utils.vendor.redbot import AsyncIter >>> async for value in AsyncIter(range(3)).map(bool): ... print(value) False True True
- async next(default=Ellipsis)[source]#
Returns a next entry of the iterable.
- Parameters:
default (
Optional[Any]
) – The value to return if the iterator is exhausted.- Raises:
StopAsyncIteration – When
default
is not specified and the iterator has been exhausted.
Examples
>>> from pylav.utils.vendor.redbot import AsyncIter >>> iterator = AsyncIter(range(5)) >>> await iterator.next() 0 >>> await iterator.next() 1
- class pylav.utils.vendor.redbot.MessagePredicate(predicate)[source]#
Bases:
Callable
[Message
,bool
]A simple collection of predicates for message events.
These predicates intend to help simplify checks in message events and reduce boilerplate code.
This class should be created through the provided classmethods. Instances of this class are callable message predicates, i.e. they return
True
if a message matches the criteria.All predicates are combined with
MessagePredicate.same_context()
.Examples
Waiting for a response in the same channel and from the same author:
await bot.wait_for("message", check=MessagePredicate.same_context(ctx))
Waiting for a response to a yes or no question:
pred = MessagePredicate.yes_or_no(ctx) await bot.wait_for("message", check=pred) if pred.result is True: # User responded "yes" ...
Getting a member object from a user’s response:
pred = MessagePredicate.valid_member(ctx) await bot.wait_for("message", check=pred) member = pred.result
- result#
The object which the message content matched with. This is dependent on the predicate used - see each predicate’s documentation for details, not every method will assign this attribute. Defaults to
None
.- Type:
Any
- classmethod cancelled(ctx=None, channel=None, user=None)[source]#
Match if the message is
[p]cancel
.- Parameters:
ctx (
Optional[Context]
) – Same asctx
insame_context()
.channel (
Optional[Union[`discord.TextChannel`
, discord.Thread, discord.DMChannel]]) – Same aschannel
insame_context()
.user (
Optional[discord.abc.User]
) – Same asuser
insame_context()
.
- Returns:
The event predicate.
- Return type:
- classmethod contained_in(collection, ctx=None, channel=None, user=None)[source]#
Match if the response is contained in the specified collection.
The index of the response in the
collection
sequence is assigned to the result attribute.- Parameters:
collection (
Sequence[str]
) – The collection containing valid responses.ctx (
Optional[Context]
) – Same asctx
insame_context()
.channel (
Optional[Union[`discord.TextChannel`
, discord.Thread, discord.DMChannel]]) – Same aschannel
insame_context()
.user (
Optional[discord.abc.User]
) – Same asuser
insame_context()
.
- Returns:
The event predicate.
- Return type:
- classmethod equal_to(value, ctx=None, channel=None, user=None)[source]#
Match if the response is equal to the specified value.
- Parameters:
value (
str
) – The value to compare the response with.ctx (
Optional[Context]
) – Same asctx
insame_context()
.channel (
Optional[Union[`discord.TextChannel`
, discord.Thread, discord.DMChannel]]) – Same aschannel
insame_context()
.user (
Optional[discord.abc.User]
) – Same asuser
insame_context()
.
- Returns:
The event predicate.
- Return type:
- classmethod greater(value, ctx=None, channel=None, user=None)[source]#
Match if the response is greater than the specified value.
- Parameters:
value (
Union[int
,float]
) – The value to compare the response with.ctx (
Optional[Context]
) – Same asctx
insame_context()
.channel (
Optional[Union[`discord.TextChannel`
, discord.Thread, discord.DMChannel]]) – Same aschannel
insame_context()
.user (
Optional[discord.abc.User]
) – Same asuser
insame_context()
.
- Returns:
The event predicate.
- Return type:
- classmethod has_role(ctx=None, channel=None, user=None)[source]#
Match if the response refers to a role which the author has.
Assigns the matching discord.Role object to result.
One of
user
orctx
must be supplied. This predicate cannot be used in DM.- Parameters:
ctx (
Optional[Context]
) – Same asctx
insame_context()
.channel (
Optional[Union[`discord.TextChannel`
, discord.Thread]]) – Same aschannel
insame_context()
.user (
Optional[discord.abc.User]
) – Same asuser
insame_context()
.
- Returns:
The event predicate.
- Return type:
- classmethod length_greater(length, ctx=None, channel=None, user=None)[source]#
Match if the response’s length is greater than the specified length.
- Parameters:
length (
int
) – The value to compare the response’s length with.ctx (
Optional[Context]
) – Same asctx
insame_context()
.channel (
Optional[Union[`discord.TextChannel`
, discord.Thread, discord.DMChannel]]) – Same aschannel
insame_context()
.user (
Optional[discord.abc.User]
) – Same asuser
insame_context()
.
- Returns:
The event predicate.
- Return type:
- classmethod length_less(length, ctx=None, channel=None, user=None)[source]#
Match if the response’s length is less than the specified length.
- Parameters:
length (
int
) – The value to compare the response’s length with.ctx (
Optional[Context]
) – Same asctx
insame_context()
.channel (
Optional[Union[`discord.TextChannel`
, discord.Thread, discord.DMChannel]]) – Same aschannel
insame_context()
.user (
Optional[discord.abc.User]
) – Same asuser
insame_context()
.
- Returns:
The event predicate.
- Return type:
- classmethod less(value, ctx=None, channel=None, user=None)[source]#
Match if the response is less than the specified value.
- Parameters:
value (
Union[int
,float]
) – The value to compare the response with.ctx (
Optional[Context]
) – Same asctx
insame_context()
.channel (
Optional[Union[`discord.TextChannel`
, discord.Thread, discord.DMChannel]]) – Same aschannel
insame_context()
.user (
Optional[discord.abc.User]
) – Same asuser
insame_context()
.
- Returns:
The event predicate.
- Return type:
- classmethod lower_contained_in(collection, ctx=None, channel=None, user=None)[source]#
Same as
contained_in()
, but the response is set to lowercase before matching.- Parameters:
collection (
Sequence[str]
) – The collection containing valid lowercase responses.ctx (
Optional[Context]
) – Same asctx
insame_context()
.channel (
Optional[Union[`discord.TextChannel`
, discord.Thread, discord.DMChannel]]) – Same aschannel
insame_context()
.user (
Optional[discord.abc.User]
) – Same asuser
insame_context()
.
- Returns:
The event predicate.
- Return type:
- classmethod lower_equal_to(value, ctx=None, channel=None, user=None)[source]#
Match if the response as lowercase is equal to the specified value.
- Parameters:
value (
str
) – The value to compare the response with.ctx (
Optional[Context]
) – Same asctx
insame_context()
.channel (
Optional[Union[`discord.TextChannel`
, discord.Thread, discord.DMChannel]]) – Same aschannel
insame_context()
.user (
Optional[discord.abc.User]
) – Same asuser
insame_context()
.
- Returns:
The event predicate.
- Return type:
- classmethod positive(ctx=None, channel=None, user=None)[source]#
Match if the response is a positive number.
Assigns the response to result as a float.
- Parameters:
ctx (
Optional[Context]
) – Same asctx
insame_context()
.channel (
Optional[Union[`discord.TextChannel`
, discord.Thread, discord.DMChannel]]) – Same aschannel
insame_context()
.user (
Optional[discord.abc.User]
) – Same asuser
insame_context()
.
- Returns:
The event predicate.
- Return type:
- classmethod regex(pattern, ctx=None, channel=None, user=None)[source]#
Match if the response matches the specified regex pattern.
This predicate will use re.search to find a match. The resulting match object <match-objects> will be assigned to result.
- Parameters:
pattern (
Union[`pattern object <re-objects>`
,str]
) – The pattern to search for in the response.ctx (
Optional[Context]
) – Same asctx
insame_context()
.channel (
Optional[Union[`discord.TextChannel`
, discord.Thread, discord.DMChannel]]) – Same aschannel
insame_context()
.user (
Optional[discord.abc.User]
) – Same asuser
insame_context()
.
- Returns:
The event predicate.
- Return type:
- result#
- classmethod same_context(ctx=None, channel=None, user=None)[source]#
Match if the message fits the described context.
- Parameters:
ctx (
Optional[Context]
) – The current invocation context.channel (
Optional[Union[`discord.TextChannel`
, discord.Thread, discord.DMChannel]]) – The channel we expect a message in. If unspecified, defaults toctx.channel
. Ifctx
is unspecified too, the message’s channel will be ignored.user (
Optional[discord.abc.User]
) – The user we expect a message from. If unspecified, defaults toctx.author
. Ifctx
is unspecified too, the message’s author will be ignored.
- Returns:
The event predicate.
- Return type:
- classmethod valid_float(ctx=None, channel=None, user=None)[source]#
Match if the response is a float.
Assigns the response to result as a float.
- Parameters:
ctx (
Optional[Context]
) – Same asctx
insame_context()
.channel (
Optional[Union[`discord.TextChannel`
, discord.Thread, discord.DMChannel]]) – Same aschannel
insame_context()
.user (
Optional[discord.abc.User]
) – Same asuser
insame_context()
.
- Returns:
The event predicate.
- Return type:
- classmethod valid_int(ctx=None, channel=None, user=None)[source]#
Match if the response is an integer.
Assigns the response to result as an int.
- Parameters:
ctx (
Optional[Context]
) – Same asctx
insame_context()
.channel (
Optional[Union[`discord.TextChannel`
, discord.Thread, discord.DMChannel]]) – Same aschannel
insame_context()
.user (
Optional[discord.abc.User]
) – Same asuser
insame_context()
.
- Returns:
The event predicate.
- Return type:
- classmethod valid_member(ctx=None, channel=None, user=None)[source]#
Match if the response refers to a member in the current guild.
Assigns the matching discord.Member object to result.
This predicate cannot be used in DM.
- Parameters:
ctx (
Optional[Context]
) – Same asctx
insame_context()
.channel (
Optional[Union[`discord.TextChannel`
, discord.Thread]]) – Same aschannel
insame_context()
.user (
Optional[discord.abc.User]
) – Same asuser
insame_context()
.
- Returns:
The event predicate.
- Return type:
- classmethod valid_role(ctx=None, channel=None, user=None)[source]#
Match if the response refers to a role in the current guild.
Assigns the matching discord.Role object to result.
This predicate cannot be used in DM.
- Parameters:
ctx (
Optional[Context]
) – Same asctx
insame_context()
.channel (
Optional[Union[`discord.TextChannel`
, discord.Thread]]) – Same aschannel
insame_context()
.user (
Optional[discord.abc.User]
) – Same asuser
insame_context()
.
- Returns:
The event predicate.
- Return type:
- classmethod valid_text_channel(ctx=None, channel=None, user=None)[source]#
Match if the response refers to a text channel in the current guild.
Assigns the matching discord.TextChannel object to result.
This predicate cannot be used in DM.
- Parameters:
ctx (
Optional[Context]
) – Same asctx
insame_context()
.channel (
Optional[Union[`discord.TextChannel`
, discord.Thread]]) – Same aschannel
insame_context()
.user (
Optional[discord.abc.User]
) – Same asuser
insame_context()
.
- Returns:
The event predicate.
- Return type:
- classmethod yes_or_no(ctx=None, channel=None, user=None)[source]#
Match if the message is “yes”/”y” or “no”/”n”.
This will assign
True
for yes, orFalse
for no to the result attribute.- Parameters:
ctx (
Optional[Context]
) – Same asctx
insame_context()
.channel (
Optional[Union[`discord.TextChannel`
, discord.Thread, discord.DMChannel]]) – Same aschannel
insame_context()
.user (
Optional[discord.abc.User]
) – Same asuser
insame_context()
.
- Returns:
The event predicate.
- Return type:
- async pylav.utils.vendor.redbot.async_enumerate(async_iterable, start=0)[source]#
Async iterable version of enumerate.
- Parameters:
async_iterable (
AsyncIterable[T]
) – The iterable to enumerate.start (
int
) – The index to start from. Defaults to 0.
- Returns:
An async iterator of tuples in the form of
(index, item)
.- Return type:
AsyncIterator[Tuple[int
,T]]
- pylav.utils.vendor.redbot.async_filter(func, iterable)[source]#
Filter an (optionally async) iterable with an (optionally async) predicate.
At least one of the arguments must be async.
- Parameters:
func (
Callable[[T]
,Union[bool
,Awaitable[bool]]]
) – A function or coroutine function which takes one item ofiterable
as an argument, and returnsTrue
orFalse
.iterable (
Union[AsyncIterable[_T]
,Iterable[_T]]
) – An iterable or async iterable which is to be filtered.
- Raises:
TypeError – If neither of the arguments are async.
- Returns:
An object which can either be awaited to yield a list of the filtered items, or can also act as an async iterator to yield items one by one.
- Return type:
AsyncFilter[T]
- pylav.utils.vendor.redbot.bounded_gather(*coros_or_futures, return_exceptions=False, limit=4, semaphore=None)[source]#
A semaphore-bounded wrapper to
asyncio.gather()
.- Parameters:
*coros_or_futures – The awaitables to run in a bounded concurrent fashion.
return_exceptions (
bool
) – If true, gather exceptions in the result list instead of raising.limit (
Optional[`int
]`) – The maximum number of concurrent tasks. Used when nosemaphore
is passed.semaphore (
Optional[:class:`asyncio.Semaphore
]`) – The semaphore to use for bounding tasks. If None, create one usingloop
andlimit
.
- Raises:
TypeError – When invalid parameters are passed
- pylav.utils.vendor.redbot.bounded_gather_iter(*coros_or_futures, limit=4, semaphore=None)[source]#
An iterator that returns tasks as they are ready, but limits the number of tasks running at a time.
- Parameters:
*coros_or_futures – The awaitables to run in a bounded concurrent fashion.
limit (
Optional[`int
]`) – The maximum number of concurrent tasks. Used when nosemaphore
is passed.semaphore (
Optional[:class:`asyncio.Semaphore
]`) – The semaphore to use for bounding tasks. If None, create one usingloop
andlimit
.
- Raises:
TypeError – When invalid parameters are passed