From 9ddfaae8526a5c044e1213a8525e794b2c2a88ed Mon Sep 17 00:00:00 2001 From: Christophe Bedard Date: Sat, 3 Aug 2019 10:19:32 +0200 Subject: [PATCH] Add typing info and docstring for get_field() --- tracetools_read/tracetools_read/__init__.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/tracetools_read/tracetools_read/__init__.py b/tracetools_read/tracetools_read/__init__.py index c627fd5..000a3b4 100644 --- a/tracetools_read/tracetools_read/__init__.py +++ b/tracetools_read/tracetools_read/__init__.py @@ -80,7 +80,24 @@ def event_to_dict(event: babeltrace.babeltrace.Event) -> DictEvent: return d -def get_field(event: DictEvent, field_name: str, default=None, raise_if_not_found=True) -> Any: +def get_field( + event: DictEvent, + field_name: str, + default: Any = None, + raise_if_not_found: bool = True, +) -> Any: + """ + Get value of a field from an event. + + Can return a custom default value if not found. Will raise `AttributeError` by default if not + found, but it can be suppressed. These two options cannot be used together. + + :param event: the event + :param field_name: the name of the field + :param default: the value to use if not found + :param raise_if_not_found: whether to raise an error the field is not found + :return: `None` (or default value) if not found + """ field_value = event.get(field_name, default) # If enabled, raise exception as soon as possible to avoid headaches if raise_if_not_found and field_value is None: