Passed
Pull Request — master (#367)
by
unknown
02:46
created

utils.batch()   A

Complexity

Conditions 3

Size

Total Lines 8
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nop 2
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
import asyncio
2
import hashlib
3
import logging
4
import pickle
5
6
from itertools import chain, islice
7
8
9
_logger = logging.getLogger(__name__)
10
11
12
class ClientNotFound(Exception):
13
    pass
14
15
16
async def event_wait(evt, timeout) -> bool:
17
    try:
18
        await asyncio.wait_for(evt.wait(), timeout)
19
    except asyncio.TimeoutError:
20
        pass
21
    return evt.is_set()
22
23
24
def get_digest(conf) -> str:
25
    return hashlib.md5(pickle.dumps(conf)).hexdigest()
26
27
28
def batch(iterable, size):
29
    iterator = iter(iterable)
30
    while True:
31
        try:
32
            batchiter = islice(iterator, size)
33
            yield list(chain([next(batchiter)], batchiter))
34
        except StopIteration:
35
            break
36