Python: examples: import explicit BT modules
authorPhilippe Proulx <eeppeliteloop@gmail.com>
Tue, 9 Dec 2014 23:51:25 +0000 (18:51 -0500)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Tue, 17 Feb 2015 20:56:27 +0000 (15:56 -0500)
The best practice in Python is generally to import
explicit modules rather than importing all its exposed
members.

This also makes the examples hide the fact that
reader/writer members are exposed directly by the package,
whereas this is only a backward compatibility layer now.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
bindings/python/examples/babeltrace_and_lttng.py
bindings/python/examples/ctf_writer.py
bindings/python/examples/example-api-test.py
bindings/python/examples/sched_switch.py
bindings/python/examples/sequence_test.py

index 0b92c0f6d79cfde7c665ec8a9dcc7205576fcd28..651fa2da982a81ec06fc0c0aaeb92f074a884935 100644 (file)
@@ -45,7 +45,7 @@ out_file = "babeltrace-lttng-trace-text-output.txt"
 
 import time
 try:
-    import babeltrace
+    import babeltrace.reader
     import lttng
 except ImportError:
     raise ImportError( "both babeltrace and lttng-tools "
@@ -109,7 +109,7 @@ if ret < 0:
 # BABELTRACE
 
 # Create TraceCollecion and add trace:
-traces = babeltrace.TraceCollection()
+traces = babeltrace.reader.TraceCollection()
 ret = traces.add_trace(trace_path + "/kernel", "ctf")
 if ret is None:
     raise BabeltraceError("Error adding trace")
index cd7b6fa02fcebd21bcef69a5690bde33d2692be0..d60eb3d880b1af0dc41841e502594901a461ff98 100644 (file)
 
 import sys
 import tempfile
-from babeltrace import *
+import babeltrace.writer as btw
+
 
 trace_path = tempfile.mkdtemp()
 
 print("Writing trace at {}".format(trace_path))
-writer = CTFWriter.Writer(trace_path)
+writer = btw.Writer(trace_path)
 
-clock = CTFWriter.Clock("A_clock")
+clock = btw.Clock("A_clock")
 print("Clock name is \"{}\"".format(clock.name))
 clock.description = "Simple clock"
 print("Clock description is \"{}\"".format(clock.description))
@@ -49,17 +50,17 @@ print("Clock UUID is {}".format(clock.uuid))
 writer.add_clock(clock)
 writer.add_environment_field("Python_version", str(sys.version_info))
 
-stream_class = CTFWriter.StreamClass("test_stream")
+stream_class = btw.StreamClass("test_stream")
 stream_class.clock = clock
 
-event_class = CTFWriter.EventClass("SimpleEvent")
+event_class = btw.EventClass("SimpleEvent")
 
 # Create a int32_t equivalent type
-int32_type = CTFWriter.IntegerFieldDeclaration(32)
+int32_type = btw.IntegerFieldDeclaration(32)
 int32_type.signed = True
 
 # Create a uint16_t equivalent type
-uint16_type = CTFWriter.IntegerFieldDeclaration(16)
+uint16_type = btw.IntegerFieldDeclaration(16)
 uint16_type.signed = False
 
 # Add a custom uint16_t field in the stream's packet context
@@ -71,34 +72,34 @@ packet_context_type.add_field(uint16_type, "a_custom_packet_context_field")
 stream_class.packet_context_type = packet_context_type
 
 # Create a string type
-string_type = CTFWriter.StringFieldDeclaration()
+string_type = btw.StringFieldDeclaration()
 
 # Create a structure type containing both an integer and a string
-structure_type = CTFWriter.StructureFieldDeclaration()
+structure_type = btw.StructureFieldDeclaration()
 structure_type.add_field(int32_type, "an_integer")
 structure_type.add_field(string_type, "a_string_field")
 event_class.add_field(structure_type, "structure_field")
 
 # Create a floating point type
-floating_point_type = CTFWriter.FloatFieldDeclaration()
-floating_point_type.exponent_digits = CTFWriter.FloatFieldDeclaration.FLT_EXP_DIG
-floating_point_type.mantissa_digits = CTFWriter.FloatFieldDeclaration.FLT_MANT_DIG
+floating_point_type = btw.FloatFieldDeclaration()
+floating_point_type.exponent_digits = btw.FloatFieldDeclaration.FLT_EXP_DIG
+floating_point_type.mantissa_digits = btw.FloatFieldDeclaration.FLT_MANT_DIG
 event_class.add_field(floating_point_type, "float_field")
 
 # Create an enumeration type
-int10_type = CTFWriter.IntegerFieldDeclaration(10)
-enumeration_type = CTFWriter.EnumerationFieldDeclaration(int10_type)
+int10_type = btw.IntegerFieldDeclaration(10)
+enumeration_type = btw.EnumerationFieldDeclaration(int10_type)
 enumeration_type.add_mapping("FIRST_ENTRY", 0, 4)
 enumeration_type.add_mapping("SECOND_ENTRY", 5, 5)
 enumeration_type.add_mapping("THIRD_ENTRY", 6, 10)
 event_class.add_field(enumeration_type, "enum_field")
 
 # Create an array type
-array_type = CTFWriter.ArrayFieldDeclaration(int10_type, 5)
+array_type = btw.ArrayFieldDeclaration(int10_type, 5)
 event_class.add_field(array_type, "array_field")
 
 # Create a sequence type
-sequence_type = CTFWriter.SequenceFieldDeclaration(int32_type, "sequence_len")
+sequence_type = btw.SequenceFieldDeclaration(int32_type, "sequence_len")
 event_class.add_field(uint16_type, "sequence_len")
 event_class.add_field(sequence_type, "sequence_field")
 
@@ -106,7 +107,7 @@ stream_class.add_event_class(event_class)
 stream = writer.create_stream(stream_class)
 
 for i in range(100):
-    event = CTFWriter.Event(event_class)
+    event = btw.Event(event_class)
 
     clock.time = i * 1000
     structure_field = event.payload("structure_field")
index 5410d2c26a3112331ffe94a3528af20a8740f6e2..1bdea6cc7eec63a80b2b848f2cd858469af25892 100644 (file)
 # to partially test the api.
 
 import sys
-from babeltrace import *
+import babeltrace.reader
+
 
 # Check for path arg:
 if len(sys.argv) < 2:
     raise TypeError("Usage: python example-api-test.py path/to/file")
 
 # Create TraceCollection and add trace:
-traces = TraceCollection()
+traces = babeltrace.reader.TraceCollection()
 trace_handle = traces.add_trace(sys.argv[1], "ctf")
 if trace_handle is None:
     raise IOError("Error adding trace")
index cf944f5278a8a6c0d7077ceadbff0a3de4758836..0deb4a5320af4a13f70c17cb3a89df820364b93b 100644 (file)
@@ -34,7 +34,9 @@
 # The trace needs PID context (lttng add-context -k -t pid)
 
 import sys
-from babeltrace import *
+import babeltrace.reader
+import babeltrace.common
+
 
 if len(sys.argv) < 2 or len(sys.argv) > 3:
     raise TypeError("Usage: python sched_switch.py [pid] path/to/trace")
@@ -43,7 +45,7 @@ elif len(sys.argv) == 3:
 else:
     filterPID = False
 
-traces = TraceCollection()
+traces = babeltrace.reader.TraceCollection()
 ret = traces.add_trace(sys.argv[len(sys.argv) - 1], "ctf")
 if ret is None:
     raise IOError("Error adding trace")
@@ -53,7 +55,7 @@ for event in traces.events:
         continue
 
     # Getting PID
-    pid = event.field_with_scope("pid", CTFScope.STREAM_EVENT_CONTEXT)
+    pid = event.field_with_scope("pid", babeltrace.common.CTFScope.STREAM_EVENT_CONTEXT)
     if pid is None:
         print("ERROR: Missing PID info for sched_switch")
         continue  # Next event
index 8bd2c749c026a8f9266683c12915e4ad05fcc5c4..e9116ced652080bb02cb55f7b02284491d1cd87c 100644 (file)
 # to partially test the sequence API
 
 import sys
-from babeltrace import *
+import babeltrace.reader
+
 
 # Check for path arg:
 if len(sys.argv) < 2:
     raise TypeError("Usage: sequence_test.py path/to/file")
 
 # Create TraceCollection and add trace:
-traces = TraceCollection()
+traces = babeltrace.reader.TraceCollection()
 trace_handle = traces.add_trace(sys.argv[1], "ctf")
 if trace_handle is None:
     raise IOError("Error adding trace")
This page took 0.02841 seconds and 4 git commands to generate.