Skip to content

Commit b9f03fc

Browse files
committed
closes #39 and fixes #40 Performance improvement 5x faster property reads and 30% reduction in memory footprint for enums
1 parent 1b54299 commit b9f03fc

File tree

4 files changed

+472
-33
lines changed

4 files changed

+472
-33
lines changed

doc/source/changelog.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ Change Log
55
v1.4.0
66
======
77

8+
* Implemented `Performance improvements <https://github.com/bckohan/enum-properties/issues/39>`_
89
* Implemented `Provide a decorator to provide function overrides per enum value. <https://github.com/bckohan/enum-properties/issues/36>`_
910
* Fixed `Address python 3.11+ deprecation warnings. <https://github.com/bckohan/enum-properties/issues/38>`_
10-
* Fixed `New flag behavior modifiers break IntFlagProperties in python 3.11+ <https://github.com/bckohan/enum-properties/issues/38>`_
11+
* Fixed `New flag behavior modifiers break IntFlagProperties in python 3.11+ <https://github.com/bckohan/enum-properties/issues/37>`_
1112

1213

1314
v1.3.3

enum_properties/__init__.py

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -467,21 +467,14 @@ def add_coerce_type(typ):
467467
for val in cls:
468468
add_coerce_type(type(val.value))
469469

470-
for prop in cls.enum_properties:
471-
values = classdict._ep_properties_[prop]
472-
setattr(
473-
cls,
474-
f'_value2{prop}_map_',
475-
dict(zip(cls._value2member_map_, values))
476-
)
477-
setattr(
478-
cls,
479-
prop,
480-
property(
481-
lambda self, prop=prop:
482-
getattr(cls, f'_value2{prop}_map_').get(self.value)
470+
# set properties onto the members
471+
for idx, member in enumerate(cls.__members__.values()):
472+
for prop, values in classdict._ep_properties_.items():
473+
setattr(
474+
member,
475+
prop,
476+
values[idx]
483477
)
484-
)
485478

486479
# we reverse to maintain precedence order for symmetric lookups
487480
for prop in reversed([
@@ -625,6 +618,19 @@ class FlagProperties(
625618
A Flag that supports properties
626619
"""
627620

621+
def _generate_next_value_(name, start, count, last_values):
622+
"""
623+
Intermixed property tuples can corrupt the last_values list with
624+
tuples. This method ensures only ints are present in last_values and
625+
delegates to the super class.
626+
"""
627+
return enum.Flag._generate_next_value_(
628+
name,
629+
start,
630+
count,
631+
[val[0] if isinstance(val, tuple) else val for val in last_values]
632+
)
633+
628634

629635
class IntFlagProperties(
630636
DecomposeMixin,
@@ -635,3 +641,16 @@ class IntFlagProperties(
635641
"""
636642
An IntFlag that supports properties.
637643
"""
644+
645+
def _generate_next_value_(name, start, count, last_values):
646+
"""
647+
Intermixed property tuples can corrupt the last_values list with
648+
tuples. This method ensures only ints are present in last_values and
649+
delegates to the super class.
650+
"""
651+
return enum.IntFlag._generate_next_value_(
652+
name,
653+
start,
654+
count,
655+
[val[0] if isinstance(val, tuple) else val for val in last_values]
656+
)

0 commit comments

Comments
 (0)