Conditions | 17 |
Total Lines | 134 |
Code Lines | 99 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like tests.timed_events.get_timed_events() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | from PyDMXControl import Colors |
||
10 | def get_timed_events(controller: Controller) -> TimedEvents: |
||
11 | events = TimedEvents(True) |
||
12 | |||
13 | # Add audio |
||
14 | |||
15 | def run_audio(): |
||
16 | player.play("you-will-be-found.mp3") |
||
17 | player.sleep_till_done() |
||
18 | |||
19 | events.add_run_callback(run_audio) |
||
20 | |||
21 | # Define some events |
||
22 | bpm = 87 |
||
23 | bpm_millis = (1 / bpm) * 60 * 1000 |
||
24 | |||
25 | def a0(): |
||
26 | controller.all_color(Colors.Black) |
||
27 | controller.all_on() |
||
28 | controller.all_color([0, 0, 15], 12000) |
||
29 | |||
30 | def a12400(): |
||
31 | controller.all_on(2000) |
||
32 | controller.get_fixtures_by_name("S1 Art Left")[0].color([50, 100, 255], 2000) |
||
33 | controller.get_fixtures_by_name("S3 Art Right")[0].color([50, 100, 255], 2000) |
||
34 | |||
35 | def a18900(): |
||
36 | controller.get_fixtures_by_name("F1 Desk Right")[0].color([160, 140, 255], 2000) |
||
37 | controller.get_fixtures_by_name("F2 Desk Left")[0].color([160, 140, 255], 2000) |
||
38 | |||
39 | def a45000(): |
||
40 | for fixture in controller.get_fixtures_by_profile(LED_Par_36): |
||
41 | fixture.color([0, 150, 255]) |
||
42 | fixture.color([0, 0, 255], 15000) |
||
43 | |||
44 | def a69000(): |
||
45 | for fixture in controller.get_fixtures_by_profile(LED_Par_10mm): |
||
46 | fixture.color(Colors.White, 5000) |
||
47 | |||
48 | for fixture in controller.get_fixtures_by_profile(Small_Flat_Par): |
||
49 | fixture.color([0, 128, 255], 5000) |
||
50 | |||
51 | def a93000(): |
||
52 | for fixture in controller.get_fixtures_by_profile(LED_Par_10mm): |
||
53 | fixture.color([0, 25, 255], 5000) |
||
54 | |||
55 | for fixture in controller.get_fixtures_by_profile(LED_Par_36): |
||
56 | fixture.color(Colors.White, 5000) |
||
57 | |||
58 | for fixture in controller.get_fixtures_by_profile(Small_Flat_Par): |
||
59 | fixture.color(Colors.White, 5000) |
||
60 | |||
61 | def a125000(): |
||
62 | for fixture in controller.get_fixtures_by_profile(LED_Par_10mm): |
||
63 | fixture.color(Colors.Blue, 5000) |
||
64 | |||
65 | for fixture in controller.get_fixtures_by_profile(LED_Par_36): |
||
66 | fixture.color(Colors.Black, 5000) |
||
67 | |||
68 | for fixture in controller.get_fixtures_by_profile(Small_Flat_Par): |
||
69 | fixture.color(Colors.Black, 5000) |
||
70 | |||
71 | def a138000(): |
||
72 | for fixture in controller.get_fixtures_by_profile(LED_Par_36): |
||
73 | fixture.dim(0) |
||
74 | fixture.dim(255, 15000) |
||
75 | |||
76 | Chase.group_apply(controller.get_fixtures_by_profile(LED_Par_36), bpm_millis * 4, colors=[ |
||
77 | [50, 128, 255], Colors.Black, Colors.Black, Colors.Black]) |
||
78 | |||
79 | def a152000(): |
||
80 | controller.clear_all_effects() |
||
81 | controller.all_color(Colors.Blue) |
||
82 | controller.all_color([50, 100, 255], 2000) |
||
83 | |||
84 | def a174000(): |
||
85 | for fixture in controller.get_fixtures_by_profile(LED_Par_10mm): |
||
86 | fixture.color(Colors.Blue, 8000) |
||
87 | |||
88 | for fixture in controller.get_fixtures_by_profile(Small_Flat_Par): |
||
89 | fixture.color(Colors.Blue, 8000) |
||
90 | |||
91 | def a216000(): |
||
92 | controller.all_color([100, 128, 255]) |
||
93 | |||
94 | for fixture in controller.get_fixtures_by_profile(LED_Par_10mm): |
||
95 | fixture.color(Colors.Blue, 5000) |
||
96 | |||
97 | def a248900(): |
||
98 | c = [100, 128, 255] |
||
99 | controller.all_color(c) |
||
100 | |||
101 | Chase.group_apply(controller.get_fixtures_by_profile(LED_Par_36), bpm_millis * 4, colors=[c, Colors.Blue, c, c]) |
||
102 | |||
103 | def a270000(): |
||
104 | controller.clear_all_effects() |
||
105 | |||
106 | for fixture in controller.get_fixtures_by_profile(LED_Par_36): |
||
107 | fixture.color(Colors.Blue, 5000) |
||
108 | |||
109 | def a292300(): |
||
110 | for fixture in controller.get_fixtures_by_profile(LED_Par_36): |
||
111 | fixture.color([128, 128, 255], 5000) |
||
112 | |||
113 | for fixture in controller.get_fixtures_by_profile(LED_Par_10mm): |
||
114 | fixture.color(Colors.Blue, 5000) |
||
115 | |||
116 | def a334000(): |
||
117 | controller.get_fixtures_by_name("S1 Art Left")[0].color([50, 100, 255], 1000) |
||
118 | controller.get_fixtures_by_name("S3 Art Right")[0].color([50, 100, 255], 1000) |
||
119 | |||
120 | # Store events |
||
121 | events.add_event(0, a0, name="Start") |
||
122 | events.add_event(12400, a12400) |
||
123 | events.add_event(16800, controller.all_color, Colors.Blue, 1000) |
||
124 | events.add_event(18900, a18900) |
||
125 | events.add_event(38000, controller.all_color, Colors.Blue, 1000) |
||
126 | events.add_event(45000, a45000) |
||
127 | events.add_event(69000, a69000) |
||
128 | events.add_event(93000, a93000) |
||
129 | events.add_event(125000, a125000) |
||
130 | events.add_event(138000, a138000) |
||
131 | events.add_event(152000, a152000) |
||
132 | events.add_event(174000, a174000) |
||
133 | events.add_event(216000, a216000) |
||
134 | events.add_event(248900, a248900) |
||
135 | events.add_event(270000, a270000) |
||
136 | events.add_event(292300, a292300) |
||
137 | events.add_event(303000, controller.all_color, Colors.White, 500) |
||
138 | events.add_event(333200, controller.all_color, Colors.Blue, 800) |
||
139 | events.add_event(334000, a334000) |
||
140 | events.add_event(344000, controller.all_color, Colors.Blue, 1000) |
||
141 | events.add_event(354000, controller.all_off, 500) |
||
142 | |||
143 | return events |
||
144 |