ahmet / Python-for-Data-Science
Herkese Açık Ahmet Kerem Piscine Final
Loading.py 1.1 KB
import os
def terminal_width() -> int:
"""Return the width of the terminal, 80 when it is unknown."""
try:
return os.get_terminal_size().columns
except OSError:
return 80
def draw_bar(done: int, total: int, width: int) -> None:
"""Draw the progress bar of done items over total on a single line."""
percent = done * 100 // total
filled = done * width // total
bar = "=" * max(filled - 1, 0) + ">" + " " * (width - max(filled, 1))
print(f"{percent:3}%|[{bar}]| {done}/{total}", end="\r", flush=True)
def ft_tqdm(lst: range):
"""Yield every item of lst while drawing a tqdm-like progress bar."""
total = len(lst)
if total == 0:
return
width = max(terminal_width() - len(f"100%|[]| {total}/{total}"), 1)
draw_bar(0, total, width)
for index, item in enumerate(lst):
yield item
draw_bar(index + 1, total, width)
def main() -> None:
"""Demonstrate the ft_tqdm progress bar."""
for _ in ft_tqdm(range(333)):
pass
print()
if __name__ == "__main__":
main()