CelestialSurveyor

Форк
0
/
progress_bar.py 
125 строк · 3.1 Кб
1
import tqdm
2

3
from abc import ABC, abstractmethod
4
from wx import Gauge
5

6

7
class AbstractProgressBar(ABC):
8
    @abstractmethod
9
    def update(self, num: int = 1):
10
        """
11
        Update the progress bar by the given number of units.
12
        :param num: An integer representing the number of units to update.
13
        """
14
        pass
15

16
    @abstractmethod
17
    def complete(self):
18
        """
19
        Mark the progress bar as complete.
20
        """
21
        pass
22

23
    @abstractmethod
24
    def clear(self):
25
        """
26
        Clear the progress bar from the console or user interface.
27
        """
28
        pass
29

30
    @abstractmethod
31
    def set_description(self, description: str):
32
        """
33
        Set a description for the progress bar.
34
        :param description: A string describing the progress bar.
35
        """
36
        pass
37

38
    @abstractmethod
39
    def set_total(self, total: int):
40
        """
41
        Set the total number of units for the progress bar.
42
        :param total: An integer representing the total number of units.
43
        """
44
        pass
45

46

47
class ProgressBarCli(AbstractProgressBar):
48
    """
49
    This class represents a command-line progress bar.
50
    """
51
    def __init__(self):
52

53
        self.progress_bar = None
54

55
    def update(self, num: int = 1):
56

57
        self.progress_bar.update()
58

59
    def complete(self):
60
        self.progress_bar.close()
61
        self.progress_bar.clear()
62
        self.progress_bar = None
63

64
    def clear(self):
65
        self.progress_bar.clear()
66

67
    def set_description(self, description: str):
68
        self.progress_bar.set_description(description)
69

70
    def set_total(self, total: int):
71
        self.progress_bar = tqdm.tqdm(total=total)
72
        self.progress_bar.display()
73

74
    def _draw(self):
75
        pass
76

77

78
class ProgressBarGui(AbstractProgressBar):
79
    """
80
    This class represents a WxPython progress bar used in UI.
81
    """
82
    def __init__(self, progress_bar: Gauge):
83
        self.progress_bar = progress_bar
84
        self.progress_bar.SetValue(0)
85

86
    def update(self, num: int = 1):
87
        self.progress_bar.SetValue(self.progress_bar.GetValue() + num)
88

89
    def complete(self):
90
        pass
91

92
    def clear(self):
93
        self.progress_bar.SetValue(0)
94

95
    def set_description(self, description: str):
96
        pass
97

98
    def set_total(self, total: int):
99
        self.progress_bar.SetRange(total)
100

101
    def _draw(self):
102
        pass
103

104

105
class ProgressBarFactory:
106
    """
107
    A factory class for creating different types of progress bars.
108
    """
109
    @staticmethod
110
    def create_progress_bar(progress_bar_instance) -> AbstractProgressBar:
111
        """
112
        Create and return a specific type of progress bar based on the given instance.
113

114
        Args:
115
            progress_bar_instance (SharedMemoryParams): An instance of a progress bar.
116

117
        Returns:
118
            tAbstractProgressBar: An AbstractProgressBar instance representing a specific type of progress bar.
119
        """
120
        if isinstance(progress_bar_instance, tqdm.tqdm):
121
            return ProgressBarCli()
122
        elif isinstance(progress_bar_instance, Gauge):
123
            return ProgressBarGui(progress_bar_instance)
124
        else:
125
            raise ValueError("Invalid progress bar type")
126

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.