aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMilo Mirate <[email protected]>2016-04-30 12:52:45 -0400
committerRapptz <[email protected]>2016-04-30 23:00:22 -0400
commit21c88cf727719f954a1b31dbae5b29e4bf61e4c8 (patch)
tree6832d40247c1d158a5ea4d05c962acd0810a852c
parentFix changing own nickname without manage_nicknames (diff)
downloaddiscord.py-21c88cf727719f954a1b31dbae5b29e4bf61e4c8.tar.xz
discord.py-21c88cf727719f954a1b31dbae5b29e4bf61e4c8.zip
Make Permissions partially-ordered.
Specifically: * P1 <= P2 iff P1 expresses a subset of the permissions expressed by P2. * P1 < P2 iff P1 <= P2 and P1 != P2 * vice versa for P1 >= P2 and P1 > P2
-rw-r--r--discord/permissions.py39
1 files changed, 39 insertions, 0 deletions
diff --git a/discord/permissions.py b/discord/permissions.py
index 8d0dbe02..bc74536c 100644
--- a/discord/permissions.py
+++ b/discord/permissions.py
@@ -36,6 +36,18 @@ class Permissions:
+-----------+------------------------------------------+
| x != y | Checks if two permissions are not equal. |
+-----------+------------------------------------------+
+ | x <= y | Checks if a permission is a subset |
+ | | of another permission. |
+ +-----------+------------------------------------------+
+ | x >= y | Checks if a permission is a superset |
+ | | of another permission. |
+ +-----------+------------------------------------------+
+ | x < y | Checks if a permission is a strict |
+ | | subset of another permission. |
+ +-----------+------------------------------------------+
+ | x > y | Checks if a permission is a strict |
+ | | superset of another permission. |
+ +-----------+------------------------------------------+
| hash(x) | Return the permission's hash. |
+-----------+------------------------------------------+
@@ -63,6 +75,33 @@ class Permissions:
def __hash__(self):
return hash(self.value)
+ def is_subset(self, other):
+ """Returns True if other has the same or fewer permissions as self."""
+ if isinstance(other, Permissions):
+ return (self.value & other.value) == self.value
+ else:
+ raise TypeError("cannot compare {} with {}".format(self.__class__.__name__, other.__class__name))
+
+ def is_superset(self, other):
+ """Returns True if other has the same or more permissions as self."""
+ if isinstance(other, Permissions):
+ return (self.value | other.value) == self.value
+ else:
+ raise TypeError("cannot compare {} with {}".format(self.__class__.__name__, other.__class__name))
+
+ def is_strict_subset(self, other):
+ """Returns True if the permissions on other are a strict subset of those on self."""
+ return self.is_subset(other) and self != other
+
+ def is_strict_superset(self, other):
+ """Returns True if the permissions on other are a strict superset of those on self."""
+ return self.is_superset(other) and self != other
+
+ __le__ = is_subset
+ __ge__ = is_superset
+ __lt__ = is_strict_subset
+ __gt__ = is_strict_superset
+
@classmethod
def none(cls):
"""A factory method that creates a :class:`Permissions` with all