From 17ef4a7bb8188844f45d16408dc013051aa49c95 Mon Sep 17 00:00:00 2001 From: Jonathan Daniel Date: Mon, 25 May 2020 19:15:23 +0300 Subject: [PATCH 1/2] MNT: Fix configparser + python 2 deprecations Also some style --- doc/source/devel/register_me.py | 12 +++++------- nibabel/arrayproxy.py | 2 +- nibabel/data.py | 2 +- nibabel/ecat.py | 2 +- nibabel/tests/test_round_trip.py | 18 +++++++++--------- nisext/sexts.py | 17 +++++------------ tools/bisect_nose.py | 7 ++++--- tools/gitwash_dumper.py | 2 +- versioneer.py | 10 +++------- 9 files changed, 30 insertions(+), 42 deletions(-) diff --git a/doc/source/devel/register_me.py b/doc/source/devel/register_me.py index b377a2d1f7..d2f571f04f 100644 --- a/doc/source/devel/register_me.py +++ b/doc/source/devel/register_me.py @@ -1,10 +1,7 @@ from os.path import join as pjoin, expanduser, abspath, dirname import sys -# Python 3 compatibility -try: - import configparser as cfp -except ImportError: - import ConfigParser as cfp +import configparser as cfp + if sys.platform == 'win32': HOME_INI = pjoin(expanduser('~'), '_dpkg', 'local.dsource') @@ -15,6 +12,7 @@ OUR_META = pjoin(OUR_PATH, 'meta.ini') DISCOVER_INIS = {'user': HOME_INI, 'system': SYS_INI} + def main(): # Get ini file to which to write try: @@ -23,7 +21,7 @@ def main(): reg_to = 'user' if reg_to in ('user', 'system'): ini_fname = DISCOVER_INIS[reg_to] - else: # it is an ini file name + else: # it is an ini file name ini_fname = reg_to # Read parameters for our distribution @@ -42,7 +40,7 @@ def main(): dsource.set(name, version, OUR_PATH) dsource.write(file(ini_fname, 'wt')) - print 'Registered package %s, %s to %s' % (name, version, ini_fname) + print('Registered package %s, %s to %s' % (name, version, ini_fname)) if __name__ == '__main__': diff --git a/nibabel/arrayproxy.py b/nibabel/arrayproxy.py index b45405b6fb..62de283071 100644 --- a/nibabel/arrayproxy.py +++ b/nibabel/arrayproxy.py @@ -79,7 +79,7 @@ class ArrayProxy(object): * get_slope_inter A header should also have a 'copy' method. This requirement will go away - when the deprecated 'header' propoerty goes away. + when the deprecated 'header' property goes away. This implementation allows us to deal with Analyze and its variants, including Nifti1, and with the MGH format. diff --git a/nibabel/data.py b/nibabel/data.py index 6208ebe7d5..cbf35819c1 100644 --- a/nibabel/data.py +++ b/nibabel/data.py @@ -124,7 +124,7 @@ def __init__(self, base_path, config_filename=None): Datasource.__init__(self, base_path) if config_filename is None: config_filename = 'config.ini' - self.config = configparser.SafeConfigParser() + self.config = configparser.ConfigParser() cfg_file = self.get_filename(config_filename) readfiles = self.config.read(cfg_file) if not readfiles: diff --git a/nibabel/ecat.py b/nibabel/ecat.py index 8a945bc887..e5af36cb1f 100644 --- a/nibabel/ecat.py +++ b/nibabel/ecat.py @@ -543,7 +543,7 @@ def get_shape(self, frame=0): x = subhdr['x_dimension'].item() y = subhdr['y_dimension'].item() z = subhdr['z_dimension'].item() - return (x, y, z) + return x, y, z def get_nframes(self): """returns number of frames""" diff --git a/nibabel/tests/test_round_trip.py b/nibabel/tests/test_round_trip.py index 79d785932d..b20cd45f19 100644 --- a/nibabel/tests/test_round_trip.py +++ b/nibabel/tests/test_round_trip.py @@ -138,7 +138,7 @@ def check_arr(test_id, V_in, in_type, out_type, scaling_type): rel_err = np.abs(top / arr) abs_err = np.abs(top) if slope == 1: # integers output, offset only scaling - if set((in_type, out_type)) == set((np.int64, np.uint64)): + if {in_type, out_type} == {np.int64, np.uint64}: # Scaling to or from 64 bit ints can go outside range of continuous # integers for float64 and thus lose precision; take this into # account @@ -181,14 +181,14 @@ def check_arr(test_id, V_in, in_type, out_type, scaling_type): rel_mx_e = rel_err[abs_fails].max() else: rel_mx_e = None - print (test_id, - np.dtype(in_type).str, - np.dtype(out_type).str, - exp_abs_mx_e, - abs_mx_e, - rel_thresh, - rel_mx_e, - slope, inter) + print(test_id, + np.dtype(in_type).str, + np.dtype(out_type).str, + exp_abs_mx_e, + abs_mx_e, + rel_thresh, + rel_mx_e, + slope, inter) # To help debugging failures with --pdb-failure np.nonzero(all_fails) assert this_test diff --git a/nisext/sexts.py b/nisext/sexts.py index b0d34348e5..c8090f5eac 100644 --- a/nisext/sexts.py +++ b/nisext/sexts.py @@ -2,16 +2,8 @@ import os from os.path import join as pjoin, split as psplit, splitext -import sys -PY3 = sys.version_info[0] >= 3 -if PY3: - string_types = str, -else: - string_types = basestring, -try: - from ConfigParser import ConfigParser -except ImportError: - from configparser import ConfigParser + +from configparser import ConfigParser from distutils.version import LooseVersion from distutils.command.build_py import build_py @@ -19,6 +11,7 @@ from distutils import log + def get_comrec_build(pkg_dir, build_cmd=build_py): """ Return extended build command class for recording commit @@ -82,7 +75,7 @@ def _add_append_key(in_dict, key, value): # Append value to in_dict[key] list if key not in in_dict: in_dict[key] = [] - elif isinstance(in_dict[key], string_types): + elif isinstance(in_dict[key], str): in_dict[key] = [in_dict[key]] in_dict[key].append(value) @@ -179,7 +172,7 @@ def version_getter(pkg_name): + msgs['opt suffix']) return # setuptools mode - if optional_tf and not isinstance(optional, string_types): + if optional_tf and not isinstance(optional, str): raise RuntimeError('Not-False optional arg should be string') dependency = pkg_name if version: diff --git a/tools/bisect_nose.py b/tools/bisect_nose.py index d3b1c0e83c..8a1b2cc2fe 100755 --- a/tools/bisect_nose.py +++ b/tools/bisect_nose.py @@ -54,6 +54,7 @@ GOOD = 0 BAD = 1 + def call_or_untestable(cmd): try: caller(cmd) @@ -77,16 +78,16 @@ def main(): args = parser.parse_args() path = os.path.abspath(args.test_path) if args.clean: - print "Cleaning" + print("Cleaning") call_or_untestable('git clean -fxd') if args.build: - print "Building" + print("Building") call_or_untestable('python setup.py build_ext -i') cwd = os.getcwd() tmpdir = tempfile.mkdtemp() try: os.chdir(tmpdir) - print "Testing" + print("Testing") proc = popener('nosetests ' + path) stdout, stderr = proc.communicate() finally: diff --git a/tools/gitwash_dumper.py b/tools/gitwash_dumper.py index 851c0579cd..038fe936fe 100755 --- a/tools/gitwash_dumper.py +++ b/tools/gitwash_dumper.py @@ -79,7 +79,7 @@ def copy_replace(replace_pairs, for rep_glob in rep_globs: fnames += fnmatch.filter(out_fnames, rep_glob) if verbose: - print '\n'.join(fnames) + print('\n'.join(fnames)) for fname in fnames: filename_search_replace(replace_pairs, fname, False) for in_exp, out_exp in renames: diff --git a/versioneer.py b/versioneer.py index 7c8333493e..eec2ab0f07 100644 --- a/versioneer.py +++ b/versioneer.py @@ -276,11 +276,7 @@ """ -from __future__ import print_function -try: - import configparser -except ImportError: - import ConfigParser as configparser +import configparser import errno import json import os @@ -340,9 +336,9 @@ def get_config_from_root(root): # configparser.NoOptionError (if it lacks "VCS="). See the docstring at # the top of versioneer.py for instructions on writing your setup.cfg . setup_cfg = os.path.join(root, "setup.cfg") - parser = configparser.SafeConfigParser() + parser = configparser.ConfigParser() with open(setup_cfg, "r") as f: - parser.readfp(f) + parser.read_file(f) VCS = parser.get("versioneer", "VCS") # mandatory def get(parser, name): From c37d52fa2254f5674b80621d2a96552f7e60df89 Mon Sep 17 00:00:00 2001 From: Jonathan Daniel <36337649+jond01@users.noreply.github.com> Date: Mon, 25 May 2020 22:38:50 +0300 Subject: [PATCH 2/2] Update nibabel/tests/test_round_trip.py Co-authored-by: Chris Markiewicz --- nibabel/tests/test_round_trip.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/nibabel/tests/test_round_trip.py b/nibabel/tests/test_round_trip.py index b20cd45f19..dee4c0637d 100644 --- a/nibabel/tests/test_round_trip.py +++ b/nibabel/tests/test_round_trip.py @@ -181,14 +181,14 @@ def check_arr(test_id, V_in, in_type, out_type, scaling_type): rel_mx_e = rel_err[abs_fails].max() else: rel_mx_e = None - print(test_id, - np.dtype(in_type).str, - np.dtype(out_type).str, - exp_abs_mx_e, - abs_mx_e, - rel_thresh, - rel_mx_e, - slope, inter) + print((test_id, + np.dtype(in_type).str, + np.dtype(out_type).str, + exp_abs_mx_e, + abs_mx_e, + rel_thresh, + rel_mx_e, + slope, inter)) # To help debugging failures with --pdb-failure np.nonzero(all_fails) assert this_test