Browse Source

Add __slots__ for missing classes that didn't have it.

pull/77/head
Rapptz 9 years ago
parent
commit
89a418a388
  1. 11
      discord/message.py
  2. 8
      discord/server.py
  3. 22
      discord/utils.py

11
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

8
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)

22
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', ''))))

Loading…
Cancel
Save