|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +""" |
| 4 | +Batch processing utilities |
| 5 | +""" |
| 6 | + |
| 7 | +from abc import ABC, abstractmethod |
| 8 | +from typing import Any, Callable, Iterable, List, MutableSequence, Tuple |
| 9 | + |
| 10 | + |
| 11 | +class BaseProcessor(ABC): |
| 12 | + |
| 13 | + # init with lambda's context ? |
| 14 | + |
| 15 | + @abstractmethod |
| 16 | + def _prepare(self): |
| 17 | + """ |
| 18 | + Prepare context manager. |
| 19 | + """ |
| 20 | + raise NotImplementedError() |
| 21 | + |
| 22 | + @abstractmethod |
| 23 | + def _clean(self): |
| 24 | + """ |
| 25 | + Clear context manager. |
| 26 | + """ |
| 27 | + raise NotImplementedError() |
| 28 | + |
| 29 | + @abstractmethod |
| 30 | + def _process_record(self): |
| 31 | + raise NotImplementedError() |
| 32 | + |
| 33 | + def process(self) -> List[Tuple]: |
| 34 | + return [self._process_record(record) for record in self.records] |
| 35 | + |
| 36 | + def __enter__(self): |
| 37 | + self._prepare() |
| 38 | + return self |
| 39 | + |
| 40 | + def __exit__(self, exception_type, exception_value, traceback): |
| 41 | + self._clean() |
| 42 | + |
| 43 | + def __call__(self, records: Iterable[Any], handler: Callable): |
| 44 | + """ |
| 45 | + Set instance attributes before execution |
| 46 | +
|
| 47 | + Parameters |
| 48 | + ---------- |
| 49 | +
|
| 50 | + records: Iterable[Any] |
| 51 | + Iterable with objects to be processed. |
| 52 | + handler: Callable |
| 53 | + Callable to process "records" entries. |
| 54 | + """ |
| 55 | + self.records = records |
| 56 | + self.handler = handler |
| 57 | + return self |
| 58 | + |
| 59 | + |
| 60 | +class BasePartialProcessor(BaseProcessor): |
| 61 | + |
| 62 | + success_messages: MutableSequence = None |
| 63 | + fail_messages: MutableSequence = None |
| 64 | + |
| 65 | + def success_handler(self, record, result): |
| 66 | + """ |
| 67 | + Success callback |
| 68 | + """ |
| 69 | + entry = (result, record) |
| 70 | + self.success_messages.append(record) |
| 71 | + return entry |
| 72 | + |
| 73 | + def failure_handler(self, record, error): |
| 74 | + """ |
| 75 | + Failure callback |
| 76 | + """ |
| 77 | + entry = (error, record) |
| 78 | + self.fail_messages.append(entry) |
| 79 | + return entry |
0 commit comments