Skip to content Skip to sidebar Skip to footer

Get Next Enumerator Constant/property

Lets's say I have an enumerator, is it possible to get the property that follows? So if I had today=Days.Sunday would I be able to do something like tomorrow=today.next()? example:

Solution 1:

Absolutely.

Just add the desired functionality to your Days class:

classDays(Enum):

    Sunday = 'S'
    Monday = 'M'
    Tuesday = 'T'
    Wednesday = 'W'
    Thursday = 'Th'
    Friday = 'F'
    Saturday = 'Sa'defnext(self):
        cls = self.__class__
        members = list(cls)
        index = members.index(self) + 1if index >= len(members):
            index = 0return members[index]

and in use:

today = Days.Wednesday
print(today.next())
# Days.Thursday

While the above is probably easier to understand, it is possible to do the work once in __init__ by adding a next attribute to each member (and previous while we're at it).

classDays(Enum):
    #
    Sunday = 'S'
    Monday = 'M'
    Tuesday = 'T'
    Wednesday = 'W'
    Thursday = 'Th'
    Friday = 'F'
    Saturday = 'Sa'#def__init__(self, value):
        iflen(self.__class__):
            # make linksall = list(self.__class__)
            first, previous = all[0], all[-1]
            previous.next = self
            self.previous = previous
            self.next = first

and in use:

>>> Days.Tuesday.next
<Days.Wednesday: 'W'>

>>> Days.Tuesday.previous
<Days.Monday: 'M'>

>>> Days.Saturday.next
<Days.Sunday: 'S'>

>>> Days.Saturday.previous
<Days.Friday: 'F'>

NB Using the this method of attributes means we no longer need the ()s after next/previous.

Solution 2:

You can create a dictionary to lookup the next day like so:

In [10]: class Days(Enum):
    Sun = 'Su'
    Mon = 'M'
    Tue = 'Tu'
    Wed = 'W'
    Thu = 'Th'
    Fri = 'F'
    Sat = 'Sa'

In [11]: days = list(Days)

In [12]: nxt = dict((day, days[(i+1) % len(days)]) for i, day in enumerate(days))

Quick test:

In [13]: nxt[Days.Tue]
Out[13]: <Days.Wed: 'W'>In [14]: nxt[Days.Sat]
Out[14]: <Days.Sun: 'Su'>

Solution 3:

#ENUM CLASS#colorsimport enum

classColor(enum.Enum):
    turquoise = 1
    indigo = 2
    magenta = 3
    cyan = 4
    teal = 5
    azure = 6
    rose = 7
    amber = 8
    vermillon = 9
    plum = 10
    russet = 11
    slate = 12def__iter__(self):
        self.idx_current = self.value
        return self

    def__next__(self):
        if (self.idx_current > 12):
            returnNone

        self.idx_current = self.idx_current + 1return Color (self.idx_current - 1)

#CLIENT CODE#iterate colors starting from cyan
    it = iter (Color.cyan)
    whileTrue:
        #print (v.get_id())
        c = next (it)
        if c isNone:
            breakprint(c)

#OUTPUT
Color.cyan
Color.teal
Color.azure
Color.rose
Color.amber
Color.vermillon
Color.plum
Color.russet
Color.slate

Solution 4:

For me that seems like the most elegant solution without additional functions

day = Days.Sunday

day = Days((day.value + 1) % len(Days) + 1) # next day cycled

Post a Comment for "Get Next Enumerator Constant/property"