Skip to content

Commit f3fa37c

Browse files
committed
closes #16
1 parent e6a97d8 commit f3fa37c

File tree

6 files changed

+45
-4
lines changed

6 files changed

+45
-4
lines changed

doc/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ sphinxcontrib-htmlhelp==2.0.0; python_version >= "3.5"
66
sphinxcontrib-jsmath==1.0.1; python_version >= "3.5"
77
sphinxcontrib-qthelp==1.0.3; python_version >= "3.5"
88
sphinxcontrib-serializinghtml==1.1.5; python_version >= "3.5"
9-
enum-properties==1.2.1
9+
enum-properties==1.2.2

doc/source/changelog.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
Change Log
33
==========
44

5+
v1.2.2
6+
======
7+
8+
* Implemented `Add convenience property to decompose Flag enumeration values <https://github.com/bckohan/enum-properties/issues/16>`_
9+
510
v1.2.1
611
======
712

doc/source/usage.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,9 @@ IntFlag_ and Flag_ types that support properties are also provided by the
211211
# properties for combined flags, that are not listed will be None
212212
assert (Perm.R | Perm.W).label is None
213213
214+
# list the active flags:
215+
assert (Perm.R | Perm.W | Perm.X).flagged == [Perm.R, Perm.W, Perm.X]
216+
214217
Flag enumerations can also be created from iterables and generators containing
215218
values or symmetric values.
216219

enum_properties/__init__.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import unicodedata
2121
from collections.abc import Generator, Hashable, Iterable
2222

23-
VERSION = (1, 2, 1)
23+
VERSION = (1, 2, 2)
2424

2525
__title__ = 'Enum Properties'
2626
__version__ = '.'.join(str(i) for i in VERSION)
@@ -36,6 +36,7 @@
3636
'IntFlagProperties',
3737
'EnumPropertiesMeta',
3838
'SymmetricMixin',
39+
'DecomposeMixin',
3940
'p',
4041
's'
4142
]
@@ -492,13 +493,36 @@ class IntEnumProperties(
492493
"""
493494

494495

495-
class FlagProperties(SymmetricMixin, enum.Flag, metaclass=EnumPropertiesMeta):
496+
class DecomposeMixin: # pylint: disable=R0903
497+
"""
498+
A mixin for Flag enumerations that decomposes composite enumeration values
499+
into the constituent activated flags.
500+
"""
501+
502+
@property
503+
def flagged(self):
504+
"""
505+
Returns the list of flags that are activated.
506+
"""
507+
# do import here to keep lib less brittle b/c this is a private
508+
# include and may change, rely on CI to find issues
509+
from enum import _decompose # pylint: disable=C0415
510+
return _decompose(self.__class__, self._value_)[0]
511+
512+
513+
class FlagProperties(
514+
DecomposeMixin,
515+
SymmetricMixin,
516+
enum.Flag,
517+
metaclass=EnumPropertiesMeta
518+
):
496519
"""
497520
A Flag that supports properties
498521
"""
499522

500523

501524
class IntFlagProperties(
525+
DecomposeMixin,
502526
SymmetricMixin,
503527
enum.IntFlag,
504528
metaclass=EnumPropertiesMeta

enum_properties/tests/tests.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -961,6 +961,10 @@ class Perm(
961961
self.assertFalse(bool(Perm.R & Perm.X))
962962
self.assertIsNone((Perm.R & Perm.X).label)
963963

964+
self.assertCountEqual((Perm.R | Perm.W).flagged, [Perm.R, Perm.W])
965+
self.assertCountEqual(Perm.RWX.flagged, [Perm.R, Perm.W, Perm.X])
966+
self.assertEqual(Perm.R.flagged, [Perm.R])
967+
964968
def test_flag(self):
965969

966970
class Perm(
@@ -1002,3 +1006,8 @@ class Perm(
10021006

10031007
self.assertFalse(bool(Perm.R & Perm.X))
10041008
self.assertIsNone((Perm.R & Perm.X).label)
1009+
1010+
self.assertCountEqual((Perm.R | Perm.W).flagged, [Perm.R, Perm.W])
1011+
self.assertCountEqual(Perm.RWX.flagged, [Perm.R, Perm.W, Perm.X])
1012+
self.assertEqual(Perm.R.flagged, [Perm.R])
1013+
self.assertEqual((Perm.R & Perm.X).flagged, [Perm(0)])

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "enum-properties"
3-
version = "1.2.1"
3+
version = "1.2.2"
44
description = "Add properties to Python enumeration values with a simple declarative syntax."
55
authors = ["Brian Kohan <bckohan@gmail.com>"]
66
license = "MIT"

0 commit comments

Comments
 (0)