From 9e20d71e60fe1c97cf548960cf289341e49eaba6 Mon Sep 17 00:00:00 2001 From: djkhl Date: Wed, 5 Jun 2024 13:40:45 +0200 Subject: [PATCH] tried making get_ordered_list prettier --- logprep/processor/field_manager/processor.py | 44 +++++++++----------- 1 file changed, 20 insertions(+), 24 deletions(-) diff --git a/logprep/processor/field_manager/processor.py b/logprep/processor/field_manager/processor.py index 701f13a1e..93efde915 100644 --- a/logprep/processor/field_manager/processor.py +++ b/logprep/processor/field_manager/processor.py @@ -168,9 +168,10 @@ def _write_to_single_target(self, args, extend_target_list, overwrite_target, ru def _overwrite_from_source_values(self, source_fields_values): lists, not_lists = self._separate_lists_form_other_types(source_fields_values) - flattened_source_fields = self._get_flatten_list(lists, not_lists) - ordered_list = self._get_ordered_flatten_list(flattened_source_fields, source_fields_values) - return ordered_list + ordered_flattened_list = self._get_ordered_flatten_list( + source_fields_values, lists, not_lists + ) + return ordered_flattened_list def _handle_missing_fields(self, event, rule, source_fields, field_values): if rule.ignore_missing_fields: @@ -186,6 +187,19 @@ def _handle_missing_fields(self, event, rule, source_fields, field_values): return True return False + def _get_ordered_flatten_list( + self, source_fields_values, lists: List[List], not_lists: List[Any] + ) -> List: + duplicates = [] + ordered_flatten_list = [] + flat_source_fields = self._get_flatten_source_fields(source_fields_values) + for field_value in flat_source_fields: + if field_value not in duplicates: + duplicates.append(field_value) + if field_value in list(itertools.chain(*lists, not_lists)): + ordered_flatten_list.append(field_value) + return ordered_flatten_list + @staticmethod def _get_field_values(event, source): return [get_dotted_field_value(event, source_field) for source_field in source] @@ -202,32 +216,14 @@ def _separate_lists_form_other_types(field_values: List[Any]) -> Tuple[List[List return field_values_lists, field_values_not_list @staticmethod - def _get_flatten_list(lists: List[List], not_lists: List[Any]) -> List: - duplicates = [] - flatten_list = [] - for field_value in list(itertools.chain(*lists, not_lists)): - if field_value not in duplicates: - duplicates.append(field_value) - flatten_list.append(field_value) - return flatten_list - - @staticmethod - def _get_ordered_flatten_list(flatten_list, source_field_values): - ordered_flatten_list = [] + def _get_flatten_source_fields(source_fields_values): flat_source_fields = [] - duplicates = [] - for item in source_field_values: + for item in source_fields_values: if isinstance(item, list): flat_source_fields.extend(item) else: flat_source_fields.append(item) - - for field_value in flat_source_fields: - if field_value not in duplicates: - duplicates.append(field_value) - if field_value in flatten_list: - ordered_flatten_list.append(field_value) - return ordered_flatten_list + return flat_source_fields @staticmethod def _filter_missing_fields(source_field_values, targets):