forked from sth-v/ptc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
72 lines (55 loc) · 1.62 KB
/
utils.py
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import sys
import os
from abc import ABC
from typing import Iterable, Iterator
def search(path, prefix="e57"):
dirs = os.scandir(path)
for d in dirs:
try:
name, pref = d.name.split('.')
if pref == prefix:
yield name
finally: continue
def progressbar(it, prefix="", size=60, out=sys.stdout): # Python3.6+
count = len(it)
def show(j):
x = int(size * j / count)
print(f"{prefix}{u'█' * x}{('.' * (size - x))} {j}/{count}", end='\r', file=out, flush=True)
show(0)
for i, item in enumerate(it):
yield item
show(i + 1)
print("\n", flush=True, file=out)
class Progresbar(Iterator):
def __iter__(self):
self.i += 1
return self
def __next__(self):
self.show()
return next(self.itr)
def __init__(self, itr, prefix="", size=60, out=sys.stdout):
self._itr = itr
self._prefix = prefix
self.size = size
self.out = out
self.i = 0
self._length=1
@property
def prefix(self):
return self._prefix
@prefix.setter
def prefix(self, val):
self._prefix = val
@property
def length(self):
return self._length
@length.setter
def length(self, v):
self._length = v
def show(self, i):
x = int(self.size * self.i / self.length)
print(f"{self.prefix}{u'█' * x}{('.' * (self.size - x))} {self.i}/{self.length}", end='\r', file=self.out,
flush=True)
def __call__(self, *args, **kwargs):
self.itr = self._itr(self, *args, **kwargs)
return self