Skip to content

Commit 638c329

Browse files
committed
specialize accepts a list of enumeration values, implements #43
1 parent a7b4582 commit 638c329

File tree

5 files changed

+49
-9
lines changed

5 files changed

+49
-9
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.1; 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.4.0
9+
enum-properties==1.5.0

doc/source/changelog.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ There is one minimally impactful breaking change in the 1.5.0 release:
1414
The 1.5.0 release includes two feature improvements:
1515

1616
* Implemented `Configurable behavior for matching none on symmetric fields <https://github.com/bckohan/enum-properties/issues/44>`_
17-
17+
* Implemented `Allow @specialize to accept a list of enumeration values. <https://github.com/bckohan/enum-properties/issues/43>`_
1818

1919
v1.4.0
2020
======

doc/source/usage.rst

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ decorator. For example:
236236
assert SpecializedEnum.TWO.method() == 'method_two()'
237237
assert SpecializedEnum.THREE.method() == 'method_three()'
238238
239-
The @specialize decorator works on @classmethods and @staticmethods as well,
239+
The :py:meth:`~enum_properties.specialize` decorator works on @classmethods and @staticmethods as well,
240240
but it must be the outermost decorator.
241241

242242
The undecorated method will apply to all members that lack a specialization:
@@ -281,6 +281,26 @@ lack the method.
281281
assert SpecializedEnum.THREE.method() == 'method_three()'
282282
283283
284+
:py:meth:`~enum_properties.specialize` will also accept a list so that
285+
multiple enumeration values can share the same specialization.
286+
287+
.. code-block:: python
288+
289+
class SpecializedEnum(EnumProperties):
290+
291+
ONE = 1
292+
TWO = 2
293+
THREE = 3
294+
295+
@specialize(TWO, THREE)
296+
def method(self):
297+
return 'shared()'
298+
299+
assert not hasattr(SpecializedEnum.ONE, 'method')
300+
assert SpecializedEnum.TWO.method() == 'shared()'
301+
assert SpecializedEnum.THREE.method() == 'shared()'
302+
303+
284304
Flags
285305
-----
286306

enum_properties/__init__.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,20 +122,20 @@ class _Specialized: # pylint: disable=R0903
122122
:param wrapped: The wrapped member function
123123
:param value: The value to specialize for
124124
"""
125-
def __init__(self, wrapped, value):
125+
def __init__(self, wrapped, values):
126126
self.wrapped = wrapped
127-
self.value = value
127+
self.values = values
128128

129129

130-
def specialize(value):
130+
def specialize(*values):
131131
"""
132132
A decorator to specialize a method for a given enumeration value.
133133
134-
:param value: The enumeration value to specialize
134+
:param values: The enumeration value(s) to specialize
135135
:return: A decorated specialized member method
136136
"""
137137
def specialize_decorator(method):
138-
return _Specialized(method, value)
138+
return _Specialized(method, values)
139139

140140
return specialize_decorator
141141

@@ -338,7 +338,8 @@ def __init__(self):
338338

339339
def __setitem__(self, key, value):
340340
if isinstance(value, _Specialized):
341-
self._specialized_.setdefault(value.value, {})[key] = value
341+
for en in value.values:
342+
self._specialized_.setdefault(en, {})[key] = value
342343
elif key in EnumPropertiesMeta.EXPECTED:
343344
dict.__setitem__(self, key, value)
344345
elif key in EnumPropertiesMeta.RESERVED:

enum_properties/tests/tests.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1913,6 +1913,25 @@ def test(self, count=3):
19131913
self.assertEqual(SpecializedEnum.THREE.test(), 'threethreethree')
19141914
self.assertEqual(SpecializedEnum('two').test(count=1), 'two')
19151915

1916+
def test_specialize_multiple_lists(self):
1917+
1918+
class SpecializedEnum(EnumProperties, s('label')):
1919+
ONE = 1, 'one'
1920+
TWO = 2, 'two'
1921+
THREE = 3, 'three'
1922+
1923+
@specialize(ONE)
1924+
def test(self, count=1):
1925+
return self.label * count
1926+
1927+
@specialize(TWO, THREE)
1928+
def test(self, count=2):
1929+
return self.label * count
1930+
1931+
self.assertEqual(SpecializedEnum.ONE.test(), 'one')
1932+
self.assertEqual(SpecializedEnum.TWO.test(), 'twotwo')
1933+
self.assertEqual(SpecializedEnum.THREE.test(), 'threethree')
1934+
19161935

19171936
class PerformanceAndMemoryChecks(TestCase):
19181937

0 commit comments

Comments
 (0)