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))
In [3]:
for status in BugStatus:
print('{:15}={}'.format(status.name, status.value))
In [4]:
actual_status = BugStatus.wont_fix
desired_status = BugStatus.fix_released
print('Equality:', actual_status==actual_status,
actual_status==desired_status)
In [5]:
print('Identity', actual_status is desired_status,
actual_status is BugStatus.wont_fix)
In [6]:
try:
print('\n'.join(' '+ s.name for s in sorted(BugStatus)))
except TypeError as errr:
print('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')
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))
In [12]:
print('Equality:', BugStatus.by_design == BugStatus.wont_fix)
In [13]:
print('Identity', BugStatus.closed is BugStatus.fix_released)
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))
In [16]:
print('\nAll Members')
for status in BugStatus:
print('{:15}={}'.format(status.name, status.value))
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))
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))