Python: document IntegerFieldDeclaration
[babeltrace.git] / bindings / python / bt.py
index ddedf06072f268d1d7d723723675cc9c1823491f..09cc9d2ff20fc2e5eb9932fafffd148abd8fd5ce 100644 (file)
@@ -370,19 +370,47 @@ class ByteOrder:
 # These are taken directly from ctf/events.h
 # All changes to enums must also be made here
 class CTFTypeId:
+    """
+    CTF numeric type identifiers.
+    """
+
+    #: Unknown type
     UNKNOWN = 0
+
+    #: Integer
     INTEGER = 1
+
+    #: Floating point number
     FLOAT = 2
+
+    #: Enumeration
     ENUM = 3
+
+    #: String
     STRING = 4
+
+    #: Structure
     STRUCT = 5
+
+    #: Untagged variant
     UNTAGGED_VARIANT = 6
+
+    #: Variant
     VARIANT = 7
+
+    #: Array
     ARRAY = 8
+
+    #: Sequence
     SEQUENCE = 9
+
     NR_CTF_TYPES = 10
 
     def type_name(id):
+        """
+        Returns the name of the CTF numeric type identifier *id*.
+        """
+
         name = "UNKNOWN_TYPE"
         constants = [
             attr for attr in dir(CTFTypeId) if not callable(
@@ -399,14 +427,33 @@ class CTFTypeId:
 
 
 class CTFScope:
+    """
+    CTF scopes.
+    """
+
+    #: Packet header
     TRACE_PACKET_HEADER = 0
+
+    #: Packet context
     STREAM_PACKET_CONTEXT = 1
+
+    #: Event header
     STREAM_EVENT_HEADER = 2
+
+    #: Stream event context
     STREAM_EVENT_CONTEXT = 3
+
+    #: Event context
     EVENT_CONTEXT = 4
+
+    #: Event fields
     EVENT_FIELDS = 5
 
     def scope_name(scope):
+        """
+        Returns the name of the CTF scope *scope*.
+        """
+
         name = "UNKNOWN_SCOPE"
         constants = [
             attr for attr in dir(CTFScope) if not callable(
@@ -435,9 +482,61 @@ _scopes = [
 
 class Event(collections.Mapping):
     """
-    This class represents an event from the trace.
-    It is obtained using the TraceCollection generator functions.
-    Do not instantiate.
+    An :class:`Event` object represents a trace event. :class:`Event`
+    objects are returned by :attr:`TraceCollection.events` and are
+    not meant to be instantiated by the user.
+
+    :class:`Event` has a :class:`dict`-like interface for accessing
+    an event's field value by field name:
+
+    .. code-block:: python
+
+       event['my_field']
+
+    If a field name exists in multiple scopes, the value of the first
+    field found is returned. The scopes are searched in the following
+    order:
+
+    1. Event fields (:attr:`CTFScope.EVENT_FIELDS`)
+    2. Event context (:attr:`CTFScope.EVENT_CONTEXT`)
+    3. Stream event context (:attr:`CTFScope.STREAM_EVENT_CONTEXT`)
+    4. Event header (:attr:`CTFScope.STREAM_EVENT_HEADER`)
+    5. Packet context (:attr:`CTFScope.STREAM_PACKET_CONTEXT`)
+    6. Packet header (:attr:`CTFScope.TRACE_PACKET_HEADER`)
+
+    It is still possible to obtain a field's value from a specific
+    scope using :meth:`field_with_scope`.
+
+    Field values are returned as native Python types, that is:
+
+    +-----------------------+----------------------------------+
+    | Field type            | Python type                      |
+    +=======================+==================================+
+    | Integer               | :class:`int`                     |
+    +-----------------------+----------------------------------+
+    | Floating point number | :class:`float`                   |
+    +-----------------------+----------------------------------+
+    | Enumeration           | :class:`str` (enumeration label) |
+    +-----------------------+----------------------------------+
+    | String                | :class:`str`                     |
+    +-----------------------+----------------------------------+
+    | Array                 | :class:`list` of native Python   |
+    |                       | objects                          |
+    +-----------------------+----------------------------------+
+    | Sequence              | :class:`list` of native Python   |
+    |                       | objects                          |
+    +-----------------------+----------------------------------+
+    | Structure             | :class:`dict` mapping field      |
+    |                       | names to native Python objects   |
+    +-----------------------+----------------------------------+
+
+    For example, printing the third element of a sequence named ``seq``
+    in a structure named ``my_struct`` of the ``event``'s field named
+    ``my_field`` is done this way:
+
+    .. code-block:: python
+
+       print(event['my_field']['my_struct']['seq'][2])
     """
 
     def __init__(self):
@@ -445,15 +544,16 @@ class Event(collections.Mapping):
 
     @property
     def name(self):
-        """Return the name of the event or None on error."""
+        """
+        Event's name or ``None`` on error.
+        """
 
         return nbt._bt_ctf_event_name(self._e)
 
     @property
     def cycles(self):
         """
-        Return the timestamp of the event as written in
-        the packet (in cycles) or -1ULL on error.
+        Event's timestamp in cycles or -1 on error.
         """
 
         return nbt._bt_ctf_get_cycles(self._e)
@@ -461,8 +561,7 @@ class Event(collections.Mapping):
     @property
     def timestamp(self):
         """
-        Return the timestamp of the event offset with the
-        system clock source or -1ULL on error.
+        Event's timestamp (nanoseconds since Epoch) or -1 on error.
         """
 
         return nbt._bt_ctf_get_timestamp(self._e)
@@ -470,17 +569,22 @@ class Event(collections.Mapping):
     @property
     def datetime(self):
         """
-        Return a datetime object based on the event's
-        timestamp. Note that the datetime class' precision
-        is limited to microseconds.
+        Event's timestamp as a standard :class:`datetime.datetime`
+        object.
+
+        Note that the :class:`datetime.datetime` class' precision
+        is limited to microseconds, whereas :attr:`timestamp` provides
+        the event's timestamp with a nanosecond resolution.
         """
 
         return datetime.fromtimestamp(self.timestamp / 1E9)
 
     def field_with_scope(self, field_name, scope):
         """
-        Get field_name's value in scope.
-        None is returned if no field matches field_name.
+        Returns the value of a field named *field_name* within the
+        scope *scope*, or ``None`` if the field cannot be found.
+
+        *scope* must be one of :class:`CTFScope` constants.
         """
 
         if scope not in _scopes:
@@ -492,7 +596,9 @@ class Event(collections.Mapping):
             return field.value
 
     def field_list_with_scope(self, scope):
-        """Return a list of field names in scope."""
+        """
+        Returns a list of field names in the scope *scope*.
+        """
 
         if scope not in _scopes:
             raise ValueError("Invalid scope provided")
@@ -507,8 +613,8 @@ class Event(collections.Mapping):
     @property
     def handle(self):
         """
-        Get the TraceHandle associated with this event
-        Return None on error
+        :class:`TraceHandle` object containing this event, or ``None``
+        on error.
         """
 
         ret = nbt._bt_ctf_event_get_handle_id(self._e)
@@ -525,8 +631,8 @@ class Event(collections.Mapping):
     @property
     def trace_collection(self):
         """
-        Get the TraceCollection associated with this event.
-        Return None on error.
+        :class:`TraceCollection` object containing this event, or
+        ``None`` on error.
         """
 
         trace_collection = TraceCollection()
@@ -536,22 +642,6 @@ class Event(collections.Mapping):
             return trace_collection
 
     def __getitem__(self, field_name):
-        """
-        Get field_name's value. If the field_name exists in multiple
-        scopes, the first field found is returned. The scopes are searched
-        in the following order:
-        1) EVENT_FIELDS
-        2) EVENT_CONTEXT
-        3) STREAM_EVENT_CONTEXT
-        4) STREAM_EVENT_HEADER
-        5) STREAM_PACKET_CONTEXT
-        6) TRACE_PACKET_HEADER
-        None is returned if no field matches field_name.
-
-        Use field_with_scope() to explicitly access fields in a given
-        scope.
-        """
-
         field = self._field(field_name)
 
         if field is not None:
@@ -579,7 +669,14 @@ class Event(collections.Mapping):
         return self._field(field_name) is not None
 
     def keys(self):
-        """Return a list of field names."""
+        """
+        Returns the list of field names.
+
+        Note: field names are unique within the returned list, although
+        a field name could exist in multiple scopes. Use
+        :meth:`field_list_with_scope` to obtain the list of field names
+        of a given scope.
+        """
 
         field_names = set()
 
@@ -590,6 +687,15 @@ class Event(collections.Mapping):
         return list(field_names)
 
     def get(self, field_name, default=None):
+        """
+        Returns the value of the field named *field_name*, or *default*
+        when not found.
+
+        See :class:`Event` note about how fields are retrieved by
+        name when multiple fields share the same name in different
+        scopes.
+        """
+
         field = self._field(field_name)
 
         if field is None:
@@ -598,6 +704,14 @@ class Event(collections.Mapping):
         return field.value
 
     def items(self):
+        """
+        Generates pairs of (field name, field value).
+
+        This method iterates :meth:`keys` to find field names, which
+        means some fields could be unavailable if other fields share
+        their names in scopes with higher priorities.
+        """
+
         for field in self.keys():
             yield (field, self[field])
 
@@ -651,6 +765,10 @@ class Event(collections.Mapping):
 
 
 class FieldError(Exception):
+    """
+    Field error, raised when a field's value cannot be accessed.
+    """
+
     def __init__(self, value):
         self.value = value
 
@@ -659,7 +777,14 @@ class FieldError(Exception):
 
 
 class EventDeclaration:
-    """Event declaration class.  Do not instantiate."""
+    """
+    An event declaration contains the properties of a class of events,
+    that is, the common properties and fields layout of all the actual
+    recorded events associated with this declaration.
+
+    This class is not meant to be instantiated by the user. It is
+    returned by :attr:`TraceHandle.events`.
+    """
 
     MAX_UINT64 = 0xFFFFFFFFFFFFFFFF
 
@@ -668,13 +793,17 @@ class EventDeclaration:
 
     @property
     def name(self):
-        """Return the name of the event or None on error"""
+        """
+        Event's name, or ``None`` on error.
+        """
 
         return nbt._bt_ctf_get_decl_event_name(self._ed)
 
     @property
     def id(self):
-        """Return the event-ID of the event or -1 on error"""
+        """
+        Event's numeric ID, or -1 on error.
+        """
 
         id = nbt._bt_ctf_get_decl_event_id(self._ed)
 
@@ -686,14 +815,27 @@ class EventDeclaration:
     @property
     def fields(self):
         """
-        Generator returning all FieldDeclarations of an event, going through
+        Generates all the event's field declarations, going through
         each scope in the following order:
-        1) EVENT_FIELDS
-        2) EVENT_CONTEXT
-        3) STREAM_EVENT_CONTEXT
-        4) STREAM_EVENT_HEADER
-        5) STREAM_PACKET_CONTEXT
-        6) TRACE_PACKET_HEADER
+
+        1. Event fields (:attr:`CTFScope.EVENT_FIELDS`)
+        2. Event context (:attr:`CTFScope.EVENT_CONTEXT`)
+        3. Stream event context (:attr:`CTFScope.STREAM_EVENT_CONTEXT`)
+        4. Event header (:attr:`CTFScope.STREAM_EVENT_HEADER`)
+        5. Packet context (:attr:`CTFScope.STREAM_PACKET_CONTEXT`)
+        6. Packet header (:attr:`CTFScope.TRACE_PACKET_HEADER`)
+
+        All the generated field declarations inherit
+        :class:`FieldDeclaration`, and are among:
+
+        * :class:`IntegerFieldDeclaration`
+        * :class:`FloatFieldDeclaration`
+        * :class:`EnumerationFieldDeclaration`
+        * :class:`StringFieldDeclaration`
+        * :class:`ArrayFieldDeclaration`
+        * :class:`SequenceFieldDeclaration`
+        * :class:`StructureFieldDeclaration`
+        * :class:`VariantFieldDeclaration`
         """
 
         for scope in _scopes:
@@ -702,7 +844,22 @@ class EventDeclaration:
 
     def fields_scope(self, scope):
         """
-        Generator returning FieldDeclarations of the current event in scope.
+        Generates all the field declarations of the event's scope
+        *scope*.
+
+        *scope* must be one of :class:`CTFScope` constants.
+
+        All the generated field declarations inherit
+        :class:`FieldDeclaration`, and are among:
+
+        * :class:`IntegerFieldDeclaration`
+        * :class:`FloatFieldDeclaration`
+        * :class:`EnumerationFieldDeclaration`
+        * :class:`StringFieldDeclaration`
+        * :class:`ArrayFieldDeclaration`
+        * :class:`SequenceFieldDeclaration`
+        * :class:`StructureFieldDeclaration`
+        * :class:`VariantFieldDeclaration`
         """
         ret = nbt._by_python_field_decl_listcaller(self._ed, scope)
 
@@ -723,7 +880,11 @@ class EventDeclaration:
 
 
 class FieldDeclaration:
-    """Field declaration class. Do not instantiate."""
+    """
+    Base class for concrete field declarations.
+
+    This class is not meant to be instantiated by the user.
+    """
 
     def __init__(self):
         raise NotImplementedError("FieldDeclaration cannot be instantiated")
@@ -735,15 +896,16 @@ class FieldDeclaration:
 
     @property
     def name(self):
-        """Return the name of a FieldDeclaration or None on error."""
+        """
+        Field's name, or ``None`` on error.
+        """
 
         return self._name
 
     @property
     def type(self):
         """
-        Return the FieldDeclaration's type. One of the entries in class
-        CTFTypeId.
+        Field's type (one of :class:`CTFTypeId` constants).
         """
 
         return nbt._bt_ctf_field_type(self._fd)
@@ -751,14 +913,16 @@ class FieldDeclaration:
     @property
     def scope(self):
         """
-        Return the FieldDeclaration's scope.
+        Field's scope (one of :class:`CTFScope` constants).
         """
 
         return self._s
 
 
 class IntegerFieldDeclaration(FieldDeclaration):
-    """Do not instantiate."""
+    """
+    Integer field declaration.
+    """
 
     def __init__(self):
         raise NotImplementedError("IntegerFieldDeclaration cannot be instantiated")
@@ -766,22 +930,23 @@ class IntegerFieldDeclaration(FieldDeclaration):
     @property
     def signedness(self):
         """
-        Return the signedness of an integer:
-        0 if unsigned; 1 if signed; -1 on error.
+        0 if this integer is unsigned, 1 if signed, or -1 on error.
         """
 
         return nbt._bt_ctf_get_int_signedness(self._fd)
 
     @property
     def base(self):
-        """Return the base of an int or a negative value on error."""
+        """
+        Integer's base (``int``), or a negative value on error.
+        """
 
         return nbt._bt_ctf_get_int_base(self._fd)
 
     @property
     def byte_order(self):
         """
-        Return the byte order. One of class ByteOrder's entries.
+        Integer's byte order (one of :class:`ByteOrder` constants).
         """
 
         ret = nbt._bt_ctf_get_int_byte_order(self._fd)
@@ -796,8 +961,7 @@ class IntegerFieldDeclaration(FieldDeclaration):
     @property
     def length(self):
         """
-        Return the size, in bits, of an int or a negative
-        value on error.
+        Integer's length in bits, or a negative value on error.
         """
 
         return nbt._bt_ctf_get_int_len(self._fd)
@@ -805,22 +969,30 @@ class IntegerFieldDeclaration(FieldDeclaration):
     @property
     def encoding(self):
         """
-        Return the encoding. One of class CTFStringEncoding's entries.
-        Return a negative value on error.
+        Integer's encoding (one of :class:`CTFStringEncoding`
+        constants).
         """
 
         return nbt._bt_ctf_get_encoding(self._fd)
 
 
 class EnumerationFieldDeclaration(FieldDeclaration):
-    """Do not instantiate."""
+    """
+    Enumeration field declaration.
+
+    .. note::
+
+       As of this version, this class is missing some properties.
+    """
 
     def __init__(self):
         raise NotImplementedError("EnumerationFieldDeclaration cannot be instantiated")
 
 
 class ArrayFieldDeclaration(FieldDeclaration):
-    """Do not instantiate."""
+    """
+    Static array field declaration.
+    """
 
     def __init__(self):
         raise NotImplementedError("ArrayFieldDeclaration cannot be instantiated")
@@ -828,8 +1000,8 @@ class ArrayFieldDeclaration(FieldDeclaration):
     @property
     def length(self):
         """
-        Return the length of an array or a negative
-        value on error.
+        Static array's fixed length (number of contained elements), or
+        a negative value on error.
         """
 
         return nbt._bt_ctf_get_array_len(self._fd)
@@ -837,7 +1009,7 @@ class ArrayFieldDeclaration(FieldDeclaration):
     @property
     def element_declaration(self):
         """
-        Return element declaration.
+        Underlying element's field declaration.
         """
 
         field_decl_ptr = nbt._bt_python_get_array_element_declaration(self._fd)
@@ -846,7 +1018,13 @@ class ArrayFieldDeclaration(FieldDeclaration):
 
 
 class SequenceFieldDeclaration(FieldDeclaration):
-    """Do not instantiate."""
+    """
+    Sequence (dynamic array) field declaration.
+
+    .. note::
+
+       As of this version, this class is missing some properties.
+    """
 
     def __init__(self):
         raise NotImplementedError("SequenceFieldDeclaration cannot be instantiated")
@@ -854,7 +1032,7 @@ class SequenceFieldDeclaration(FieldDeclaration):
     @property
     def element_declaration(self):
         """
-        Return element declaration.
+        Underlying element's field declaration.
         """
 
         field_decl_ptr = nbt._bt_python_get_sequence_element_declaration(self._fd)
@@ -863,28 +1041,53 @@ class SequenceFieldDeclaration(FieldDeclaration):
 
 
 class FloatFieldDeclaration(FieldDeclaration):
-    """Do not instantiate."""
+    """
+    Floating point number field declaration.
+
+    .. note::
+
+       As of this version, this class is missing some properties.
+    """
 
     def __init__(self):
         raise NotImplementedError("FloatFieldDeclaration cannot be instantiated")
 
 
 class StructureFieldDeclaration(FieldDeclaration):
-    """Do not instantiate."""
+    """
+    Structure (ordered map of field names to field declarations) field
+    declaration.
+
+    .. note::
+
+       As of this version, this class is missing some properties.
+    """
 
     def __init__(self):
         raise NotImplementedError("StructureFieldDeclaration cannot be instantiated")
 
 
 class StringFieldDeclaration(FieldDeclaration):
-    """Do not instantiate."""
+    """
+    String (NULL-terminated array of bytes) field declaration.
+
+    .. note::
+
+       As of this version, this class is missing some properties.
+    """
 
     def __init__(self):
         raise NotImplementedError("StringFieldDeclaration cannot be instantiated")
 
 
 class VariantFieldDeclaration(FieldDeclaration):
-    """Do not instantiate."""
+    """
+    Variant (dynamic selection between different types) field declaration.
+
+    .. note::
+
+       As of this version, this class is missing some properties.
+    """
 
     def __init__(self):
         raise NotImplementedError("VariantFieldDeclaration cannot be instantiated")
This page took 0.030613 seconds and 4 git commands to generate.