summaryrefslogtreecommitdiff
path: root/py05/ex1/data_stream.py
blob: b457a24e8f34c7e7cc84aa9a600470c2ba8f2f06 (plain)
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#!/usr/bin/env python3

from abc import ABC, abstractmethod
import typing
import sys


class DataProcessor(ABC):
    def __init__(self, name: str) -> None:
        self.storage: typing.List[typing.Any] = []
        self.rank: int = 0
        self.counter: int = 0
        self.name = name

    @abstractmethod
    def validate(self, data: typing.Any) -> bool:
        pass

    @abstractmethod
    def ingest(self, data: typing.Any) -> None:
        pass

    def output(self) -> tuple[int, str]:
        # extract the oldest piece of data
        # piece of data is removed
        position = self.rank
        self.rank += 1
        if isinstance(self.storage[0], dict):
            entry = self.storage[0]
            output_tuple: tuple = tuple(entry.values())
            to_return = (f"{output_tuple[0]}: {output_tuple[1]}")
            self.storage.pop(0)
            return position, to_return
        to_return = self.storage[0]
        self.storage.pop(0)
        return position, to_return


class NumericProcessor(DataProcessor):

    def validate(self, data: int | float | typing.List[int | float]) -> bool:
        try:
            if isinstance(data, int):
                self.counter += 1
                return True
            elif isinstance(data, float):
                self.counter += 1
                return True
            elif isinstance(data, typing.List):
                type_list = "yes"
                for item in data:
                    self.counter += 1
                    if not isinstance(item, int | float):
                        type_list = "no"
                        self.counter -= 1
                if type_list == "yes":
                    return True
                else:
                    raise ValueError()
            else:
                raise ValueError()
        except ValueError:
            return False

    def ingest(self, data: int | float | typing.List[int | float]) -> None:
        try:
            if isinstance(data, typing.List):
                for item in data:
                    if isinstance(item, int | float):
                        self.storage.append(item)
            elif isinstance(data, int | float):
                self.storage.append(data)
            else:
                raise ValueError()
        except ValueError:
            print("Improper numeric data", file=sys.stderr)


def is_number(s: str) -> bool:
    try:
        float(s)
        return True
    except (ValueError, TypeError):
        return False


class TextProcessor(DataProcessor):

    def validate(self, data: str | typing.List[str]) -> bool:
        try:
            if isinstance(data, str):
                if is_number(data):
                    raise ValueError()
                else:
                    self.counter += 1
                    return True
            elif isinstance(data, typing.List):
                for item in data:
                    if isinstance(item, str):
                        if is_number(item):
                            raise ValueError()
                            return False
                        else:
                            self.counter += 1
                    if isinstance(item, dict):
                        raise ValueError()
                        return False
                return True
            else:
                raise ValueError()
        except ValueError:
            return False

    def ingest(self, data: str | typing.List[str]) -> None:
        try:
            if isinstance(data, str):
                self.storage.append(data)
            elif isinstance(data, typing.List):
                for item in data:
                    if is_number(item):
                        raise ValueError()
                    elif isinstance(item, dict):
                        raise ValueError()
                    else:
                        self.storage.append(item)
            else:
                raise ValueError()
        except ValueError:
            print("Improper text data", file=sys.stderr)


class LogProcessor(DataProcessor):

    def validate(self, data: dict | typing.List[dict]) -> bool:
        try:
            if isinstance(data, dict):
                return True
            if isinstance(data, typing.List):
                for item in data:
                    if isinstance(item, dict):
                        self.counter += 1
                    else:
                        raise ValueError()
                        return False
                return True
            else:
                raise ValueError()
        except ValueError:
            return False

    def ingest(self, data: dict | typing.List[dict]) -> None:
        try:
            if isinstance(data, dict):
                for item in data:
                    self.storage.append(item)
            elif isinstance(data, typing.List):
                for item in data:
                    if isinstance(item, dict):
                        self.storage.append(item)
            else:
                raise ValueError()
        except ValueError:
            print("Improper log data", file=sys.stderr)


class DataStream():
    def __init__(self) -> None:
        self.processor: list = []
        self.counter: int = 0

    def register_processor(self, proc: DataProcessor) -> None:
        # method that allows you to register a new data processor to process
        # the data stream.
        self.processor.append(proc)

    def process_stream(self, stream: list[typing.Any]) -> None:
        # method that will analyze each element of the list received as a
        # parameter and send it to the appropriate registered data processor.
        # Error messages will be printed if no data processor can handle an
        # element
        for item in stream:
            try:
                for processor in self.processor:
                    # route towards appropriate processors
                    if processor.validate(item):
                        processor.ingest(item)
                        # stop when appropriate processors is found
                        break
                else:
                    raise ValueError()
            except ValueError:
                print(f"Data Stream Error - Can't process "
                      f"element in stream: {item}", file=sys.stderr)

    def print_processors_stats(self) -> None:
        if self.processor == []:
            print("No processor found, no data")
        else:
            for processor in self.processor:
                remaining = len(processor.storage)
                print(f"{processor.name}: total {processor.counter} items "
                      f"processed, remaining {remaining} on processor")

    def consume_processors(self, rm_1: int, rm_2: int, rm_3: int) -> None:
        for processor in self.processor:
            if processor.name == "Numeric Processor":
                for _ in range(rm_1):
                    processor.output()
            elif processor.name == "Text Processor":
                for _ in range(rm_2):
                    processor.output()
            elif processor.name == "Log Processor":
                for _ in range(rm_3):
                    processor.output()


def main() -> None:

    # Create a test scenario that demonstrates the correct processing of a data
    # stream.  Display statistics on registered data processors, consume
    # elements using the output method of each data processor and show updated
    # statistics

    print("=== Code Nexus - Data Stream ===")
    print()
    print("Initialize Data Stream...")
    print()
    processors = DataStream()
    print("=== DataStream statistic ===")
    processors.print_processors_stats()
    print()
    # initialize processors
    is_num = NumericProcessor("Numeric Processor")
    print("Registering Numeric Processor")
    print()
    processors.register_processor(is_num)
    batch_one = ['Hello world',
                 [3.14, -1, 2.71],
                 [{
                    'log_level': 'WARNING',
                    ' log_message': 'Telnet access! Use ssh instead'
                  },
                  {
                    'log_level': 'INFO',
                    'log_message': 'User wil is connected'
                  }],
                 42,
                 ['Hi', 'five']]
    print(f"Sending first batch of data on stream: {batch_one}")
    processors.process_stream(batch_one)
    print()
    print("=== DataStream statistic ===")
    processors.print_processors_stats()
    print()
    print("Registering other data processors")
    is_text = TextProcessor("Text Processor")
    is_log = LogProcessor("Log Processor")
    processors.register_processor(is_text)
    processors.register_processor(is_log)
    print()
    print("Send the same batch again")
    processors.process_stream(batch_one)
    print()
    print("=== DataStream statistic ===")
    processors.print_processors_stats()
    print()
    consume_num = 3
    consume_text = 2
    consume_log = 1
    print(f"Consume some elements from the data processors:"
          f" Numeric {consume_num}, Text {consume_text}, Log {consume_log}")
    processors.consume_processors(consume_num, consume_text, consume_log)
    print("=== DataStream statistic ===")
    processors.print_processors_stats()


if __name__ == "__main__":
    main()