forked from windelbouwman/lognplot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
35 lines (29 loc) · 696 Bytes
/
Copy pathutils.py
File metadata and controls
35 lines (29 loc) · 696 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import time
import contextlib
def chunk(sequence, chunk_size):
""" Split an iterable into batches of items.
"""
c = []
for item in sequence:
c.append(item)
if len(c) >= chunk_size:
yield c
c = []
if c:
yield c
def clip(value, minimum, maximum):
""" Clip a value between two other values. """
# assert minimum <= maximum
if value < minimum:
return minimum
elif value > maximum:
return maximum
else:
return value
@contextlib.contextmanager
def bench_it(name):
t1 = time.time()
yield
t2 = time.time()
duration = t2 - t1
print(f"{name} took {duration} seconds")