-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprocess_bar.py
More file actions
43 lines (34 loc) · 1.22 KB
/
Copy pathprocess_bar.py
File metadata and controls
43 lines (34 loc) · 1.22 KB
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
36
37
38
39
40
41
42
43
# coding=utf-8
import datetime
import sys
import time
def progress_test(counts, lenfile, speed):
bar_length = 20
w = (lenfile - counts) * speed
eta = time.time() + w
precent = counts / float(lenfile)
ETA = datetime.datetime.fromtimestamp(eta)
hashes = '#' * int(precent * bar_length)
spaces = ' ' * (bar_length - len(hashes))
sys.stdout.write("""\r%d%%|%s|read %d projects|Speed : %.4f |ETA: %s """ % (
precent * 100, hashes + spaces, counts, speed, ETA))
# sys.stdout.write("\rthis spider has already read %d projects, speed: %.4f/projects" % (counts,f2-f1))
# sys.stdout.write("\rPercent: [%s] %d%%,remaining time: %.4f mins"%(hashes + spaces,precent,w))
sys.stdout.flush()
def process_bar(iterable_obj, counts=None):
if hasattr(iterable_obj, '__len__'):
counts = len(iterable_obj)
elif isinstance(counts, int):
pass # counts = counts
else:
iterable_obj = list(iterable_obj)
counts = len(iterable_obj)
for count, i in enumerate(iterable_obj):
f = time.time()
yield i
progress_test(count, counts, time.time() - f)
if __name__ == '__main__':
s = range(1000)
for si in process_bar(s):
pass
pass