CelestialSurveyor
125 строк · 3.1 Кб
1import tqdm2
3from abc import ABC, abstractmethod4from wx import Gauge5
6
7class AbstractProgressBar(ABC):8@abstractmethod9def update(self, num: int = 1):10"""11Update the progress bar by the given number of units.
12:param num: An integer representing the number of units to update.
13"""
14pass15
16@abstractmethod17def complete(self):18"""19Mark the progress bar as complete.
20"""
21pass22
23@abstractmethod24def clear(self):25"""26Clear the progress bar from the console or user interface.
27"""
28pass29
30@abstractmethod31def set_description(self, description: str):32"""33Set a description for the progress bar.
34:param description: A string describing the progress bar.
35"""
36pass37
38@abstractmethod39def set_total(self, total: int):40"""41Set the total number of units for the progress bar.
42:param total: An integer representing the total number of units.
43"""
44pass45
46
47class ProgressBarCli(AbstractProgressBar):48"""49This class represents a command-line progress bar.
50"""
51def __init__(self):52
53self.progress_bar = None54
55def update(self, num: int = 1):56
57self.progress_bar.update()58
59def complete(self):60self.progress_bar.close()61self.progress_bar.clear()62self.progress_bar = None63
64def clear(self):65self.progress_bar.clear()66
67def set_description(self, description: str):68self.progress_bar.set_description(description)69
70def set_total(self, total: int):71self.progress_bar = tqdm.tqdm(total=total)72self.progress_bar.display()73
74def _draw(self):75pass76
77
78class ProgressBarGui(AbstractProgressBar):79"""80This class represents a WxPython progress bar used in UI.
81"""
82def __init__(self, progress_bar: Gauge):83self.progress_bar = progress_bar84self.progress_bar.SetValue(0)85
86def update(self, num: int = 1):87self.progress_bar.SetValue(self.progress_bar.GetValue() + num)88
89def complete(self):90pass91
92def clear(self):93self.progress_bar.SetValue(0)94
95def set_description(self, description: str):96pass97
98def set_total(self, total: int):99self.progress_bar.SetRange(total)100
101def _draw(self):102pass103
104
105class ProgressBarFactory:106"""107A factory class for creating different types of progress bars.
108"""
109@staticmethod110def create_progress_bar(progress_bar_instance) -> AbstractProgressBar:111"""112Create and return a specific type of progress bar based on the given instance.
113
114Args:
115progress_bar_instance (SharedMemoryParams): An instance of a progress bar.
116
117Returns:
118tAbstractProgressBar: An AbstractProgressBar instance representing a specific type of progress bar.
119"""
120if isinstance(progress_bar_instance, tqdm.tqdm):121return ProgressBarCli()122elif isinstance(progress_bar_instance, Gauge):123return ProgressBarGui(progress_bar_instance)124else:125raise ValueError("Invalid progress bar type")126