From 89a418a3886d3a96cb77965eacdc1078dbb38dbf Mon Sep 17 00:00:00 2001 From: Rapptz Date: Wed, 6 Jan 2016 23:40:20 -0500 Subject: [PATCH] Add __slots__ for missing classes that didn't have it. --- discord/message.py | 11 ++++++++--- discord/server.py | 8 ++++++-- discord/utils.py | 22 ++++++++++++++++++++++ 3 files changed, 36 insertions(+), 5 deletions(-) diff --git a/discord/message.py b/discord/message.py index a610bd360..3db7bdb30 100644 --- a/discord/message.py +++ b/discord/message.py @@ -87,6 +87,11 @@ class Message: A list of attachments given to a message. """ + __slots__ = [ 'edited_timestamp', 'timestamp', 'tts', 'content', 'channel', + 'mention_everyone', 'embeds', 'id', 'mentions', 'author', + 'channel_mentions', 'server', '_raw_mentions', 'attachments', + '_clean_content', '_raw_channel_mentions' ] + def __init__(self, **kwargs): # at the moment, the timestamps seem to be naive so they have no time zone and operate on UTC time. # we can use this to our advantage to use strptime instead of a complicated parsing routine. @@ -124,7 +129,7 @@ class Message: if channel is not None: self.channel_mentions.append(channel) - @utils.cached_property + @utils.cached_slot_property('_raw_mentions') def raw_mentions(self): """A property that returns an array of user IDs matched with the syntax of <@user_id> in the message content. @@ -134,7 +139,7 @@ class Message: """ return re.findall(r'<@([0-9]+)>', self.content) - @utils.cached_property + @utils.cached_slot_property('_raw_channel_mentions') def raw_channel_mentions(self): """A property that returns an array of channel IDs matched with the syntax of <#channel_id> in the message content. @@ -144,7 +149,7 @@ class Message: """ return re.findall(r'<#([0-9]+)>', self.content) - @utils.cached_property + @utils.cached_slot_property('_clean_content') def clean_content(self): """A property that returns the content in a "cleaned up" manner. This basically means that mentions are transformed diff --git a/discord/server.py b/discord/server.py index e04049be8..92e2a3873 100644 --- a/discord/server.py +++ b/discord/server.py @@ -84,6 +84,10 @@ class Server(Hashable): Check the :func:`on_server_unavailable` and :func:`on_server_available` events. """ + __slots__ = [ 'afk_timeout', 'afk_channel', 'members', 'channels', 'icon', + 'name', 'id', 'owner', 'unavailable', 'name', 'me', 'region', + '_default_role', '_default_channel' ] + def __init__(self, **kwargs): self.channels = [] self.owner = None @@ -157,12 +161,12 @@ class Server(Hashable): for obj in guild.get('voice_states', []): self._update_voice_state(obj) - @utils.cached_property + @utils.cached_slot_property('_default_role') def default_role(self): """Gets the @everyone role that all members have by default.""" return utils.find(lambda r: r.is_everyone, self.roles) - @utils.cached_property + @utils.cached_slot_property('_default_channel') def default_channel(self): """Gets the default :class:`Channel` for the server.""" return utils.find(lambda c: c.is_default, self.channels) diff --git a/discord/utils.py b/discord/utils.py index 169bf1b18..5318b631a 100644 --- a/discord/utils.py +++ b/discord/utils.py @@ -46,6 +46,28 @@ class cached_property: return value +class CachedSlotProperty: + def __init__(self, name, function): + self.name = name + self.function = function + self.__doc__ = getattr(function, '__doc__') + + def __get__(self, instance, owner): + if instance is None: + return self + + try: + return getattr(instance, self.name) + except AttributeError: + value = self.function(instance) + setattr(instance, self.name, value) + return value + +def cached_slot_property(name): + def decorator(func): + return CachedSlotProperty(name, func) + return decorator + def parse_time(timestamp): if timestamp: return datetime.datetime(*map(int, re_split(r'[^\d]', timestamp.replace('+00:00', ''))))