Conditions | 6 |
Total Lines | 57 |
Code Lines | 25 |
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:
1 | # Copyright Pincer 2021-Present |
||
155 | async def loop_for( |
||
156 | self, |
||
157 | event_name: str, |
||
158 | check: Union[Callable[[Any], bool], None], |
||
159 | iteration_timeout: Union[float, None], |
||
160 | loop_timeout: Union[float, None], |
||
161 | ) -> Any: |
||
162 | """ |
||
163 | Parameters |
||
164 | ---------- |
||
165 | event_name : str |
||
166 | The type of event. It should start with `on_`. This is the same |
||
167 | name that is used for @Client.event. |
||
168 | check : Callable[[Any], bool] |
||
169 | This function only returns a value if this return true. |
||
170 | iteration_timeout: Union[float, None] |
||
171 | Amount of seconds before timeout. Timeouts are for each loop. |
||
172 | loop_timeout: Union[float, None] |
||
173 | Amount of seconds before the entire loop times out. The generator |
||
174 | will only raise a timeout error while it is waiting for an event. |
||
175 | |||
176 | Yields |
||
177 | ------ |
||
178 | Any |
||
179 | What the Discord API returns for this event. |
||
180 | """ |
||
181 | |||
182 | if not loop_timeout: |
||
183 | while True: |
||
184 | yield await self.wait_for(event_name, check, iteration_timeout) |
||
185 | |||
186 | loop = get_running_loop() |
||
187 | |||
188 | while True: |
||
189 | start_time = loop.time() |
||
190 | |||
191 | try: |
||
192 | yield await _wait_for( |
||
193 | self.wait_for( |
||
194 | event_name, |
||
195 | check, |
||
196 | iteration_timeout |
||
197 | ), |
||
198 | timeout=loop_timeout |
||
199 | ) |
||
200 | |||
201 | except TimeoutError: |
||
202 | raise TimeoutError( |
||
203 | "loop_for() timed out while waiting for an event" |
||
204 | ) |
||
205 | |||
206 | loop_timeout -= loop.time() - start_time |
||
207 | |||
208 | # loop_timeout can be below 0 if the user's code in the for loop |
||
209 | # takes longer than the time left in loop_timeout |
||
210 | if loop_timeout <= 0: |
||
211 | break |
||
212 |