openrvdas.logger.transforms.unique_transform

Pass the record to the next transform/writer only if the contents of the record have changed from the previous value.

 1#!/usr/bin/env python3
 2"""Pass the record to the next transform/writer only if the contents of the
 3record have changed from the previous value.
 4"""
 5
 6
 7from logger.transforms.transform import Transform  # noqa: E402
 8
 9
10################################################################################
11class UniqueTransform(Transform):
12    """Return the record only if it has changed from the previous value."""
13
14    def __init__(self, **kwargs):
15        super().__init__(**kwargs)  # processes 'quiet' and type hints
16
17        """Starts with an empty record."""
18        self.prev_record = ""
19
20    ############################
21    def transform(self, record: str) -> str:
22        # See if it's something we can process, and if not, try digesting
23        if not self.can_process_record(record):  # inherited from BaseModule()
24            return self.digest_record(record)  # inherited from BaseModule()
25
26        """If same as previous, return None, else record."""
27        if record == self.prev_record:
28            return None
29
30        self.prev_record = record
31
32        return record
class UniqueTransform(logger.transforms.transform.Transform):
12class UniqueTransform(Transform):
13    """Return the record only if it has changed from the previous value."""
14
15    def __init__(self, **kwargs):
16        super().__init__(**kwargs)  # processes 'quiet' and type hints
17
18        """Starts with an empty record."""
19        self.prev_record = ""
20
21    ############################
22    def transform(self, record: str) -> str:
23        # See if it's something we can process, and if not, try digesting
24        if not self.can_process_record(record):  # inherited from BaseModule()
25            return self.digest_record(record)  # inherited from BaseModule()
26
27        """If same as previous, return None, else record."""
28        if record == self.prev_record:
29            return None
30
31        self.prev_record = record
32
33        return record

Return the record only if it has changed from the previous value.

UniqueTransform(**kwargs)
15    def __init__(self, **kwargs):
16        super().__init__(**kwargs)  # processes 'quiet' and type hints
17
18        """Starts with an empty record."""
19        self.prev_record = ""
quiet - if type checking should log type errors or operate silently.

Two additional arguments govern how records will be encoded/decoded
from bytes, if desired by the Writer subclass when it calls
_encode_str() or _decode_bytes:

encoding - 'utf-8' by default. If empty or None, do not attempt any
        decoding and return raw bytes. Other possible encodings are
        listed in online documentation here:
        https://docs.python.org/3/library/codecs.html#standard-encodings

encoding_errors - 'ignore' by default. Other error strategies are
        'strict', 'replace', and 'backslashreplace', described here:
        https://docs.python.org/3/howto/unicode.html#encodings

mirror_to - Optional Writer to which all records read or transformed
        by this module (if it is a Reader or Transform) will be
        "mirrored" (copied). Mirroring happens asynchronously via
        a queue and background thread to minimize impact on the
        primary data flow. Writers cannot be mirrored.
prev_record
def transform(self, record: str) -> str:
22    def transform(self, record: str) -> str:
23        # See if it's something we can process, and if not, try digesting
24        if not self.can_process_record(record):  # inherited from BaseModule()
25            return self.digest_record(record)  # inherited from BaseModule()
26
27        """If same as previous, return None, else record."""
28        if record == self.prev_record:
29            return None
30
31        self.prev_record = record
32
33        return record