1 Creating Enumerations


In [1]:
import enum
class BugStatus(enum.Enum):
    new = 7
    incomplete = 6
    invalid = 5
    wont_fix = 4
    in_grogress = 3
    fix_committed = 2
    fix_released = 1

In [2]:
print('\nMember name:{}'.format(BugStatus.wont_fix.name))
print('Member value:{}'.format(BugStatus.wont_fix.value))


Member name:wont_fix
Member value:4

2 Iterations


In [3]:
for status in BugStatus:
    print('{:15}={}'.format(status.name, status.value))


new            =7
incomplete     =6
invalid        =5
wont_fix       =4
in_grogress    =3
fix_committed  =2
fix_released   =1

3 Comparision


In [4]:
actual_status = BugStatus.wont_fix
desired_status = BugStatus.fix_released
print('Equality:', actual_status==actual_status,
      actual_status==desired_status)


Equality: True False

In [5]:
print('Identity', actual_status is desired_status,
     actual_status is BugStatus.wont_fix)


Identity False True

In [6]:
try:
    print('\n'.join(' '+ s.name for s in sorted(BugStatus)))
except TypeError as errr:
    print('Cannot sorted')


Cannot sorted

using IntEnum can behave like number


In [8]:
import enum
class BugStatus(enum.IntEnum):
    new = 7
    incomplete = 6
    invalid = 5
    wont_fix = 4
    in_grogress = 3
    fix_committed = 2
    fix_released = 1
try:
    print('\n'.join(' '+ s.name for s in sorted(BugStatus)))
except TypeError as errr:
    print('Cannot sorted')


 fix_released
 fix_committed
 in_grogress
 wont_fix
 invalid
 incomplete
 new

4 Unique Enumeration Values


In [9]:
class BugStatus(enum.Enum):

    new = 7
    incomplete = 6
    invalid = 5
    wont_fix = 4
    in_progress = 3
    fix_committed = 2
    fix_released = 1

    by_design = 4
    closed = 1

In [10]:
for status in BugStatus:
    print('{:15}={}'.format(status.name, status.value))


new            =7
incomplete     =6
invalid        =5
wont_fix       =4
in_progress    =3
fix_committed  =2
fix_released   =1

In [12]:
print('Equality:', BugStatus.by_design == BugStatus.wont_fix)


Equality: True

In [13]:
print('Identity', BugStatus.closed is BugStatus.fix_released)


Identity True

5 Generate Enumerations Programmatically


In [15]:
import enum
BugStatus = enum.Enum(
    value='BugStatus',
    names="fix_released fix_committed in_progress wont_fix invalid incomplete new"
)
print('Member: {}'.format(BugStatus.new))


Member: BugStatus.new

In [16]:
print('\nAll Members')
for status in BugStatus:
    print('{:15}={}'.format(status.name, status.value))


All Members
fix_released   =1
fix_committed  =2
in_progress    =3
wont_fix       =4
invalid        =5
incomplete     =6
new            =7

In [17]:
import enum


BugStatus = enum.Enum(
    value='BugStatus',
    names=[
        ('new', 7),
        ('incomplete', 6),
        ('invalid', 5),
        ('wont_fix', 4),
        ('in_progress', 3),
        ('fix_committed', 2),
        ('fix_released', 1),
    ],
)

print('All members:')
for status in BugStatus:
    print('{:15} = {}'.format(status.name, status.value))


All members:
new             = 7
incomplete      = 6
invalid         = 5
wont_fix        = 4
in_progress     = 3
fix_committed   = 2
fix_released    = 1

6 Non-integer member values


In [18]:
import enum


class BugStatus(enum.Enum):

    new = (7, ['incomplete',
               'invalid',
               'wont_fix',
               'in_progress'])
    incomplete = (6, ['new', 'wont_fix'])
    invalid = (5, ['new'])
    wont_fix = (4, ['new'])
    in_progress = (3, ['new', 'fix_committed'])
    fix_committed = (2, ['in_progress', 'fix_released'])
    fix_released = (1, ['new'])

    def __init__(self, num, transitions):
        self.num = num
        self.transitions = transitions

    def can_transition(self, new_state):
        return new_state.name in self.transitions


print('Name:', BugStatus.in_progress)
print('Value:', BugStatus.in_progress.value)
print('Custom attribute:', BugStatus.in_progress.transitions)
print('Using attribute:',
      BugStatus.in_progress.can_transition(BugStatus.new))


Name: BugStatus.in_progress
Value: (3, ['new', 'fix_committed'])
Custom attribute: ['new', 'fix_committed']
Using attribute: True