|# The MIT License (MIT)|
|# Copyright (c) 2016 Vladimir Ignatev|
|# Permission is hereby granted, free of charge, to any person obtaining|
|# a copy of this software and associated documentation files (the "Software"),|
|# to deal in the Software without restriction, including without limitation|
|# the rights to use, copy, modify, merge, publish, distribute, sublicense,|
|# and/or sell copies of the Software, and to permit persons to whom the Software|
|# is furnished to do so, subject to the following conditions:|
|# The above copyright notice and this permission notice shall be included|
|# in all copies or substantial portions of the Software.|
|# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,|
|# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR|
|# PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE|
|# FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT|
|# OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE|
|# OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.|
|def progress(count, total, status=''):|
|bar_len = 60|
|filled_len = int(round(bar_len * count / float(total)))|
|percents = round(100.0 * count / float(total), 1)|
|bar = '=' * filled_len + '-' * (bar_len - filled_len)|
|sys.stdout.write('[%s] %s%s ...%s\r' % (bar, percents, '%', status))|
|sys.stdout.flush() # As suggested by Rom Ruben (see: http://stackoverflow.com/questions/3173320/text-progress-bar-in-the-console/27871113#comment50529068_27871113)|
I plan to have this code in a MediaWiki extension (https://gerrit.wikimedia.org/r/#/c/265529/2/captcha.py). However, the code is currently not licensed under a free software license (preferably a license which is compatible with GPL 2). Could you add such a license so I can use the code inside of this extension? :) Thanks!
I plan to use this code for a program I am running and will use your license in this.
How do I figure out how to know how I plan for len of my code so this program know, ok the program takes 12 seconds;
Is there a variable in python that tells me how it takes to run my program as it changes.
@errard70101 I did so, but I still get a new line whenever the percentage is increasing.
If I wanted go for 100% to 0%, what modifications would I have to do?
Never mind, I made that work.
I made a small improvement because I couldn't get it working, it wasn't printing anything cause of the
def progres(count, total, status='', bar_len=60): filled_len = int(round(bar_len * count / float(total))) percents = round(100.0 * count / float(total), 1) bar = '=' * filled_len + '-' * (bar_len - filled_len) fmt = '[%s] %s%s ...%s' % (bar, percents, '%', status) print('\b' * len(fmt), end='') # clears the line sys.stdout.write(fmt) sys.stdout.flush()
@vladignatyev Great snippet of code!! I will use this many times over.