| Conditions | 6 |
| Total Lines | 62 |
| Code Lines | 28 |
| 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 |
||
| 193 | async def loop_for( |
||
| 194 | self, |
||
| 195 | event_name: str, |
||
| 196 | check: Union[Callable[[Any], bool], None], |
||
| 197 | iteration_timeout: Union[float, None], |
||
| 198 | loop_timeout: Union[float, None], |
||
| 199 | ) -> Any: |
||
| 200 | """ |
||
| 201 | Parameters |
||
| 202 | ---------- |
||
| 203 | event_name : str |
||
| 204 | The type of event. It should start with `on_`. This is the same |
||
| 205 | name that is used for @Client.event. |
||
| 206 | check : Callable[[Any], bool] |
||
| 207 | This function only returns a value if this return true. |
||
| 208 | iteration_timeout: Union[float, None] |
||
| 209 | Amount of seconds before timeout. Timeouts are for each loop. |
||
| 210 | loop_timeout: Union[float, None] |
||
| 211 | Amount of seconds before the entire loop times out. The generator |
||
| 212 | will only raise a timeout error while it is waiting for an event. |
||
| 213 | |||
| 214 | Yields |
||
| 215 | ------ |
||
| 216 | Any |
||
| 217 | What the Discord API returns for this event. |
||
| 218 | """ |
||
| 219 | |||
| 220 | loop_mgr = _LoopMgr(event_name, check) |
||
| 221 | self.event_list.append(loop_mgr) |
||
| 222 | |||
| 223 | loop = get_running_loop() |
||
| 224 | |||
| 225 | while True: |
||
| 226 | start_time = loop.time() |
||
| 227 | |||
| 228 | try: |
||
| 229 | yield await _wait_for( |
||
| 230 | loop_mgr.get_next(), |
||
| 231 | timeout=_lowest_value( |
||
| 232 | loop_timeout, iteration_timeout |
||
| 233 | ) |
||
| 234 | ) |
||
| 235 | |||
| 236 | except TimeoutError: |
||
| 237 | # Loop timed out. Loop through the remaining events recieved |
||
| 238 | # before the timeout. |
||
| 239 | loop_mgr.can_expand = False |
||
| 240 | try: |
||
| 241 | while True: |
||
| 242 | yield await loop_mgr.get_next() |
||
| 243 | except __LoopEmptyError: |
||
| 244 | raise TimeoutError( |
||
| 245 | "loop_for() timed out while waiting for an event" |
||
| 246 | ) |
||
| 247 | |||
| 248 | loop_timeout -= loop.time() - start_time |
||
| 249 | |||
| 250 | # loop_timeout can be below 0 if the user's code in the for loop |
||
| 251 | # takes longer than the time left in loop_timeout |
||
| 252 | if loop_timeout <= 0: |
||
| 253 | raise TimeoutError( |
||
| 254 | "loop_for() timed out while waiting for an event" |
||
| 255 | ) |
||
| 256 |