diff options
author | 2010-01-17 03:48:23 +0100 | |
---|---|---|
committer | 2010-01-17 03:48:23 +0100 | |
commit | 025a723a5a201009da392bca4c27c4eb25e9e734 (patch) | |
tree | 8a16a78a0326c78ab6cdca270623986bcc369dcd /progressbar.py | |
parent | fb65246575871e0129b80911c3610606884451b0 (diff) | |
download | 4grab-025a723a5a201009da392bca4c27c4eb25e9e734.tar.gz 4grab-025a723a5a201009da392bca4c27c4eb25e9e734.zip |
Parser seperation, progress bar
* Seperated the parser from the downloader code.
* Added a progressbar class, to make it look fancier
* Created some functions to do all the work in downloader.py, cleaner now
* Changed parser.py to htmlparser.py, since it was conflicting with a built-in module
Diffstat (limited to 'progressbar.py')
-rw-r--r-- | progressbar.py | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/progressbar.py b/progressbar.py new file mode 100644 index 0000000..a2ea711 --- /dev/null +++ b/progressbar.py @@ -0,0 +1,27 @@ +import sys +import time + +class Progress(): + def __init__(self, maxvalue, maxwidth=80, fd=sys.stdout): + self.maxwidth = maxwidth + self.maxvalue = maxvalue + self.fd = fd + self.fill_char = '#' + + self.show_progress(0) + + def show_progress(self, value): + str_value = str(value) + str_maxvalue = str(self.maxvalue) + true_maxwidth = self.maxwidth - 4 - len(str_value) - len(str_maxvalue) + progress = int(round((true_maxwidth/float(self.maxvalue))*value)) + self.fd.write("\r%s/%s [%s%s]" % (str_value, str_maxvalue, self.fill_char * progress, " " * (true_maxwidth - progress))) + self.fd.flush() + if value == self.maxvalue: + self.fd.write("\n") + +if __name__ == "__main__": + prog = Progress(200) + for i in range(1, 201): + prog.show_progress(i) + time.sleep(1) |