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