diff --git a/Makefile.am b/Makefile.am index 106c0df2..2bd5634b 100644 --- a/Makefile.am +++ b/Makefile.am @@ -96,7 +96,7 @@ check-cfityk: check-python: PYTHONPATH=fityk/swig/.libs:$(srcdir)/fityk/swig/ \ - python -m unittest discover -s $(srcdir)/tests/ + $(PYTHON) -m unittest discover -s $(srcdir)/tests/ check-help: echo '$$ fityk -h' > $(srcdir)/doc/help-message.txt diff --git a/samples/cfityk.py b/samples/cfityk.py index b13f9286..f54c10b3 100755 --- a/samples/cfityk.py +++ b/samples/cfityk.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 """ Equivalent of cfityk in Python. """ diff --git a/samples/hello.py b/samples/hello.py index 3c4d79d3..961e1a30 100755 --- a/samples/hello.py +++ b/samples/hello.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 import os.path, sys from fityk import Fityk @@ -10,23 +10,23 @@ def __init__(self, filename): raise ValueError("File `%s' not found." % filename) self.filename = filename self.execute("@0 < '%s'" % filename) - print "Data info:", self.get_info("data", 0) + print("Data info:", self.get_info("data", 0)) def run(self): self.execute("guess Gaussian") gauss = self.all_functions()[-1] # the last function (just created) - print "initial Gaussian center: %g" % gauss.get_param_value("center") - print "Fitting %s ..." % self.filename + print("initial Gaussian center: %g" % gauss.get_param_value("center")) + print("Fitting %s ..." % self.filename) self.execute("fit") - print "WSSR=", self.get_wssr() - print "Gaussian center: %g" % gauss.get_param_value("center") + print("WSSR=", self.get_wssr()) + print("Gaussian center: %g" % gauss.get_param_value("center")) def save_session(self, filename): self.execute("info state >'%s'" % filename) f = Fityk() -print f.get_info("version") -print "ln(2) =", f.calculate_expr("ln(2)") +print(f.get_info("version")) +print("ln(2) =", f.calculate_expr("ln(2)")) del f g = GaussianFitter(os.path.join(os.path.dirname(sys.argv[0]), "nacl01.dat")) diff --git a/tests/test_data_load.py b/tests/test_data_load.py index 36f31013..4cd1fce2 100755 --- a/tests/test_data_load.py +++ b/tests/test_data_load.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # -*- coding: utf-8 -*- # run tests with: python -m unittest test_data_load @@ -22,7 +22,7 @@ def setUp(self): f = tempfile.NamedTemporaryFile(prefix=prefix, delete=False) self.data = self.generate_data() for d in self.data: - f.write(self.line_format(d)) + f.write(self.line_format(d).encode()) f.close() self.data.sort() self.filename = f.name @@ -32,7 +32,7 @@ def generate_data(self): for _ in range(30)] def tearDown(self): - #print "remove " + self.filename + #print("remove " + self.filename) os.unlink(self.filename) def compare(self, out, ndigits): @@ -110,7 +110,7 @@ def test_load_default(self): def test_load_with_index(self): self.ftk.execute("@0 < '%s:0:2::'" % self.filename) out = self.ftk.get_data() - self.assertEqual([i.x for i in out], range(len(out))) + self.assertEqual([i.x for i in out], list(range(len(out)))) def test_load_with_sigma(self): self.ftk.execute("@0 < '%s:1:2:3:'" % self.filename) out = self.ftk.get_data() @@ -133,7 +133,7 @@ def setUp(self): self.ftk = fityk.Fityk() self.ftk.set_option_as_number("verbosity", -1) f = tempfile.NamedTemporaryFile(delete=False) - f.write("M=1\n X[0]=1.1, Y[0]=-5, S[0]=0.8") + f.write(b"M=1\n X[0]=1.1, Y[0]=-5, S[0]=0.8") f.close() self.filename = f.name @@ -154,8 +154,8 @@ def setUp(self): self.ftk.set_option_as_number("verbosity", -1) f = tempfile.NamedTemporaryFile(suffix='.gz', delete=False) self.filename = f.name - gf = gzip.GzipFile(fileobj=f) - gf.write("M=1\n X[0]=1.1, Y[0]=-5, S[0]=0.8") + gf = gzip.GzipFile(fileobj=f, mode='wb') + gf.write(b"M=1\n X[0]=1.1, Y[0]=-5, S[0]=0.8") gf.close() def tearDown(self): diff --git a/tests/test_guess.py b/tests/test_guess.py index ca7a07c5..580d3bab 100755 --- a/tests/test_guess.py +++ b/tests/test_guess.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 import os import sys diff --git a/tests/test_info.py b/tests/test_info.py index 21603d9d..680f7dd9 100755 --- a/tests/test_info.py +++ b/tests/test_info.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # run tests with: python -m unittest test_info # or python -m unittest discover diff --git a/tests/test_model.py b/tests/test_model.py index 75c37c0b..456af305 100755 --- a/tests/test_model.py +++ b/tests/test_model.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # run tests with: python -m unittest test_model # or python -m unittest discover diff --git a/tests/test_nist.py b/tests/test_nist.py index 54faadab..71893e51 100755 --- a/tests/test_nist.py +++ b/tests/test_nist.py @@ -1,11 +1,11 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # run tests with: python -m unittest discover import os import sys import re -import urllib2 +from urllib.request import urlopen import unittest import fityk @@ -40,14 +40,17 @@ def open_nist_data(name): name_ext = name + ".dat" local_file = os.path.join(CACHE_DIR, name_ext) if os.path.exists(local_file): - text = open(local_file).read() + with open(local_file, 'rb') as f: + text = f.read() else: sys.stderr.write("Local data copy not found. Trying itl.nist.gov...\n") - text = urllib2.urlopen(DATA_URL_BASE + name_ext).read() + text = urlopen(DATA_URL_BASE + name_ext).read() if not os.path.isdir(CACHE_DIR): os.mkdir(CACHE_DIR) - open(local_file, "wb").write(text) - return text + with open(local_file, "wb") as f: + sys.stderr.write("Writing " + local_file + ' ...\n') + f.write(text) + return text.decode() def read_reference_data(name): @@ -100,12 +103,13 @@ def run(data_name, fit_method, easy=True): tolerance["err"] = 5e-5 #if fit_method in ("mpfit", "levenberg_marquardt"): if VERBOSE > 0: - print "Testing %s (start%s) on %-10s" % (fit_method, easy+1, data_name), + print("Testing %s (start%s) on %-10s" % (fit_method, easy+1, data_name), + end='') if VERBOSE > 1: - print + print() ref = read_reference_data(data_name) if VERBOSE > 2: - print ref.model + print(ref.model) ftk = fityk.Fityk() if VERBOSE < 3: ftk.execute("set verbosity=-1") @@ -134,7 +138,7 @@ def run(data_name, fit_method, easy=True): #ftk.execute("set fitting_method=levenberg_marquardt") #ftk.execute("fit") except fityk.ExecuteError as e: - print "fityk.ExecuteError: %s" % e + print("fityk.ExecuteError: %s" % e) return False ssr = ftk.get_ssr() ssr_diff = (ssr - ref.ssr) / ref.ssr @@ -147,10 +151,10 @@ def run(data_name, fit_method, easy=True): ok = (abs(ssr_diff) < tolerance["wssr"]) if ref.ssr > 1e-10 and ssr_diff < -1e-10: - print "Eureka! %.10E < %.10E" % (ssr, ref.ssr) + print("Eureka! %.10E < %.10E" % (ssr, ref.ssr)) fmt = " %8s %13E %13E %+.1E" if VERBOSE > 2 or (VERBOSE == 2 and not ok): - print fmt % ("SSR", ssr, ref.ssr, ssr_diff) + print(fmt % ("SSR", ssr, ref.ssr, ssr_diff)) our_func = ftk.all_functions()[0] for par in ref.parameters: calc_value = our_func.get_param_value(par.name) @@ -163,9 +167,9 @@ def run(data_name, fit_method, easy=True): err_diff = (calc_err - par.stddev) / par.stddev err_ok = (abs(err_diff) < tolerance["err"]) if VERBOSE > 2 or (VERBOSE == 2 and (not param_ok or not err_ok)): - print fmt % (par.name, calc_value, par.value, val_diff) + print(fmt % (par.name, calc_value, par.value, val_diff)) if "err" in tolerance: - print fmt % ("+/-", calc_err, par.stddev, err_diff) + print(fmt % ("+/-", calc_err, par.stddev, err_diff)) ok = (ok and param_ok and err_ok) if VERBOSE == 1: print("OK" if ok else "FAILED") @@ -255,7 +259,7 @@ def try_method(method): if ok: ok_count += 1 sys.stdout.flush() - print "OK: %2d / %2d" % (ok_count, all_count) + print("OK: %2d / %2d" % (ok_count, all_count)) if __name__ == '__main__': diff --git a/tests/test_tranform.py b/tests/test_tranform.py index bd6d8c01..9511b2b1 100755 --- a/tests/test_tranform.py +++ b/tests/test_tranform.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # run tests with: python -m unittest test_tranform # python -m unittest discover @@ -30,8 +30,8 @@ def test_load(self): self.assertEqual(xx, self.x) self.assertEqual(yy, self.y) self.assertEqual(ss, self.sigma) - #print self.x - #print self.y + #print(self.x) + #print(self.y) def assert_expr(self, expr, val, places=None): expr_val = self.ftk.calculate_expr(expr)