From c212cbd39281f56cfd925862dc38c42442b796de Mon Sep 17 00:00:00 2001 From: Luna Mendes Date: Wed, 7 Nov 2018 17:53:31 -0300 Subject: [PATCH] pubsub.lazy_guild: change some instance vars to properties - utils: add index_by_func --- litecord/pubsub/lazy_guild.py | 26 +++++++++++++++++++------- litecord/utils.py | 10 ++++++++++ 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/litecord/pubsub/lazy_guild.py b/litecord/pubsub/lazy_guild.py index e8f7b30..8063383 100644 --- a/litecord/pubsub/lazy_guild.py +++ b/litecord/pubsub/lazy_guild.py @@ -12,6 +12,7 @@ from litecord.pubsub.dispatcher import Dispatcher from litecord.permissions import ( Permissions, overwrite_find_mix, get_permissions, role_permissions ) +from litecord.utils import index_by_func log = Logger(__name__) @@ -88,19 +89,30 @@ class GuildMemberList: self.guild_id = guild_id self.channel_id = channel_id - # a really long chain of classes to get - # to the storage instance... self.main = main_lg - self.storage = self.main.app.storage - self.presence = self.main.app.presence - self.state_man = self.main.app.state_manager - self.list = MemberList(None, None, None, None) - #: {session_id: set[list]} + #: store the states that are subscribed to the list + # type is{session_id: set[list]} self.state = defaultdict(set) + @property + def storage(self): + """Get the global :class:`Storage` instance.""" + return self.main.app.storage + + @property + def presence(self): + """Get the global :class:`PresenceManager` instance.""" + return self.main.app.presence + + @property + def state_man(self): + """Get the global :class:`StateManager` instance.""" + return self.main.app.state_manager + def _set_empty_list(self): + """Set the member list as being empty.""" self.list = MemberList(None, None, None, None) async def _init_check(self): diff --git a/litecord/utils.py b/litecord/utils.py index 3949194..1a2b676 100644 --- a/litecord/utils.py +++ b/litecord/utils.py @@ -27,3 +27,13 @@ async def task_wrapper(name: str, coro): def dict_get(mapping, key, default): """Return `default` even when mapping[key] is None.""" return mapping.get(key) or default + + +def index_by_func(function, indexable: iter) -> int: + """Search in an idexable and return the index number + for an iterm that has func(item) = True.""" + for index, item in indexable: + if function(item): + return index + + return None