|
6 | 6 | """Utility function model.""" |
7 | 7 | from __future__ import annotations |
8 | 8 |
|
9 | | -import asyncio |
10 | 9 | import stat |
11 | 10 | import time |
12 | 11 | from collections.abc import Iterator |
13 | 12 | from datetime import date, datetime, timedelta |
14 | | -from functools import wraps |
15 | 13 | from hashlib import md5 |
16 | 14 | from inspect import isfunction |
17 | | -from itertools import chain, islice, product |
| 15 | +from itertools import product |
18 | 16 | from pathlib import Path |
19 | 17 | from random import randrange |
20 | 18 | from typing import Any, Final, Optional, TypeVar, Union, overload |
@@ -258,34 +256,6 @@ def cross_product(matrix: Matrix) -> Iterator[DictData]: |
258 | 256 | ) |
259 | 257 |
|
260 | 258 |
|
261 | | -def batch(iterable: Union[Iterator[Any], range], n: int) -> Iterator[Any]: |
262 | | - """Batch data into iterators of length n. The last batch may be shorter. |
263 | | -
|
264 | | - Example: |
265 | | - >>> for b in batch(iter('ABCDEFG'), 3): |
266 | | - ... print(list(b)) |
267 | | - ['A', 'B', 'C'] |
268 | | - ['D', 'E', 'F'] |
269 | | - ['G'] |
270 | | -
|
271 | | - :param iterable: |
272 | | - :param n: (int) A number of returning batch size. |
273 | | -
|
274 | | - :rtype: Iterator[Any] |
275 | | - """ |
276 | | - if n < 1: |
277 | | - raise ValueError("n must be at least one") |
278 | | - |
279 | | - it: Iterator[Any] = iter(iterable) |
280 | | - while True: |
281 | | - chunk_it = islice(it, n) |
282 | | - try: |
283 | | - first_el = next(chunk_it) |
284 | | - except StopIteration: |
285 | | - return |
286 | | - yield chain((first_el,), chunk_it) |
287 | | - |
288 | | - |
289 | 259 | def cut_id(run_id: str, *, num: int = 6) -> str: |
290 | 260 | """Cutting running ID with length. |
291 | 261 |
|
@@ -325,24 +295,3 @@ def dump_all( |
325 | 295 | elif isinstance(value, BaseModel): |
326 | 296 | return value.model_dump(by_alias=by_alias) |
327 | 297 | return value |
328 | | - |
329 | | - |
330 | | -def awaitable(func): |
331 | | - """Dynamic function to async or not depend on the called statement.""" |
332 | | - |
333 | | - @wraps(func) |
334 | | - async def async_wrapper(*args, **kwargs): |
335 | | - return func(*args, **kwargs) |
336 | | - |
337 | | - @wraps(func) |
338 | | - def sync_wrapper(*args, **kwargs): |
339 | | - return func(*args, **kwargs) |
340 | | - |
341 | | - def dispatch(*args, **kwargs): |
342 | | - try: |
343 | | - asyncio.get_running_loop() |
344 | | - return async_wrapper(*args, **kwargs) |
345 | | - except RuntimeError: |
346 | | - return sync_wrapper(*args, **kwargs) |
347 | | - |
348 | | - return dispatch |
0 commit comments