btt_plot.py: Fix pylint: len-as-condition
[blktrace.git] / btt / btt_plot.py
index b81dad5d7a4f884599bd13c7a0011a252eb9bb26..d6d27aa32f09fea1366fafbe4b72635e31f0f5fd 100755 (executable)
@@ -55,13 +55,17 @@ Arguments:
   but the -o (--output) and -T (--title) options will be ignored.
 """
 
+from __future__ import absolute_import
+from __future__ import print_function
+import getopt, glob, os, sys
+import six
+from six.moves import range
 __author__ = 'Alan D. Brunelle <alan.brunelle@hp.com>'
 
 #------------------------------------------------------------------------------
 
 import matplotlib
 matplotlib.use('Agg')
-import getopt, glob, os, sys
 import matplotlib.pyplot as plt
 
 plot_size      = [10.9, 8.4]   # inches...
@@ -82,7 +86,7 @@ get_base      = lambda file: file[file.find('_')+1:file.rfind('_')]
 def fatal(msg):
        """Generate fatal error message and exit"""
 
-       print >>sys.stderr, 'FATAL: %s' % msg
+       print('FATAL: %s' % msg, file=sys.stderr)
        sys.exit(1)
 
 #------------------------------------------------------------------------------
@@ -121,10 +125,7 @@ def get_data(files):
                def _avg(vals):
                        """Computes average for array of values passed"""
 
-                       total = 0.0
-                       for val in vals:
-                               total += val
-                       return total / len(vals)
+                       return sum(vals) / len(vals)
 
                #------------------------------------------------------
                if len(xs) < 1000:
@@ -163,7 +164,7 @@ def get_data(files):
                if not os.path.exists(file):
                        fatal('%s not found' % file)
                elif verbose:
-                       print 'Processing %s' % file
+                       print('Processing %s' % file)
 
                xs = []
                ys = []
@@ -214,8 +215,8 @@ def parse_args(args):
 
        try:
                (opts, args) = getopt.getopt(args[1:], s_opts, l_opts)
-       except getopt.error, msg:
-               print >>sys.stderr, msg
+       except getopt.error as msg:
+               print(msg, file=sys.stderr)
                fatal(__doc__)
 
        for (o, a) in opts:
@@ -293,15 +294,15 @@ def generate_output(type, db):
        def color(idx, style):
                """Returns a color/symbol type based upon the index passed."""
 
-                colors = [ 'b', 'g', 'r', 'c', 'm', 'y', 'k' ]
+               colors = [ 'b', 'g', 'r', 'c', 'm', 'y', 'k' ]
                l_styles = [ '-', ':', '--', '-.' ]
                m_styles = [ 'o', '+', '.', ',', 's', 'v', 'x', '<', '>' ]
 
                color = colors[idx % len(colors)]
                if style == 'line':
-                       style = l_styles[(idx / len(l_styles)) % len(l_styles)]
+                       style = l_styles[int((idx / len(l_styles)) % len(l_styles))]
                elif style == 'marker':
-                       style = m_styles[(idx / len(m_styles)) % len(m_styles)]
+                       style = m_styles[int((idx / len(m_styles)) % len(m_styles))]
 
                return '%s%s' % (color, style)
 
@@ -314,7 +315,7 @@ def generate_output(type, db):
                ofile = '%s.png' % type
 
        if verbose:
-               print 'Generating plot into %s' % ofile
+               print('Generating plot into %s' % ofile)
 
        fig = plt.figure(figsize=plot_size)
        ax = fig.add_subplot(111)
@@ -329,7 +330,7 @@ def generate_output(type, db):
                legends = None
 
        keys = []
-       for file in db.iterkeys():
+       for file in six.iterkeys(db):
                if not file in ['min_x', 'max_x', 'min_y', 'max_y']:
                        keys.append(file)
 
@@ -348,7 +349,7 @@ def generate_output(type, db):
                        legends.append(get_base(file))
                idx += 1
 
-       if add_legend and len(legends) > 0:
+       if add_legend and legends:
                gen_legends(ax, legends)
        plt.savefig(ofile)
 
@@ -449,7 +450,7 @@ if __name__ == '__main__':
                output_file = title_str = type = None
                for t in types:
                        files = get_files(t)
-                       if len(files) == 0:
+                       if files == 0:
                                continue
                        elif t == 'bnos':
                                do_bnos(files)
@@ -459,7 +460,7 @@ if __name__ == '__main__':
                                generate_output(t, get_data(files))
                                continue
 
-       elif len(files) < 1:
+       elif not files:
                fatal('Need data files to process')
        else:
                generate_output(type, get_data(files))