Last active
August 29, 2015 14:03
-
-
Save tdgunes/cee25b31991f636abd40 to your computer and use it in GitHub Desktop.
Python Terminal A Progress bar
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#... | |
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() | |
#... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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