Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

class Days(Enum):
     Sunday = 'S'
     Monday = 'M'
     ...
     Saturday = 'Sa'

I know I could use tuples (like below) to do something like tomorrow=today[1], but I was hoping there was something built in or more elegant.

class Days(Enum):
     Sunday = ('S','Monday')
     Monday = ('M','Tuesday')
     ...
     Saturday = ('Sa','Sunday')
like image 594
Parker Avatar asked Oct 26 '14 20:10

Parker


People also ask

How do I find the enum of a name?

Enum. GetName(Type, Object) Method is used to get the name of the constant in the specified enumeration that has the specified value. Syntax: public static string GetName (Type enumType, object value);

What is Auto in enum Python?

auto() method, we can get the assigned integer value automatically by just using enum. auto() method. Syntax : enum.auto() Automatically assign the integer value to the values of enum class attributes.

Can we create instance of enum?

You do not instantiate an enum , but rely the constants defined. Enums can be used in a switch-case statement, just like an int .

What is enum value?

An enumeration type (or enum type) is a value type defined by a set of named constants of the underlying integral numeric type. To define an enumeration type, use the enum keyword and specify the names of enum members: C# Copy.


2 Answers

Absolutely.

Just add the desired functionality to your Days class:

class Days(Enum):

    Sunday = 'S'
    Monday = 'M'
    Tuesday = 'T'
    Wednesday = 'W'
    Thursday = 'Th'
    Friday = 'F'
    Saturday = 'Sa'

    def next(self):
        cls = self.__class__
        members = list(cls)
        index = members.index(self) + 1
        if index >= len(members):
            index = 0
        return 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).

class Days(Enum):
    #
    Sunday = 'S'
    Monday = 'M'
    Tuesday = 'T'
    Wednesday = 'W'
    Thursday = 'Th'
    Friday = 'F'
    Saturday = 'Sa'
    #
    def __init__(self, value):
        if len(self.__class__):
            # make links
            all = 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.

like image 150
Ethan Furman Avatar answered Nov 10 '22 16:11

Ethan Furman


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'>
like image 2
Bas Swinckels Avatar answered Nov 10 '22 18:11

Bas Swinckels