Skip to content

Commit

Permalink
Make Permissions an iterable class.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rapptz committed May 1, 2016
1 parent fdaa429 commit 1acf478
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions discord/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ class Permissions:
+-----------+------------------------------------------+
| hash(x) | Return the permission's hash. |
+-----------+------------------------------------------+
| iter(x) | Returns an iterator of (perm, value) |
| | pairs. This allows this class to be used |
| | as an iterable in e.g. set/list/dict |
| | constructions. |
+-----------+------------------------------------------+
The properties provided are two way. You can set and retrieve individual bits using the properties as if they
were regular bools. This allows you to edit permissions.
Expand All @@ -75,6 +80,16 @@ def __ne__(self, other):
def __hash__(self):
return hash(self.value)

def _perm_iterator(self):
for attr in dir(self):
# check if it's a property, because if so it's a permission
is_property = isinstance(getattr(self.__class__, attr), property)
if is_property:
yield (attr, getattr(self, attr))

def __iter__(self):
return self._perm_iterator()

def is_subset(self, other):
"""Returns True if other has the same or fewer permissions as self."""
if isinstance(other, Permissions):
Expand Down

0 comments on commit 1acf478

Please sign in to comment.