diff options
| author | Rapptz <[email protected]> | 2019-05-29 00:45:48 -0400 |
|---|---|---|
| committer | Rapptz <[email protected]> | 2019-05-29 00:45:48 -0400 |
| commit | 0622e18cb9026f1088f5848b57b5ac3df6455c13 (patch) | |
| tree | 7362dd1c78f914df044b93ed9e7604b91151989e | |
| parent | Ensure message links resolve in the Messageable.pins note. (diff) | |
| download | discord.py-0622e18cb9026f1088f5848b57b5ac3df6455c13.tar.xz discord.py-0622e18cb9026f1088f5848b57b5ac3df6455c13.zip | |
Speed-up utils.get for the common cases
| -rw-r--r-- | discord/utils.py | 32 |
1 files changed, 21 insertions, 11 deletions
diff --git a/discord/utils.py b/discord/utils.py index 8303916c..35ef6a12 100644 --- a/discord/utils.py +++ b/discord/utils.py @@ -34,6 +34,7 @@ import datetime from email.utils import parsedate_to_datetime import functools from inspect import isawaitable as _isawaitable +from operator import attrgetter import json import re import warnings @@ -247,19 +248,28 @@ def get(iterable, **attrs): Keyword arguments that denote attributes to search with. """ - def predicate(elem): - for attr, val in attrs.items(): - nested = attr.split('__') - obj = elem - for attribute in nested: - obj = getattr(obj, attribute) - - if obj != val: - return False - return True + # global -> local + _all = all + attrget = attrgetter + + # Special case the single element call + if len(attrs) == 1: + k, v = attrs.popitem() + pred = attrget(k.replace('__', '.')) + for elem in iterable: + if pred(elem) == v: + return elem + return None - return find(predicate, iterable) + converted = [ + (attrget(attr.replace('__', '.')), value) + for attr, value in attrs.items() + ] + for elem in iterable: + if _all(pred(elem) == value for pred, value in converted): + return elem + return None def _unique(iterable): seen = set() |