From a47ab64ac2cd047fbc7719d86589fab021e7b0dc Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=A9mie=20Galarneau?= Date: Sun, 12 Oct 2014 05:16:14 -0400 Subject: [PATCH] Python bindings: Make examples PEP8 compliant --- .../python/examples/babeltrace_and_lttng.py | 48 +++++++++-------- bindings/python/examples/ctf_writer.py | 46 ++++++++-------- bindings/python/examples/example-api-test.py | 42 +++++++-------- bindings/python/examples/sched_switch.py | 54 +++++++++---------- bindings/python/examples/sequence_test.py | 34 ++++++------ 5 files changed, 114 insertions(+), 110 deletions(-) diff --git a/bindings/python/examples/babeltrace_and_lttng.py b/bindings/python/examples/babeltrace_and_lttng.py index 42ebc85e..d837a532 100644 --- a/bindings/python/examples/babeltrace_and_lttng.py +++ b/bindings/python/examples/babeltrace_and_lttng.py @@ -1,19 +1,19 @@ #!/usr/bin/env python3 # babeltrace_and_lttng.py -# +# # Babeltrace and LTTng example script -# +# # Copyright 2012 EfficiOS Inc. -# +# # Author: Danny Serres -# +# # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: -# +# # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. @@ -29,25 +29,28 @@ # ------------------------------------------------------ -ses_name = "babeltrace-lttng-test" -trace_path = "/lttng-traces/babeltrace-lttng-trace/" -out_file = "babeltrace-lttng-trace-text-output.txt" +ses_name = "babeltrace-lttng-test" +trace_path = "/lttng-traces/babeltrace-lttng-trace/" +out_file = "babeltrace-lttng-trace-text-output.txt" # ------------------------------------------------------ import time try: - import babeltrace, lttng + import babeltrace + import lttng except ImportError: - raise ImportError( "both babeltrace and lttng-tools " - "python modules must be installed" ) + raise ImportError( "both babeltrace and lttng-tools " + "python modules must be installed") # Errors to raise if something goes wrong class LTTngError(Exception): - pass + pass + + class BabeltraceError(Exception): - pass + pass # LTTNG-TOOLS @@ -56,9 +59,9 @@ class BabeltraceError(Exception): lttng.destroy(ses_name) # Creating a new session and handle -ret = lttng.create(ses_name,trace_path) +ret = lttng.create(ses_name, trace_path) if ret < 0: - raise LTTngError(lttng.strerror(ret)) + raise LTTngError(lttng.strerror(ret)) domain = lttng.Domain() domain.type = lttng.DOMAIN_KERNEL @@ -66,7 +69,7 @@ domain.type = lttng.DOMAIN_KERNEL han = None han = lttng.Handle(ses_name, domain) if han is None: - raise LTTngError("Handle not created") + raise LTTngError("Handle not created") # Enabling all events @@ -75,24 +78,24 @@ event.type = lttng.EVENT_ALL event.loglevel_type = lttng.EVENT_LOGLEVEL_ALL ret = lttng.enable_event(han, event, None) if ret < 0: - raise LTTngError(lttng.strerror(ret)) + raise LTTngError(lttng.strerror(ret)) # Start, wait, stop ret = lttng.start(ses_name) if ret < 0: - raise LTTngError(lttng.strerror(ret)) + raise LTTngError(lttng.strerror(ret)) print("Tracing...") time.sleep(2) print("Stopped.") ret = lttng.stop(ses_name) if ret < 0: - raise LTTngError(lttng.strerror(ret)) + raise LTTngError(lttng.strerror(ret)) # Destroying tracing session ret = lttng.destroy(ses_name) if ret < 0: - raise LTTngError(lttng.strerror(ret)) + raise LTTngError(lttng.strerror(ret)) # BABELTRACE @@ -101,7 +104,7 @@ if ret < 0: traces = babeltrace.TraceCollection() ret = traces.add_trace(trace_path + "/kernel", "ctf") if ret is None: - raise BabeltraceError("Error adding trace") + raise BabeltraceError("Error adding trace") # Reading events from trace # and outputting timestamps and event names @@ -110,7 +113,8 @@ print("Writing trace file...") output = open(out_file, "wt") for event in traces.events: - output.write("TS: {}, {} : {}\n".format(event.timestamp, event.cycles, event.name)) + output.write("TS: {}, {} : {}\n".format( + event.timestamp, event.cycles, event.name)) # Closing file output.close() diff --git a/bindings/python/examples/ctf_writer.py b/bindings/python/examples/ctf_writer.py index b33af931..178eed31 100644 --- a/bindings/python/examples/ctf_writer.py +++ b/bindings/python/examples/ctf_writer.py @@ -58,7 +58,7 @@ uint16_type.signed = False packet_context_type = stream_class.packet_context_type print("\nFields in default packet context:") for field in packet_context_type.fields: - print(str(type(field[1])) + " " + field[0]) + print(str(type(field[1])) + " " + field[0]) packet_context_type.add_field(uint16_type, "a_custom_packet_context_field") stream_class.packet_context_type = packet_context_type @@ -98,35 +98,35 @@ stream_class.add_event_class(event_class) stream = writer.create_stream(stream_class) for i in range(100): - event = CTFWriter.Event(event_class) + event = CTFWriter.Event(event_class) - clock.time = i * 1000 - structure_field = event.payload("structure_field") - integer_field = structure_field.field("an_integer") - integer_field.value = i + clock.time = i * 1000 + structure_field = event.payload("structure_field") + integer_field = structure_field.field("an_integer") + integer_field.value = i - string_field = structure_field.field("a_string_field") - string_field.value = "Test string." + string_field = structure_field.field("a_string_field") + string_field.value = "Test string." - float_field = event.payload("float_field") - float_field.value = float(i) + (float(i) / 100.0) + float_field = event.payload("float_field") + float_field.value = float(i) + (float(i) / 100.0) - array_field = event.payload("array_field") - for j in range(5): - element = array_field.field(j) - element.value = i + j + array_field = event.payload("array_field") + for j in range(5): + element = array_field.field(j) + element.value = i + j - event.payload("sequence_len").value = i % 10 - sequence_field = event.payload("sequence_field") - sequence_field.length = event.payload("sequence_len") - for j in range(event.payload("sequence_len").value): - sequence_field.field(j).value = i + j + event.payload("sequence_len").value = i % 10 + sequence_field = event.payload("sequence_field") + sequence_field.length = event.payload("sequence_len") + for j in range(event.payload("sequence_len").value): + sequence_field.field(j).value = i + j - enumeration_field = event.payload("enum_field") - integer_field = enumeration_field.container - enumeration_field.value = i % 10 + enumeration_field = event.payload("enum_field") + integer_field = enumeration_field.container + enumeration_field.value = i % 10 - stream.append_event(event) + stream.append_event(event) # Populate custom packet context field before flushing packet_context = stream.packet_context diff --git a/bindings/python/examples/example-api-test.py b/bindings/python/examples/example-api-test.py index 56a8f88f..c1c6f42e 100644 --- a/bindings/python/examples/example-api-test.py +++ b/bindings/python/examples/example-api-test.py @@ -25,36 +25,36 @@ from babeltrace import * # Check for path arg: if len(sys.argv) < 2: - raise TypeError("Usage: python example-api-test.py path/to/file") + raise TypeError("Usage: python example-api-test.py path/to/file") # Create TraceCollection and add trace: traces = TraceCollection() trace_handle = traces.add_trace(sys.argv[1], "ctf") if trace_handle is None: - raise IOError("Error adding trace") + raise IOError("Error adding trace") # Listing events print("--- Event list ---") for event_declaration in trace_handle.events: - print("event : {}".format(event_declaration.name)) - if event_declaration.name == "sched_switch": - for field_declaration in event_declaration.fields: - print(field_declaration) + print("event : {}".format(event_declaration.name)) + if event_declaration.name == "sched_switch": + for field_declaration in event_declaration.fields: + print(field_declaration) print("--- Done ---") for event in traces.events: - print("TS: {}, {} : {}".format(event.timestamp, event.cycles, event.name)) - - if event.name == "sched_switch": - prev_comm = event["prev_comm"] - if prev_comm is None: - print("ERROR: Missing prev_comm context info") - else: - print("sched_switch prev_comm: {}".format(prev_comm)) - - if event.name == "exit_syscall": - ret_code = event["ret"] - if ret_code is None: - print("ERROR: Unable to extract ret") - else: - print("exit_syscall ret: {}".format(ret_code)) + print("TS: {}, {} : {}".format(event.timestamp, event.cycles, event.name)) + + if event.name == "sched_switch": + prev_comm = event["prev_comm"] + if prev_comm is None: + print("ERROR: Missing prev_comm context info") + else: + print("sched_switch prev_comm: {}".format(prev_comm)) + + if event.name == "exit_syscall": + ret_code = event["ret"] + if ret_code is None: + print("ERROR: Unable to extract ret") + else: + print("exit_syscall ret: {}".format(ret_code)) diff --git a/bindings/python/examples/sched_switch.py b/bindings/python/examples/sched_switch.py index 8c475565..99e818e2 100644 --- a/bindings/python/examples/sched_switch.py +++ b/bindings/python/examples/sched_switch.py @@ -29,41 +29,41 @@ import sys from babeltrace import * if len(sys.argv) < 2 or len(sys.argv) > 3: - raise TypeError("Usage: python sched_switch.py [pid] path/to/trace") + raise TypeError("Usage: python sched_switch.py [pid] path/to/trace") elif len(sys.argv) == 3: - filterPID = True + filterPID = True else: - filterPID = False + filterPID = False traces = TraceCollection() -ret = traces.add_trace(sys.argv[len(sys.argv)-1], "ctf") +ret = traces.add_trace(sys.argv[len(sys.argv) - 1], "ctf") if ret is None: - raise IOError("Error adding trace") + raise IOError("Error adding trace") for event in traces.events: - if event.name != "sched_switch": - continue + if event.name != "sched_switch": + continue - # Getting PID - pid = event.field_with_scope("pid", CTFScope.STREAM_EVENT_CONTEXT) - if pid is None: - print("ERROR: Missing PID info for sched_switch") - continue # Next event + # Getting PID + pid = event.field_with_scope("pid", CTFScope.STREAM_EVENT_CONTEXT) + if pid is None: + print("ERROR: Missing PID info for sched_switch") + continue # Next event - if filterPID and (pid != long(sys.argv[1])): - continue # Next event + if filterPID and (pid != long(sys.argv[1])): + continue # Next event - prev_comm = event["prev_comm"] - prev_tid = event["prev_tid"] - prev_prio = event["prev_prio"] - prev_state = event["prev_state"] - next_comm = event["next_comm"] - next_tid = event["next_tid"] - next_prio = event["next_prio"] + prev_comm = event["prev_comm"] + prev_tid = event["prev_tid"] + prev_prio = event["prev_prio"] + prev_state = event["prev_state"] + next_comm = event["next_comm"] + next_tid = event["next_tid"] + next_prio = event["next_prio"] - # Output - print("sched_switch, pid = {}, TS = {}, prev_comm = {},\n\t" - "prev_tid = {}, prev_prio = {}, prev_state = {},\n\t" - "next_comm = {}, next_tid = {}, next_prio = {}".format( - pid, event.timestamp, prev_comm, prev_tid, - prev_prio, prev_state, next_comm, next_tid, next_prio)) + # Output + print("sched_switch, pid = {}, TS = {}, prev_comm = {},\n\t" + "prev_tid = {}, prev_prio = {}, prev_state = {},\n\t" + "next_comm = {}, next_tid = {}, next_prio = {}".format( + pid, event.timestamp, prev_comm, prev_tid, + prev_prio, prev_state, next_comm, next_tid, next_prio)) diff --git a/bindings/python/examples/sequence_test.py b/bindings/python/examples/sequence_test.py index c6313b7e..38cf0aa1 100644 --- a/bindings/python/examples/sequence_test.py +++ b/bindings/python/examples/sequence_test.py @@ -27,32 +27,32 @@ from babeltrace import * # Check for path arg: if len(sys.argv) < 2: - raise TypeError("Usage: sequence_test.py path/to/file") + raise TypeError("Usage: sequence_test.py path/to/file") # Create TraceCollection and add trace: traces = TraceCollection() trace_handle = traces.add_trace(sys.argv[1], "ctf") if trace_handle is None: - raise IOError("Error adding trace") + raise IOError("Error adding trace") # Listing events print("--- Event list ---") for event_declaration in trace_handle.events: - print("event : {}".format(event_declaration.name)) + print("event : {}".format(event_declaration.name)) print("--- Done ---") for event in traces.events: - print("TS: {}, {} : {}".format(event.timestamp, - event.cycles, event.name)) - - try: - sequence = event["seq_int_field"] - print("int sequence values: {}". format(sequence)) - except KeyError: - pass - - try: - sequence = event["seq_long_field"] - print("long sequence values: {}". format(sequence)) - except KeyError: - pass + print("TS: {}, {} : {}".format(event.timestamp, + event.cycles, event.name)) + + try: + sequence = event["seq_int_field"] + print("int sequence values: {}". format(sequence)) + except KeyError: + pass + + try: + sequence = event["seq_long_field"] + print("long sequence values: {}". format(sequence)) + except KeyError: + pass -- 2.34.1