TimeSampleHistogram: port to py3
authorDan White <dan@whiteaudio.com>
Tue, 27 Jun 2023 20:38:43 +0000 (15:38 -0500)
committerDan White <dan@whiteaudio.com>
Tue, 27 Jun 2023 20:38:43 +0000 (15:38 -0500)
TimeSampleHistogram

index 03fbdc915d20e1e67d48b3b5712d5c247d74653c..999fbf426892ae1d771199762b245002f3109e10 100755 (executable)
@@ -1,4 +1,4 @@
-#!/usr/bin/env python2
+#!/usr/bin/env python3
 
 # Present histogram reports from timeSampler data.  See "$0 -h" for more
 #
@@ -93,7 +93,7 @@ if not opt.startdate:
     startdate = LONG_AGO
 else:
     #convert from YYYY-MM-DD format
-    d = map(int, opt.startdate.split('-'))
+    d = list(map(int, opt.startdate.split('-')))
     d += [1]*(3 - len(d))  # use start of year/month if missing
     startdate = dt.date(*d)
 
@@ -101,7 +101,7 @@ if not opt.enddate:
     enddate = dt.date.today()
 else:
     #convert from YYYY-MM-DD
-    enddate = dt.date(*map(int, opt.enddate.split('-')))
+    enddate = dt.date(*list(map(int, opt.enddate.split('-'))))
 
 if opt.today and opt.yesterday:
     startdate = dt.date.today() - dt.timedelta(1)
@@ -148,14 +148,14 @@ enddate = dt.datetime.combine(enddate, dt.time.max)
 # Sorting options
 #
 if opt.numsort:
-    def sorter(x, y, reverse=opt.reverse):
+    def sorter(x, reverse=opt.reverse):
         m = -1 if reverse else 1
-        return m * cmp(x[1], y[1])
+        return m * x[1]
 else:
     # alphabetical on key
-    def sorter(x, y, reverse=opt.reverse):
+    def sorter(x, reverse=opt.reverse):
         m = -1 if reverse else 1
-        return m * cmp(x[0].lower(), y[0].lower())
+        return m * x[0].lower()
 
 
 
@@ -185,7 +185,7 @@ for file in files:
         try:
             r = m.groups()[:-1]
         except:
-            print line
+            print(line)
             raise
         ri = [int(i) for i in r]
         d = dt.datetime(*ri)
@@ -214,8 +214,8 @@ for file in files:
             histGtd[a[0]] += 1
 
 if hist:
-    maxlen = max(map(len, hist.iterkeys()))
-    maxnum = max(hist.itervalues())
+    maxlen = max(list(map(len, iter(hist.keys()))))
+    maxnum = max(hist.values())
 else:
     maxlen = maxnum = 1
 
@@ -238,31 +238,31 @@ ps = '%%-%is (%%%ii)%%s' % (name_len, count_len)
 # Show date range
 #
 if not opt.bare:
-    print 'From:', startdate.strftime('%Y-%m-%d'), ' Thru:', enddate.strftime('%Y-%m-%d')
+    print('From:', startdate.strftime('%Y-%m-%d'), ' Thru:', enddate.strftime('%Y-%m-%d'))
 
     if False:
         if opt.today and opt.yesterday:
-            print 'Today (%s) and yesterday' % startdate.strftime('%Y-%m-%d')
+            print('Today (%s) and yesterday' % startdate.strftime('%Y-%m-%d'))
 
         elif opt.yesterday:
             if opt.days:
-                print 'From:', startdate.strftime('%Y-%m-%d'), ' Thru',
+                print('From:', startdate.strftime('%Y-%m-%d'), ' Thru', end=' ')
 
-            print 'Yesterday:', startdate.strftime('%Y-%m-%d')
+            print('Yesterday:', startdate.strftime('%Y-%m-%d'))
 
         elif opt.today:
             if opt.days:
-                print 'From:', startdate.strftime('%Y-%m-%d'), ' Thru',
+                print('From:', startdate.strftime('%Y-%m-%d'), ' Thru', end=' ')
 
-            print 'Today:', startdate.strftime('%Y-%m-%d')
+            print('Today:', startdate.strftime('%Y-%m-%d'))
 
         elif startdate > LONG_AGO:
-            print 'From:', startdate.strftime('%Y-%m-%d'), ' Thru:', enddate.strftime('%Y-%m-%d')
+            print('From:', startdate.strftime('%Y-%m-%d'), ' Thru:', enddate.strftime('%Y-%m-%d'))
 
         elif enddate:
-            print 'Thru:', enddate.strftime('%Y-%m-%d')
+            print('Thru:', enddate.strftime('%Y-%m-%d'))
 
-    print '-' * opt.width
+    print('-' * opt.width)
 
 
 
@@ -271,7 +271,7 @@ if not opt.bare:
 #
 keys = set()
 for pat in args:
-    keys |= set(fnmatch.filter(hist.keys(), pat))
+    keys |= set(fnmatch.filter(list(hist.keys()), pat))
 
 # new dict with only matching values
 hist = {k: hist[k] for k in keys}
@@ -280,30 +280,30 @@ hist = {k: hist[k] for k in keys}
 #
 # Display the histogram
 #
-for k,v in sorted(hist.iteritems(), cmp=sorter):
+for k,v in sorted(iter(hist.items()), key=sorter):
     if v < 1: continue
 
     if len(k) > MAX_NAME_LEN:
         k = k[:MAX_NAME_LEN-1] + '~'
 
     if opt.sep == '':
-        print ps % (k, v, '+'*tics(v))
+        print(ps % (k, v, '+'*tics(v)))
     else:
-        print opt.sep.join((k, str(v)))
+        print(opt.sep.join((k, str(v))))
 
 if not opt.bare:
-    print ('%%%is' % (name_len + count_len + 2)) % sum(hist.values())
+    print(('%%%is' % (name_len + count_len + 2)) % sum(hist.values()))
 
 
 if opt.gtd:
     if not opt.bare:
-        print
-        print 'Counts for GTD categories:'
-        print '--------------------------'
+        print()
+        print('Counts for GTD categories:')
+        print('--------------------------')
     for k in sorted(histGtd.keys()):
-        print '%-5s %4i' % (k, histGtd[k])
-    print 'other %4i' % (sum(hist.values()) - sum(histGtd.values()))
+        print('%-5s %4i' % (k, histGtd[k]))
+    print('other %4i' % (sum(hist.values()) - sum(histGtd.values())))
 
     for k in sorted(histOther.keys()):
-        print '%s: %3i' % (k, histOther[k])
+        print('%s: %3i' % (k, histOther[k]))