File: graph.py

package info (click to toggle)
bumblebee-status 2.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,844 kB
  • sloc: python: 13,430; sh: 68; makefile: 29
file content (196 lines) | stat: -rw-r--r-- 5,392 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
MAX_PERCENTS = 100.0


class Bar(object):
    bars = None

    def __init__(self, value):
        self.value = value


class HBar(Bar):
    bars = [
        "\u2581",
        "\u2582",
        "\u2583",
        "\u2584",
        "\u2585",
        "\u2586",
        "\u2587",
        "\u2588",
    ]

    """This class is a helper class used to draw horizontal bars - please use hbar directly

    :param value: percentage value to draw (float, between 0 and 100)
    """

    def __init__(self, value):
        super(HBar, self).__init__(value)
        self.step = MAX_PERCENTS / len(HBar.bars)

    def get_char(self):
        """Returns the character representing the current object's value

        :return: character representing the value passed during initialization
        :rtype: string with one character
        """
        for i in range(len(HBar.bars)):
            left = i * self.step
            right = (i + 1) * self.step
            if left <= self.value < right:
                return self.bars[i]
        return self.bars[-1]


def hbar(value):
    """"Retrieves the horizontal bar character representing the input value

    :param value: percentage value to draw (float, between 0 and 100)
    :return: character representing the value passed during initialization
    :rtype: string with one character
    """
    return HBar(value).get_char()


class VBar(Bar):
    bars = [
        "\u258f",
        "\u258e",
        "\u258d",
        "\u258c",
        "\u258b",
        "\u258a",
        "\u2589",
        "\u2588",
    ]

    """This class is a helper class used to draw vertical bars - please use vbar directly

    :param value: percentage value to draw (float, between 0 and 100)
    :param width: maximum width of the bar in characters
    """

    def __init__(self, value, width=1):
        super(VBar, self).__init__(value)
        self.step = MAX_PERCENTS / (len(VBar.bars) * width)
        self.width = width

    """Returns the characters representing the current object's value

    :return: characters representing the value passed during initialization
    :rtype: string
    """

    def get_chars(self):
        if self.value == 100:
            return self.bars[-1] * self.width
        if self.width == 1:
            for i in range(len(VBar.bars)):
                left = i * self.step
                right = (i + 1) * self.step
                if left <= self.value < right:
                    return self.bars[i]
        else:
            full_parts = int(self.value // (self.step * len(Vbar.bars)))
            remainder = self.value - full_parts * self.step * CHARS
            empty_parts = self.width - full_parts
            if remainder >= 0:
                empty_parts -= 1
            part_vbar = VBar(remainder * self.width)  # scale to width
            chars = self.bars[-1] * full_parts
            chars += part_vbar.get_chars()
            chars += " " * empty_parts
            return chars


def vbar(value, width):
    """Returns the characters representing the current object's value

    :param value: percentage value to draw (float, between 0 and 100)
    :param width: maximum width of the bar in characters

    :return: characters representing the value passed during initialization
    :rtype: string
    """
    return VBar(value, width).get_chars()


class BrailleGraph(object):
    chars = {
        (0, 0): " ",
        (1, 0): "\u2840",
        (2, 0): "\u2844",
        (3, 0): "\u2846",
        (4, 0): "\u2847",
        (0, 1): "\u2880",
        (0, 2): "\u28a0",
        (0, 3): "\u28b0",
        (0, 4): "\u28b8",
        (1, 1): "\u28c0",
        (2, 1): "\u28c4",
        (3, 1): "\u28c6",
        (4, 1): "\u28c7",
        (1, 2): "\u28e0",
        (2, 2): "\u28e4",
        (3, 2): "\u28e6",
        (4, 2): "\u28e7",
        (1, 3): "\u28f0",
        (2, 3): "\u28f4",
        (3, 3): "\u28f6",
        (4, 3): "\u28f7",
        (1, 4): "\u28f8",
        (2, 4): "\u28fc",
        (3, 4): "\u28fe",
        (4, 4): "\u28ff",
    }

    """This class is a helper class used to draw braille graphs - please use braille directly

    :param values: values to draw
    """

    def __init__(self, values):
        self.values = values
        # length of values list must be even
        # because one Braille char displays two values
        if len(self.values) % 2 == 1:
            self.values.append(0)
        self.steps = self.get_steps()
        self.parts = [tuple(self.steps[i : i + 2]) for i in range(len(self.steps))[::2]]

    @staticmethod
    def get_height(value, unit):
        if value < unit / 10.0:
            return 0
        elif value <= unit:
            return 1
        elif value <= unit * 2:
            return 2
        elif value <= unit * 3:
            return 3
        else:
            return 4

    def get_steps(self):
        maxval = max(self.values)
        unit = maxval / 4.0
        if unit == 0:
            return [0] * len(self.values)
        stepslist = []
        for value in self.values:
            stepslist.append(self.get_height(value, unit))
        return stepslist

    def get_chars(self):
        chars = []
        for part in self.parts:
            chars.append(BrailleGraph.chars[part])
        return "".join(chars)


def braille(values):
    return BrailleGraph(values).get_chars()


# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4