adding total keyword to progress and total time spent

This commit is contained in:
Ozbolt Menegatti 2019-07-03 14:54:23 +02:00
parent 771547b7e4
commit b25e3de76b
2 changed files with 10 additions and 8 deletions

View File

@ -85,7 +85,8 @@ class MatchStore:
def set_representations(self, word_renderer, structures): def set_representations(self, word_renderer, structures):
structures_dict = {s.id: s for s in structures} structures_dict = {s.id: s for s in structures}
for cid, sid in progress(self.db.execute("SELECT colocation_id, structure_id FROM Colocations"), "representations"): num_representations = int(self.db.execute("SELECT Count(*) FROM Colocations").fetchone()[0])
for cid, sid in progress(self.db.execute("SELECT colocation_id, structure_id FROM Colocations"), "representations", total=num_representations):
structure = structures_dict[sid] structure = structures_dict[sid]
match = StructureMatch.from_db(self.db, cid, structure) match = StructureMatch.from_db(self.db, cid, structure)
RepresentationAssigner.set_representations(match, word_renderer) RepresentationAssigner.set_representations(match, word_renderer)

View File

@ -9,23 +9,24 @@ except ImportError:
REPORT_ON = 0.3 REPORT_ON = 0.3
class Progress: class Progress:
def __call__(self, iterable, description): def __call__(self, iterable, description, total=None):
if tqdm is None: if tqdm is None:
try: try:
ln = len(iterable) total = len(iterable)
except TypeError: except TypeError:
ln = -1 total = -1
last_report = time.time() - REPORT_ON start_time = time.time()
last_report = start_time - REPORT_ON
for n, el in enumerate(iterable): for n, el in enumerate(iterable):
now = time.time() now = time.time()
if now - last_report > REPORT_ON: if now - last_report > REPORT_ON:
print("\r{}: {}/{}".format(description, n, ln), end="") print("\r{}: {}/{}".format(description, n, total), end="")
last_report = now last_report = now
yield el yield el
print("") print(" -> {}".format(time.time() - start_time))
else: else:
yield from tqdm(iterable, desc=description) yield from tqdm(iterable, desc=description, total=total)
progress = Progress() progress = Progress()