Skip to content

Commit 00a60a9

Browse files
committed
improve docs
1 parent 615dfc4 commit 00a60a9

File tree

1 file changed

+39
-20
lines changed

1 file changed

+39
-20
lines changed

logprep/util/helper.py

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,22 @@
2020

2121

2222
class Missing(Enum):
23-
"""Sentinel value for missing fields."""
23+
"""Sentinel type for indicating missing fields."""
2424

2525
MISSING = auto()
2626

2727

28-
MISSING = Missing.MISSING
29-
"""Sentinel value for missing fields."""
28+
MISSING = Missing.MISSING # pylint: disable=invalid-name
29+
"""Sentinel value for indicating missing fields."""
3030

3131

3232
class Skip(Enum):
33-
"""Sentinel value for method instrumentation to skip fields."""
33+
"""Sentinel type for method instrumentation to skip fields."""
3434

3535
SKIP = auto()
3636

3737

38-
SKIP = Skip.SKIP
38+
SKIP = Skip.SKIP # pylint: disable=invalid-name
3939
"""Sentinel value for method instrumentation to skip fields."""
4040

4141

@@ -58,6 +58,7 @@ def color_print_line(
5858

5959

6060
def color_print_title(background: Union[str, AnsiBack], message: str):
61+
"""Print dashed title line with black foreground colour and reset the color afterwards."""
6162
message = f"------ {message} ------"
6263
color_print_line(background, Fore.BLACK, message)
6364

@@ -182,7 +183,6 @@ def add_fields_to(
182183
rule: "Rule" = None,
183184
merge_with_target: bool = False,
184185
overwrite_target: bool = False,
185-
filter_none: bool = True,
186186
) -> None:
187187
"""
188188
Handles the batch addition operation while raising a FieldExistsWarning with all unsuccessful targets.
@@ -208,7 +208,7 @@ def add_fields_to(
208208
existence restrictions.
209209
"""
210210
# filter out None values
211-
fields = {key: value for key, value in fields.items() if not filter_none or value is not None}
211+
fields = {key: value for key, value in fields.items() if value is not None}
212212
number_fields = len(dict(fields))
213213
if number_fields == 1:
214214
_add_field_to(event, list(fields.items())[0], rule, merge_with_target, overwrite_target)
@@ -277,9 +277,7 @@ def _get_item(container: FieldValue, key: str) -> FieldValue:
277277
return list.__getitem__(typing.cast(list[FieldValue], container), index_or_slice)
278278

279279

280-
def get_dotted_field_value(
281-
event: dict[str, FieldValue], dotted_field: str, silent_fail: bool = True
282-
) -> FieldValue:
280+
def get_dotted_field_value(event: dict[str, FieldValue], dotted_field: str) -> FieldValue:
283281
"""
284282
Returns the value of a requested dotted_field by iterating over the event dictionary until the
285283
field was found. In case the field could not be found None is returned.
@@ -296,7 +294,7 @@ def get_dotted_field_value(
296294
297295
Returns
298296
-------
299-
Optional[Union[dict, list, str]]
297+
FieldValue
300298
The value of the requested dotted field, which can be None.
301299
None is also returnd when the field could not be found and silent_fail is True.
302300
@@ -310,10 +308,8 @@ def get_dotted_field_value(
310308
for field in get_dotted_field_list(dotted_field):
311309
current = _get_item(current, field)
312310
return current
313-
except (KeyError, ValueError, TypeError, IndexError) as error:
314-
if silent_fail:
315-
return None
316-
raise error
311+
except (KeyError, ValueError, TypeError, IndexError):
312+
return None
317313

318314

319315
def get_dotted_field_value_with_explicit_missing(
@@ -355,6 +351,30 @@ def get_dotted_field_values(
355351
dotted_fields: Iterable[str],
356352
on_missing: Callable[[str], Union[FieldValue, Skip]] = lambda _: None,
357353
) -> dict[str, FieldValue]:
354+
"""
355+
Extract the subset of fields from the dict by using the list of (potentially dotted)
356+
field names as an allow list.
357+
The behavior for fields targeted by the list but missing in the dict can be controlled
358+
by a callback.
359+
The callback allows for providing a replacement value, or - by returning SKIP - can
360+
instruct the method to omit the field entirely from the extracted dict.
361+
362+
363+
Parameters
364+
----------
365+
event : dict
366+
The (potentially nested) dict where the values are sourced from
367+
dotted_fields : Iterable[str]
368+
The (potentially dotted) list of field names to extract
369+
on_missing : _type_, optional
370+
The callback to control the behavior for missing fields, by default
371+
`lambda _: None` which returns missing fields with `None` value
372+
373+
Returns
374+
-------
375+
dict[str, FieldValue]
376+
The (potentially nested) sub-dict
377+
"""
358378
result: dict[str, FieldValue] = {}
359379
for field_to_copy in dotted_fields:
360380
value = get_dotted_field_value_with_explicit_missing(event, field_to_copy)
@@ -498,10 +518,10 @@ def copy_fields_to_event(
498518
target_event: dict,
499519
source_event: dict,
500520
dotted_field_names: Iterable[str],
521+
*,
501522
skip_missing: bool = True,
502523
merge_with_target: bool = False,
503524
overwrite_target: bool = False,
504-
filter_none: bool = True,
505525
rule: "Rule" = None,
506526
) -> None:
507527
"""
@@ -517,11 +537,11 @@ def copy_fields_to_event(
517537
dotted_field_names : list[str]
518538
The list of (potentially dotted) field names to copy
519539
skip_missing : bool, optional
520-
_description_, by default False
540+
Controls whether missing fields should be skipped or defaulted to None, by default True
521541
merge_with_target : bool, optional
522-
_description_, by default False
542+
Controls whether already existing fields should be merged as a list, by default False
523543
overwrite_target : bool, optional
524-
_description_, by default False
544+
Controls whether already existing fields should be overwritten, by default False
525545
rule : Rule, optional
526546
Contextual info for error handling, by default None
527547
"""
@@ -535,7 +555,6 @@ def copy_fields_to_event(
535555
rule=rule,
536556
overwrite_target=overwrite_target,
537557
merge_with_target=merge_with_target,
538-
filter_none=filter_none,
539558
)
540559

541560

0 commit comments

Comments
 (0)