From 1acf478fb720e47b23dea81e86e15876a8e0ef72 Mon Sep 17 00:00:00 2001 From: Rapptz Date: Sun, 1 May 2016 07:22:45 -0400 Subject: [PATCH] Make Permissions an iterable class. --- discord/permissions.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/discord/permissions.py b/discord/permissions.py index bc74536c3..f7e262d16 100644 --- a/discord/permissions.py +++ b/discord/permissions.py @@ -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. @@ -75,6 +80,16 @@ class Permissions: 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):