Conditions | 22 |
Total Lines | 98 |
Code Lines | 78 |
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 pypen.__main__.main() 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 | import sys |
||
93 | def main(arguments): |
||
94 | if not arguments.init: |
||
95 | try: |
||
96 | file_path = path.join(getcwd(), arguments.filename) |
||
97 | spec = import_util.spec_from_file_location("", file_path) |
||
98 | user_sketch = import_util.module_from_spec(spec) |
||
99 | spec.loader.exec_module(user_sketch) |
||
100 | except FileNotFoundError: |
||
101 | print() |
||
102 | print(f"Hmm, PyPen can't find '{arguments.filename}'.") |
||
103 | print("Are you sure that's the right path?") |
||
104 | print() |
||
105 | sys.exit(1) |
||
106 | |||
107 | try: |
||
108 | user_sketch.TIME |
||
|
|||
109 | user_sketch.T |
||
110 | user_sketch.rectangle |
||
111 | user_sketch.PI |
||
112 | except AttributeError: |
||
113 | print() |
||
114 | print(f"It seems like you're not importing PyPen to your sketch '{arguments.filename}'") |
||
115 | print("Import it by writing 'from pypen import *' at the very top!") |
||
116 | print() |
||
117 | sys.exit(1) |
||
118 | |||
119 | try: |
||
120 | user_sketch.start |
||
121 | except AttributeError: |
||
122 | settings._user_has_start = False |
||
123 | |||
124 | try: |
||
125 | user_sketch.update |
||
126 | except AttributeError: |
||
127 | settings._user_has_update = False |
||
128 | |||
129 | if not settings._user_has_start and not settings._user_has_update: |
||
130 | print() |
||
131 | print( |
||
132 | f"Your PyPen sketch '{arguments.filename}' appears to have neither a start() nor an update() function.") |
||
133 | print("Try to add at least one of those and run again!") |
||
134 | print() |
||
135 | sys.exit(1) |
||
136 | |||
137 | def start(): |
||
138 | user_sketch.start() |
||
139 | if settings._user_has_update: |
||
140 | update() |
||
141 | |||
142 | def update(passed_time=0, delta_time=0, frame_count=0): |
||
143 | if not settings._user_has_update: |
||
144 | return |
||
145 | user_sketch.TIME = user_sketch.T = passed_time |
||
146 | user_sketch.DELTA_TIME = user_sketch.DT = delta_time |
||
147 | user_sketch.FRAME = user_sketch.F = frame_count |
||
148 | |||
149 | user_sketch.update() |
||
150 | |||
151 | pygame.init() |
||
152 | pygame.display.set_caption(f"PyPen | {arguments.filename}") |
||
153 | |||
154 | pygame.display.set_mode((settings.width, settings.height), pygame.SRCALPHA) |
||
155 | |||
156 | if settings._user_has_start: |
||
157 | initial_settings = settings |
||
158 | start() |
||
159 | |||
160 | if settings.width != initial_settings.width or settings.height != initial_settings.height: |
||
161 | pygame.display.set_mode((settings.width, settings.height), pygame.SRCALPHA) |
||
162 | |||
163 | pygame.display.flip() |
||
164 | clock = pygame.time.Clock() |
||
165 | |||
166 | is_running = True |
||
167 | passed_time = frame_count = 0 |
||
168 | |||
169 | while is_running: |
||
170 | # Handle events |
||
171 | for event in pygame.event.get(): |
||
172 | if event.type == pygame.QUIT: |
||
173 | is_running = False |
||
174 | |||
175 | if event.type == pygame.KEYDOWN: |
||
176 | if event.key == pygame.K_ESCAPE: |
||
177 | is_running = False |
||
178 | |||
179 | delta_time = clock.tick(settings.fps if settings.fps > 0 else 1)/1000 |
||
180 | passed_time += delta_time |
||
181 | frame_count += 1 |
||
182 | |||
183 | if arguments.timeout > 0: |
||
184 | if passed_time > arguments.timeout: |
||
185 | is_running = False |
||
186 | |||
187 | if settings._user_has_update: |
||
188 | update(passed_time, delta_time, frame_count) |
||
189 | |||
190 | pygame.display.flip() |
||
191 | |||
195 |