Skip to content

Commit fcc3d43

Browse files
committed
factory: use 3d-key-tuple dictionary rather than excessive if-then-else
1 parent 5558d49 commit fcc3d43

File tree

1 file changed

+14
-17
lines changed

1 file changed

+14
-17
lines changed

transitions/extensions/factory.py

+14-17
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,7 @@ def get_predefined(graph=False, nested=False, locked=False):
3030
3131
Returns (class): A machine class with the specified features.
3232
"""
33-
if graph and nested and locked:
34-
cls = LockedHierarchicalGraphMachine
35-
elif locked and nested:
36-
cls = LockedHierarchicalMachine
37-
elif locked and graph:
38-
cls = LockedGraphMachine
39-
elif nested and graph:
40-
cls = HierarchicalGraphMachine
41-
elif graph:
42-
cls = GraphMachine
43-
elif nested:
44-
cls = HierarchicalMachine
45-
elif locked:
46-
cls = LockedMachine
47-
else:
48-
cls = Machine
49-
return cls
33+
return _class_map[(graph, nested, locked)]
5034

5135

5236
class NestedGraphTransition(TransitionGraphSupport, NestedTransition):
@@ -95,3 +79,16 @@ class LockedHierarchicalGraphMachine(GraphMachine, LockedMachine, HierarchicalMa
9579

9680
transition_cls = NestedGraphTransition
9781
event_cls = LockedNestedEvent
82+
83+
84+
# 3d tuple (graph, nested, locked)
85+
_class_map = {
86+
(False, False, False): Machine,
87+
(False, False, True): LockedMachine,
88+
(False, True, False): HierarchicalMachine,
89+
(False, True, True): LockedHierarchicalMachine,
90+
(True, False, False): GraphMachine,
91+
(True, False, True): LockedGraphMachine,
92+
(True, True, False): HierarchicalGraphMachine,
93+
(True, True, True): LockedHierarchicalGraphMachine
94+
}

0 commit comments

Comments
 (0)