Created
October 30, 2020 10:22
-
-
Save jshwi/fcc614c8a6098b10409746a3ea5489cb to your computer and use it in GitHub Desktop.
Progress bar for python scripts
This file contains hidden or 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
#!/usr/bin/env python3 | |
""" | |
pybar | |
===== | |
""" | |
# Copyright 2020 Stephen Whitlock | |
# | |
# 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. | |
__author__ = "Stephen Whitlock" | |
__maintainer__ = "Stephen Whitlock" | |
__email__ = "[email protected]" | |
__version__ = "1.0.0" | |
__copyright__ = "2019, Stephen Whitlock" | |
__license__ = "MIT" | |
from sys import stdout | |
from typing import Union | |
class ProgressBar: | |
"""Instantiate with the item being iterated (to indicate the maximum | |
number thus determining each iteration's percentage) and the length | |
of the bar | |
""" | |
def __init__(self, items: Union[dict, list, str], bar_: int = 70) -> None: | |
self.bar_len = bar_ | |
self.total = self.get_total(items) | |
@staticmethod | |
def convert_file(item: str, lines: list) -> list: | |
"""Read the entered file and compile each line into an index | |
in the returned list | |
:param item: The path of the file | |
:param lines: Empty list | |
:return: Each individual line in the file as a list | |
""" | |
with open(item, "r") as file: | |
for line in file.readlines(): | |
lines.append(line) | |
return lines | |
@staticmethod | |
def convert_dict(items: dict, keys: list) -> list: | |
"""Compile a list of the dictionary's keys | |
:param items: The entered dictionary | |
:param keys: Empty list | |
:return: The keys of the dictionary as a list | |
""" | |
for key in items.items(): | |
keys.append(key) | |
return keys | |
def get_total(self, items: Union[dict, list, str]) -> int: | |
"""Sort parameters by type and organise them all into a list | |
so that the length can be discovered | |
:param items: A string (file path), dictionary or list | |
:return: The length of the object | |
""" | |
if isinstance(items, str): | |
items = self.convert_file(items, []) | |
return self.get_total(items) | |
elif isinstance(items, dict): | |
items = self.convert_dict(items, []) | |
return self.get_total(items) | |
elif isinstance(items, list): | |
return len(items) | |
@staticmethod | |
def display(bar: str, percent: int) -> None: | |
"""The progress bar that will be displayed | |
:param bar: The representation of progress | |
:param percent: The numbers demonstrating percentage completion | |
""" | |
stdout.write(f"[{bar}] {percent}%\r") | |
stdout.flush() | |
def bar(self, count: int) -> None: | |
"""Calculations occur here | |
:param count: The number of iterations that have occurred | |
""" | |
fill = "\033[1;32m#\033[0;0m" | |
filled_len = int(round(self.bar_len * count / float(self.total))) | |
percent = round((100.0 * count) / float(self.total), 1) | |
bar = (fill * filled_len) + " " * (self.bar_len - filled_len) | |
self.display(bar, percent) | |
def complete(self): | |
"""The completed progress bar - a good addition as sometimes | |
a script will complete too quickly to see the bar hit 100% | |
""" | |
self.bar(self.total) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment