aboutsummaryrefslogtreecommitdiff
path: root/discord/utils.py
diff options
context:
space:
mode:
authorRapptz <[email protected]>2015-12-30 13:00:52 -0500
committerRapptz <[email protected]>2015-12-30 14:48:34 -0500
commit7765580a140400cc55db93ed29d661b9542b3a74 (patch)
tree82112763fb7765ee91990dba5f3dff0e4aa10b58 /discord/utils.py
parentChanged cache check to be the login endpoint itself. (diff)
downloaddiscord.py-7765580a140400cc55db93ed29d661b9542b3a74.tar.xz
discord.py-7765580a140400cc55db93ed29d661b9542b3a74.zip
utils.get now supports nested attribute retrieval.
Diffstat (limited to 'discord/utils.py')
-rw-r--r--discord/utils.py18
1 files changed, 15 insertions, 3 deletions
diff --git a/discord/utils.py b/discord/utils.py
index 1629a454..74c070c8 100644
--- a/discord/utils.py
+++ b/discord/utils.py
@@ -88,6 +88,9 @@ def get(iterable, **attrs):
logical AND, not logical OR. Meaning they have to meet every
attribute passed in and not one of them.
+ To have a nested attribute search (i.e. search by ``x.y``) then
+ pass in ``x__y`` as the keyword argument.
+
If nothing is found that matches the attributes passed, then
``None`` is returned.
@@ -106,6 +109,12 @@ def get(iterable, **attrs):
channel = discord.utils.get(server.channels, name='Foo', type=ChannelType.voice)
+ Nested attribute matching:
+
+ .. code-block:: python
+
+ channel = discord.utils.get(client.get_all_channels(), server__name='Cool', name='general')
+
Parameters
-----------
iterable
@@ -116,9 +125,12 @@ def get(iterable, **attrs):
def predicate(elem):
for attr, val in attrs.items():
- if not hasattr(elem, attr):
- return False
- if getattr(elem, attr) != val:
+ nested = attr.split('__')
+ obj = elem
+ for attribute in nested:
+ obj = getattr(obj, attribute)
+
+ if obj != val:
return False
return True