From 8cfbb0e776cf2733c92e7c0ce4df0b5422dd762c Mon Sep 17 00:00:00 2001 From: Dan White Date: Tue, 27 Jun 2023 15:38:43 -0500 Subject: [PATCH] TimeSampleHistogram: port to py3 --- TimeSampleHistogram | 60 ++++++++++++++++++++++----------------------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/TimeSampleHistogram b/TimeSampleHistogram index 03fbdc9..999fbf4 100755 --- a/TimeSampleHistogram +++ b/TimeSampleHistogram @@ -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])) -- 2.25.1