Browse Source

Most data classes now support hashing.

pull/60/head
Rapptz 9 years ago
parent
commit
51d91c2a82
  1. 10
      discord/channel.py
  2. 5
      discord/colour.py
  3. 6
      discord/invite.py
  4. 4
      discord/mixins.py
  5. 5
      discord/permissions.py
  6. 6
      discord/role.py
  7. 6
      discord/server.py
  8. 5
      discord/user.py

10
discord/channel.py

@ -28,11 +28,11 @@ from . import utils
from .permissions import Permissions from .permissions import Permissions
from .enums import ChannelType from .enums import ChannelType
from collections import namedtuple from collections import namedtuple
from .mixins import EqualityComparable from .mixins import Hashable
Overwrites = namedtuple('Overwrites', 'id allow deny type') Overwrites = namedtuple('Overwrites', 'id allow deny type')
class Channel(EqualityComparable): class Channel(Hashable):
"""Represents a Discord server channel. """Represents a Discord server channel.
Supported Operations: Supported Operations:
@ -44,6 +44,8 @@ class Channel(EqualityComparable):
+-----------+---------------------------------------+ +-----------+---------------------------------------+
| x != y | Checks if two channels are not equal. | | x != y | Checks if two channels are not equal. |
+-----------+---------------------------------------+ +-----------+---------------------------------------+
| hash(x) | Returns the channel's hash. |
+-----------+---------------------------------------+
| str(x) | Returns the channel's name. | | str(x) | Returns the channel's name. |
+-----------+---------------------------------------+ +-----------+---------------------------------------+
@ -196,7 +198,7 @@ class Channel(EqualityComparable):
return base return base
class PrivateChannel(EqualityComparable): class PrivateChannel(Hashable):
"""Represents a Discord private channel. """Represents a Discord private channel.
Supported Operations: Supported Operations:
@ -208,6 +210,8 @@ class PrivateChannel(EqualityComparable):
+-----------+-------------------------------------------------+ +-----------+-------------------------------------------------+
| x != y | Checks if two channels are not equal. | | x != y | Checks if two channels are not equal. |
+-----------+-------------------------------------------------+ +-----------+-------------------------------------------------+
| hash(x) | Returns the channel's hash. |
+-----------+-------------------------------------------------+
| str(x) | Returns the string "Direct Message with <User>" | | str(x) | Returns the string "Direct Message with <User>" |
+-----------+-------------------------------------------------+ +-----------+-------------------------------------------------+

5
discord/colour.py

@ -39,6 +39,8 @@ class Colour(object):
+-----------+----------------------------------------+ +-----------+----------------------------------------+
| x != y | Checks if two colours are not equal. | | x != y | Checks if two colours are not equal. |
+-----------+----------------------------------------+ +-----------+----------------------------------------+
| hash(x) | Return the colour's hash. |
+-----------+----------------------------------------+
| str(x) | Returns the hex format for the colour. | | str(x) | Returns the hex format for the colour. |
+-----------+----------------------------------------+ +-----------+----------------------------------------+
@ -63,6 +65,9 @@ class Colour(object):
def __str__(self): def __str__(self):
return '#' + format(self.value, 'x') return '#' + format(self.value, 'x')
def __hash__(self):
return hash(self.value)
@property @property
def r(self): def r(self):
"""Returns the red component of the colour.""" """Returns the red component of the colour."""

6
discord/invite.py

@ -26,9 +26,9 @@ DEALINGS IN THE SOFTWARE.
from .user import User from .user import User
from .utils import parse_time from .utils import parse_time
from .mixins import EqualityComparable from .mixins import Hashable
class Invite(EqualityComparable): class Invite(Hashable):
"""Represents a Discord :class:`Server` or :class:`Channel` invite. """Represents a Discord :class:`Server` or :class:`Channel` invite.
Depending on the way this object was created, some of the attributes can Depending on the way this object was created, some of the attributes can
@ -43,6 +43,8 @@ class Invite(EqualityComparable):
+-----------+--------------------------------------+ +-----------+--------------------------------------+
| x != y | Checks if two invites are not equal. | | x != y | Checks if two invites are not equal. |
+-----------+--------------------------------------+ +-----------+--------------------------------------+
| hash(x) | Return the invite's hash. |
+-----------+--------------------------------------+
| str(x) | Returns the invite's URL. | | str(x) | Returns the invite's URL. |
+-----------+--------------------------------------+ +-----------+--------------------------------------+

