Skip to content

Instantly share code, notes, and snippets.

@tdgunes tdgunes/pipe.py
Last active Aug 29, 2015

Embed
What would you like to do?
Python Terminal A Progress bar
#...
class MongoProgressBarElement(FilterPipeElement):
def __init__(self, db_name, collection_name, sample):
self.total = pymongo.MongoClient()[db_name][collection_name].find().count()
self.count = 0
self.sample_count = 0
self.start = 0
self.operation_time = 0
self.total_operation = 0
self.operation_progress = 0
self.sample = sample
def stay(self, data):
self.count += 1
self.sample_count += 1
if self.sample_count == 1:
self.start = time.time()
elif self.sample_count == self.sample:
self.operation_time = float(str(time.time() - self.start)[:4]) / self.sample
self.total_operation = self.operation_time * self.total
self.sample_count = 0
self.update_progress(self.count, self.total)
self.operation_progress += self.operation_time
return True
def update_progress(self, done, total):
progress = done / float(total)
left = str(datetime.timedelta(seconds=self.total_operation-self.operation_progress))[:-4]
sys.stdout.write(
"\r| [{0:50s}] {1:.1f}% ({2}/{3} ) eta:{4}s pit:{5}".format('#' * int(progress * 50), progress * 100, done,
total, str(left), str(self.operation_time)))
sys.stdout.flush()
#...
import time,sys
def update_progress(done,total):
progress = done/float(total)
sys.stdout.write("\rProgress: [{0:50s}] {1:.1f}% ({2}/{3})".format('#' * int(progress * 50), progress * 100, done, total))
sys.stdout.flush()
start = time.time()
total = 9999
for i in range(0,total,1):
update_progress(i,total)
time.sleep(0.0001)
end = time.time()
print ""
print "Job is finished in:", str(end-start)[:4], "seconds!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.