1

If I have an enum class set up like this


class fruits(enum.IntEnum):
    apples = 0
    bananas = 1  # deprecated
    pears = 2  # deprecated
    strawberries = 3

Is there a way to dynamically get the enum's that are not deprecated (basically only fetch apples and strawberries? They're only marked via comment, and I prefer not to create a set of "deprecated" notifs

Michael
  • 776
  • 4
  • 19

2 Answers2

5

You'll need some extra code to support that use-case. I'll show it using aenum1:

from aenum import IntEnum

class Fruits(IntEnum):
    _init_ = 'value active'
    #
    apples = 0, True
    bananas = 1, False  # deprecated
    pears = 2, False    # deprecated
    strawberries = 3, True
    #
    @classmethod
    def active(cls):
        return [m for m in cls if m.active]
    #
    @classmethod
    def deprecated(cls):
        return [m for m in cls if not m.active]

and in use:

>>> list(Fruits)
[<Fruits.apples: 0>, <Fruits.bananas: 1>, <Fruits.pears: 2>, <Fruits.strawberries: 3>]

>>> Fruits.apples
<Fruits.apples: 0>

>>> Fruits.bananas
<Fruits.bananas: 1>

>>> Fruits.active()
[<Fruits.apples: 0>, <Fruits.strawberries: 3>]

>>> Fruits.deprecated()
[<Fruits.bananas: 1>, <Fruits.pears: 2>]

1 Disclosure: I am the author of the Python stdlib Enum, the enum34 backport, and the Advanced Enumeration (aenum) library (a drop-in replacement for the stdlib enum).

Ethan Furman
  • 63,992
  • 20
  • 159
  • 237
1

The answer is no, not with this setup
comments will not modify anything about your code and they are solely for user use.

Hippolippo
  • 803
  • 1
  • 5
  • 28