From 2640c5f3874408243df68a73a1eedaf71c81efd6 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Wed, 17 Apr 2013 17:14:52 -0400 Subject: [PATCH] src/figures/cantilever-data: Modernize print statements Old New =============== ================ print >> x, ... x.write(...) print ... print(...) '%s' % (...) '{}'.format(...) =============== ================ The old `print >> ` syntax wasn't Python 3 compliant. --- src/figures/cantilever-data/avg_data.py | 18 ++++++++++-------- src/figures/cantilever-data/fit_data.py | 11 ++++++----- .../cantilever-data/get_loading_rates.py | 4 ++-- 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/src/figures/cantilever-data/avg_data.py b/src/figures/cantilever-data/avg_data.py index c9a3fe1..e10c4ec 100755 --- a/src/figures/cantilever-data/avg_data.py +++ b/src/figures/cantilever-data/avg_data.py @@ -45,8 +45,8 @@ def write_k_file(Ks, kfile=KFILE): if i == 1: continue # poor calibration bumps for the older cantilevers - print >> kf, "K(%d) = %g +/- %g, (# %d)" \ - % (i, avgK[i], stdK[i], numK[i]) + kf.write('K({}) = {} +/- {}, (# {})\n'.format( + i, avgK[i], stdK[i], numK[i])) kf.close() return (avgK, stdK, numK) @@ -54,9 +54,10 @@ def write_average_file(FofV, avgK, avgfile=AVGFILE): Vs = FofV.keys() Vs.sort() af = open(AVGFILE, 'w') - print >> af, '#'+'\t'.join(\ - ['Pulling speed (nm/s)','Spring constant (pN/nm)', - 'Mean force (pN)','Std. force (pN)','Events (#)']) + af.write('{}{}\n'.format( + '#', '\t'.join( + ['Pulling speed (nm/s)','Spring constant (pN/nm)', + 'Mean force (pN)','Std. force (pN)','Events (#)']))) for V in Vs: for i,k in enumerate(avgK): if i == 1: @@ -65,9 +66,10 @@ def write_average_file(FofV, avgK, avgfile=AVGFILE): if len(F) == 0: continue outs= [V, k, F.mean(), F.std(), len(F)] - souts = ["%.2f" % (x) for x in outs] - souts[-1] = "%d" % outs[-1] # special treatment for the integer - print >> af, '\t'.join(souts) + souts = ['{:.2f}'.format(x) for x in outs] + souts[-1] = str(outs[-1]) # special treatment for the integer + af.write('\t'.join(souts)) + af.write('\n') af.close() if __name__ == '__main__': diff --git a/src/figures/cantilever-data/fit_data.py b/src/figures/cantilever-data/fit_data.py index de03a66..778497c 100755 --- a/src/figures/cantilever-data/fit_data.py +++ b/src/figures/cantilever-data/fit_data.py @@ -13,7 +13,7 @@ x = [] y = [] w = [] # weights -print >> sys.stderr, "Fitting data from", fname +sys.stderr.write('Fitting data from {}\n'.format(fname)) for line in open(fname, 'r'): fields = [float(a) for a in line.strip().split()] if WEIGHTHACK==True: @@ -29,7 +29,8 @@ if LOGX == True: slope,intercept,r,two_tailed_prob,stderr_of_the_estimate = linregress([log10(xi) for xi in x],y) else: slope,intercept,r,two_tailed_prob,stderr_of_the_estimate = linregress(x,y) -#print slope,intercept,r,two_tailed_prob,stderr_of_the_estimate +#print(' '.join(str(x) for x in [ +# slope,intercept,r,two_tailed_prob,stderr_of_the_estimate])) def f(x): if LOGX == True: @@ -40,6 +41,6 @@ def f(x): x.sort() xmin = x[0] xmax = x[-1] -print '#fitted line' -print '%g\t%g' % (xmin, f(xmin)) -print '%g\t%g' % (xmax, f(xmax)) +print('#fitted line') +print('{}\t{}'.format(xmin, f(xmin))) +print('{}\t{}'.format(xmax, f(xmax))) diff --git a/src/figures/cantilever-data/get_loading_rates.py b/src/figures/cantilever-data/get_loading_rates.py index 99e6f24..13353b8 100755 --- a/src/figures/cantilever-data/get_loading_rates.py +++ b/src/figures/cantilever-data/get_loading_rates.py @@ -31,8 +31,8 @@ for line in open(DATA, 'r'): if ispring == 1: continue # drop middle spring constants - print >> OFILEs[ispring], "%g\t%g" % (L, F) - + OFILEs[ispring].write('{}\t{}\n'.format(L, F)) + for i,K in enumerate(Ks): if K == None: continue -- 2.26.2