Skip to content

Commit aefa313

Browse files
committed
Add enum module
1 parent 5b3a586 commit aefa313

11 files changed

+248
-0
lines changed

module_enum/__init__.py

Whitespace-only changes.

module_enum/enum_aliases.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import enum
2+
3+
4+
class BugStatus(enum.Enum):
5+
new = 7
6+
incomplete = 6
7+
invalid = 5
8+
wont_fix = 4
9+
in_progress = 3
10+
fix_committed = 2
11+
fix_released = 1
12+
# Enum members with the same value are tracked as alias references to the same member object
13+
by_design = 4
14+
closed = 1
15+
16+
17+
# Aliases do not cause repeated values to be present in the iterator for the Enum
18+
# The canonical name for a member is the first name attached to the value
19+
for status in BugStatus:
20+
print('{:15} = {}'.format(status.name, status.value))
21+
22+
23+
print('\nSame: by_design is wont_fix: ', BugStatus.by_design is BugStatus.wont_fix)
24+
print('Same: closed is fix_released: ', BugStatus.closed is BugStatus.fix_released)
25+

module_enum/enum_comparison.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import enum
2+
3+
4+
class BugStatus(enum.Enum):
5+
new = 7
6+
incomplete = 6
7+
invalid = 5
8+
wont_fix = 4
9+
in_progress = 3
10+
fix_committed = 2
11+
fix_released = 1
12+
13+
14+
actual_state = BugStatus.wont_fix
15+
desired_state = BugStatus.fix_released
16+
17+
print('Equality: ', actual_state == desired_state, actual_state == BugStatus.wont_fix)
18+
print('Identity: ', actual_state is desired_state, actual_state is BugStatus.wont_fix)
19+
print('Ordered by value: ')
20+
21+
try:
22+
print('\n'.join(' ' + s.name for s in sorted(BugStatus)))
23+
except TypeError as err:
24+
print('Cannot sort: {}'.format(err))
25+

module_enum/enum_complex_values.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import enum
2+
3+
4+
class BugStatus(enum.Enum):
5+
new = {
6+
'num': 7,
7+
'transitions': [
8+
'incomplete',
9+
'invalid',
10+
'wont_fix',
11+
'in_progress',
12+
]
13+
}
14+
incomplete = {
15+
'num': 6,
16+
'transitions': ['new', 'wont_fix']
17+
}
18+
invalid = {
19+
'num': 5,
20+
'transitions': ['new']
21+
}
22+
wont_fix = {
23+
'num': 4,
24+
'transitions': ['new']
25+
}
26+
in_progress = {
27+
'num': 3,
28+
'transitions': [
29+
'new',
30+
'fix_committed',
31+
]
32+
}
33+
fix_committed = {
34+
'num': 2,
35+
'transitions': [
36+
'in_progress', 'fix_released'
37+
]
38+
}
39+
fix_released = {
40+
'num': 1,
41+
'transitions': [
42+
'new'
43+
]
44+
}
45+
46+
def __init__(self, vals):
47+
self.num = vals['num']
48+
self.transitions = vals['transitions']
49+
50+
def can_transition(self, new_state):
51+
return new_state.name in self.transitions
52+
53+
54+
print('Name: ', BugStatus.in_progress)
55+
print('Value: ', BugStatus.in_progress.value)
56+
print('Custom attribute: ', BugStatus.in_progress.transitions)
57+
print('Using attribute: ', BugStatus.in_progress.can_transition(BugStatus.new))
58+

module_enum/enum_create.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import enum
2+
3+
4+
class BugStatus(enum.Enum):
5+
new = 7
6+
incomplete = 6
7+
invalid = 5
8+
wont_fix = 4
9+
in_progress = 3
10+
fix_committed = 2
11+
fix_released = 1
12+
13+
14+
print('\nMember name: {}'.format(BugStatus.wont_fix.name))
15+
print('Member value: {}'.format(BugStatus.wont_fix.value))
16+
print('Type: {}'.format(type(BugStatus.wont_fix)))

module_enum/enum_intenum.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import enum
2+
3+
4+
class BugStatus(enum.IntEnum):
5+
new = 7
6+
incomplete = 6
7+
invalid = 5
8+
wont_fix = 4
9+
in_progress = 3
10+
fix_committed = 2
11+
fix_released = 1
12+
13+
14+
print('Ordered by value: ')
15+
print('\n'.join(' ' + s.name for s in sorted(BugStatus)))
16+

module_enum/enum_iterate.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import enum
2+
3+
4+
class BugStatus(enum.Enum):
5+
new = 7
6+
incomplete = 6
7+
invalid = 5
8+
wont_fix = 4
9+
in_progress = 3
10+
fix_committed = 2
11+
fix_released = 1
12+
13+
14+
for status in BugStatus:
15+
print('{:15} = {}'.format(status.name, status.value))
16+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import enum
2+
3+
4+
BugStatus = enum.Enum(
5+
value='BugStatus',
6+
names='fix_released fix_committed in_progress wont_fix invalid incomplete new'
7+
)
8+
9+
print('Member: {}'.format(BugStatus.new))
10+
11+
print('\nAll members: ')
12+
for status in BugStatus:
13+
print('{:15} = {}'.format(status.name, status.value))
14+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import enum
2+
3+
4+
BugStatus = enum.Enum(
5+
value='BugStatus',
6+
names=[
7+
('new', 7),
8+
('incomplete', 6),
9+
('invalid', 5),
10+
('wont_fix', 4),
11+
('in_progress', 3),
12+
('fix_committed', 2),
13+
('fix_released', 1),
14+
]
15+
)
16+
17+
print('All members: ')
18+
for status in BugStatus:
19+
print('{:15} = {}'.format(status.name, status.value))
20+

module_enum/enum_tuple_values.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import enum
2+
3+
4+
class BugStatus(enum.Enum):
5+
new = (7, [
6+
'incomplete',
7+
'invalid',
8+
'wont_fix',
9+
'in_progress',
10+
])
11+
incomplete = (6, [
12+
'new',
13+
'wont_fix',
14+
])
15+
invalid = (5, [
16+
'new'
17+
])
18+
wont_fix = (4, [
19+
'new'
20+
])
21+
in_progress = (3, [
22+
'new', 'fix_committed',
23+
])
24+
fix_committed = (2, [
25+
'in_progress', 'fix_released'
26+
])
27+
fix_released = (1, ['new'])
28+
29+
def __init__(self, num, transitions):
30+
self.num = num
31+
self.transitions = transitions
32+
33+
def can_transition(self, new_state):
34+
return new_state.name in self.transitions
35+
36+
37+
print('Name: ', BugStatus.in_progress)
38+
print('Value: ', BugStatus.in_progress.value)
39+
print('Custom attribute: ', BugStatus.in_progress.transitions)
40+
print('Using attribute: ', BugStatus.in_progress.can_transition(BugStatus.new))
41+

0 commit comments

Comments
 (0)