4
discord/mixins.py

@ -32,3 +32,7 @@ class EqualityComparable:
if isinstance(other, self.__class__): if isinstance(other, self.__class__):
return other.id != self.id return other.id != self.id
return True return True
class Hashable(EqualityComparable):
def __hash__(self):
return hash(self.id)

5
discord/permissions.py

@ -36,6 +36,8 @@ class Permissions(object):
+-----------+------------------------------------------+ +-----------+------------------------------------------+
| x != y | Checks if two permissions are not equal. | | x != y | Checks if two permissions are not equal. |
+-----------+------------------------------------------+ +-----------+------------------------------------------+
| hash(x) | Return the permission's hash. |
+-----------+------------------------------------------+
Attributes Attributes
----------- -----------
@ -57,6 +59,9 @@ class Permissions(object):
def __ne__(self, other): def __ne__(self, other):
return not self.__eq__(other) return not self.__eq__(other)
def __hash__(self):
return hash(self.value)
@classmethod @classmethod
def none(cls): def none(cls):
"""A factory method that creates a :class:`Permission` with all """A factory method that creates a :class:`Permission` with all

6
discord/role.py

@ -26,9 +26,9 @@ DEALINGS IN THE SOFTWARE.
from .permissions import Permissions from .permissions import Permissions
from .colour import Colour from .colour import Colour
from .mixins import EqualityComparable from .mixins import Hashable
class Role(EqualityComparable): class Role(Hashable):
"""Represents a Discord role in a :class:`Server`. """Represents a Discord role in a :class:`Server`.
Supported Operations: Supported Operations:
@ -40,6 +40,8 @@ class Role(EqualityComparable):
+-----------+------------------------------------+ +-----------+------------------------------------+
| x != y | Checks if two roles are not equal. | | x != y | Checks if two roles are not equal. |
+-----------+------------------------------------+ +-----------+------------------------------------+
| hash(x) | Return the role's hash. |
+-----------+------------------------------------+
| str(x) | Returns the role's name. | | str(x) | Returns the role's name. |
+-----------+------------------------------------+ +-----------+------------------------------------+

6
discord/server.py

@ -29,9 +29,9 @@ from .role import Role
from .member import Member from .member import Member
from .channel import Channel from .channel import Channel
from .enums import ServerRegion, Status from .enums import ServerRegion, Status
from .mixins import EqualityComparable from .mixins import Hashable
class Server(EqualityComparable): class Server(Hashable):
"""Represents a Discord server. """Represents a Discord server.
Supported Operations: Supported Operations:
@ -43,6 +43,8 @@ class Server(EqualityComparable):
+-----------+--------------------------------------+ +-----------+--------------------------------------+
| x != y | Checks if two servers are not equal. | | x != y | Checks if two servers are not equal. |
+-----------+--------------------------------------+ +-----------+--------------------------------------+
| hash(x) | Returns the server's hash. |
+-----------+--------------------------------------+
| str(x) | Returns the server's name. | | str(x) | Returns the server's name. |
+-----------+--------------------------------------+ +-----------+--------------------------------------+

5
discord/user.py

@ -36,6 +36,8 @@ class User:
+-----------+------------------------------------+ +-----------+------------------------------------+
| x != y | Checks if two users are not equal. | | x != y | Checks if two users are not equal. |
+-----------+------------------------------------+ +-----------+------------------------------------+
| hash(x) | Return the user's hash. |
+-----------+------------------------------------+
| str(x) | Returns the user's name. | | str(x) | Returns the user's name. |
+-----------+------------------------------------+ +-----------+------------------------------------+
@ -66,6 +68,9 @@ class User:
def __ne__(self, other): def __ne__(self, other):
return not self.__eq__(other) return not self.__eq__(other)
def __hash__(self):
return hash(self.id)
@property @property
def avatar_url(self): def avatar_url(self):
"""Returns a friendly URL version of the avatar variable the user has. An empty string if """Returns a friendly URL version of the avatar variable the user has. An empty string if

Loading…
Cancel
Save