From 925962fdef99aae5341ca37b14cdfe549a76459b Mon Sep 17 00:00:00 2001 From: Matthew Larson Date: Tue, 30 Apr 2024 09:30:44 -0500 Subject: [PATCH 1/3] Remove py2 format strings in attrs, dsets, dtypes --- h5pyd/__init__.py | 6 +++--- h5pyd/_apps/hsget.py | 2 +- h5pyd/_hl/attrs.py | 7 +++---- h5pyd/_hl/dataset.py | 21 ++++++++++----------- h5pyd/_hl/datatype.py | 4 ++-- h5pyd/version.py | 28 ++++++++++------------------ 6 files changed, 29 insertions(+), 39 deletions(-) diff --git a/h5pyd/__init__.py b/h5pyd/__init__.py index e337fa0..0474e1e 100644 --- a/h5pyd/__init__.py +++ b/h5pyd/__init__.py @@ -34,12 +34,12 @@ __doc__ = \ - """ + f""" This is the h5pyd package, a Python interface to the HDF REST Server. - Version %s + Version {version.version} - """ % (version.version) + """ def enable_ipython_completer(): diff --git a/h5pyd/_apps/hsget.py b/h5pyd/_apps/hsget.py index b78784c..d2f4e3b 100755 --- a/h5pyd/_apps/hsget.py +++ b/h5pyd/_apps/hsget.py @@ -17,7 +17,7 @@ import h5py import h5pyd except ImportError as e: - sys.stderr.write("ERROR : %s : install it to use this utility...\n" % str(e)) + sys.stderr.write(f"ERROR : {str(e)} : install it to use this utility...\n") sys.exit(1) if __name__ == "__main__": diff --git a/h5pyd/_hl/attrs.py b/h5pyd/_hl/attrs.py index d4d64a0..c9e6a3e 100644 --- a/h5pyd/_hl/attrs.py +++ b/h5pyd/_hl/attrs.py @@ -222,8 +222,7 @@ def create(self, name, data, shape=None, dtype=None): if is_complex: raise TypeError( - 'Wrong committed datatype for complex numbers: %s' % - dtype.name) + f'Wrong committed datatype for complex numbers: {dtype.name}') elif dtype is None: if data.dtype.kind == 'U': # use vlen for unicode strings @@ -243,7 +242,7 @@ def create(self, name, data, shape=None, dtype=None): # Make sure the subshape matches the last N axes' sizes. if shape[-len(subshape):] != subshape: - raise ValueError("Array dtype shape %s is incompatible with data shape %s" % (subshape, shape)) + raise ValueError(f"Array dtype shape {subshape} is incompatible with data shape {shape}") # New "advertised" shape and dtype shape = shape[0:len(shape) - len(subshape)] @@ -383,4 +382,4 @@ def __contains__(self, name): def __repr__(self): if not self._parent.id.id: return "" - return "" % id(self._parent.id) + return f"" diff --git a/h5pyd/_hl/dataset.py b/h5pyd/_hl/dataset.py index 992203c..da61c63 100644 --- a/h5pyd/_hl/dataset.py +++ b/h5pyd/_hl/dataset.py @@ -50,7 +50,7 @@ def readtime_dtype(basetype, names): elif itemsize == 8: return numpy.dtype(numpy.complex64) else: - TypeError("Unsupported dtype for complex numbers: %s" % basetype) + TypeError(f"Unsupported dtype for complex numbers: {basetype}") if len(names) == 0: # Not compound, or we want all fields return basetype @@ -60,7 +60,7 @@ def readtime_dtype(basetype, names): for name in names: # Check all names are legal if name not in basetype.names: - raise ValueError("Field %s does not appear in this type." % name) + raise ValueError(f"Field {name} does not appear in this type.") return numpy.dtype([(name, basetype.fields[name][0]) for name in names]) @@ -721,7 +721,7 @@ def __init__(self, bind): """Create a new Dataset object by binding to a low-level DatasetID.""" if not isinstance(bind, DatasetID): - raise ValueError("%s is not a DatasetID" % bind) + raise ValueError(f"{bind} is not a DatasetID") HLObject.__init__(self, bind) self._dcpl = self.id.dcpl_json @@ -781,7 +781,7 @@ def resize(self, size, axis=None): if axis is not None: if not (axis >= 0 and axis < self.id.rank): - raise ValueError("Invalid axis (0 to %s allowed)" % (self.id.rank - 1)) + raise ValueError(f"Invalid axis (0 to {self.id.rank - 1} allowed)") try: newlen = int(size) except TypeError: @@ -1404,8 +1404,7 @@ def __setitem__(self, args, val): ): if self.dtype.kind != "V" or self.dtype.names != ("r", "i"): raise TypeError( - "Wrong dataset dtype for complex number values: %s" - % self.dtype.fields + f"Wrong dataset dtype for complex number values: {self.dtype.fields}" ) if isinstance(val, complex): val = numpy.asarray(val, dtype=type(val)) @@ -1425,7 +1424,7 @@ def __setitem__(self, args, val): if len(names) == 1 and self.dtype.fields is not None: # Single field selected for write, from a non-array source if not names[0] in self.dtype.fields: - raise ValueError("No such field for indexing: %s" % names[0]) + raise ValueError(f"No such field for indexing: {names[0]}") dtype = self.dtype.fields[names[0]][0] cast_compound = True else: @@ -1456,8 +1455,8 @@ def __setitem__(self, args, val): shp = self.dtype.subdtype[1] # type shape valshp = val.shape[-len(shp):] if valshp != shp: # Last dimension has to match - raise TypeError("When writing to array types,\ - last N dimensions have to match (got %s, but should be %s)" % (valshp, shp,)) + raise TypeError(f"When writing to array types,\ + last N dimensions have to match (got {valshp}, but should be {shp})") mtype = h5t.py_create(numpy.dtype((val.dtype, shp))) mshape = val.shape[0:len(val.shape)-len(shp)] """ @@ -1469,8 +1468,8 @@ def __setitem__(self, args, val): raise TypeError("Illegal slicing argument (not a compound dataset)") mismatch = [x for x in names if x not in self.dtype.fields] if len(mismatch) != 0: - mismatch = ", ".join('"%s"' % x for x in mismatch) - raise ValueError("Illegal slicing argument (fields %s not in dataset type)" % mismatch) + mismatch = ", ".join(f"{x}" for x in mismatch) + raise ValueError(f"Illegal slicing argument (fields {mismatch} not in dataset type)") # Use mtype derived from array (let DatasetID.write figure it out) else: diff --git a/h5pyd/_hl/datatype.py b/h5pyd/_hl/datatype.py index 82e091a..309dae3 100644 --- a/h5pyd/_hl/datatype.py +++ b/h5pyd/_hl/datatype.py @@ -42,7 +42,7 @@ def __init__(self, bind): """ if not isinstance(bind, TypeID): # todo: distinguish type from other hl objects - raise ValueError("%s is not a TypeID" % bind) + raise ValueError(f"{bind} is not a TypeID") HLObject.__init__(self, bind) self._dtype = createDataType(self.id.type_json) @@ -55,7 +55,7 @@ def __repr__(self): namestr = '("anonymous")' else: name = pp.basename(pp.normpath(self.name)) - namestr = '"%s"' % (name if name != '' else '/') + namestr = f"{name if name != '' else '/'}" if name: namestr = f'"{name}"' else: diff --git a/h5pyd/version.py b/h5pyd/version.py index e9a9c88..34ad8a0 100644 --- a/h5pyd/version.py +++ b/h5pyd/version.py @@ -31,26 +31,18 @@ api_version_tuple = (0, 18, 0) api_version = "0.18.0" -__doc__ = """\ -This is h5pyd **%s** +__doc__ = f"""\ +This is h5pyd **{version}** -""" % ( - version -) +""" -info = """\ +info = f"""\ Summary of the h5pyd configuration --------------------------------- -h5pyd %(h5pyd)s -Python %(python)s -sys.platform %(platform)s -sys.maxsize %(maxsize)s -numpy %(numpy)s -""" % { - "h5pyd": version, - "python": sys.version, - "platform": sys.platform, - "maxsize": sys.maxsize, - "numpy": numpy.__version__, -} +h5pyd {version} +Python {sys.version} +sys.platform {sys.platform} +sys.maxsize {sys.maxsize} +numpy {numpy.__version__} +""" From 84521915cf589dc6b5dc9c524cd9962fab3737da Mon Sep 17 00:00:00 2001 From: Matthew Larson Date: Tue, 30 Apr 2024 09:48:15 -0500 Subject: [PATCH 2/3] Remove py2 strings in dims, groups, sel, h5type --- h5pyd/_hl/dims.py | 2 +- h5pyd/_hl/group.py | 8 ++++---- h5pyd/_hl/h5type.py | 4 ++-- h5pyd/_hl/selections.py | 8 ++++---- h5pyd/_hl/table.py | 4 ++-- 5 files changed, 13 insertions(+), 13 deletions(-) diff --git a/h5pyd/_hl/dims.py b/h5pyd/_hl/dims.py index aadbfd8..e4b31e5 100644 --- a/h5pyd/_hl/dims.py +++ b/h5pyd/_hl/dims.py @@ -387,7 +387,7 @@ def __iter__(self): def __repr__(self): if not self._id: return '' - return '' % self._id + return f'' def create_scale(self, dset, name=''): ''' Create a new dimension, from an initial scale. diff --git a/h5pyd/_hl/group.py b/h5pyd/_hl/group.py index 831b377..b6b4695 100644 --- a/h5pyd/_hl/group.py +++ b/h5pyd/_hl/group.py @@ -56,7 +56,7 @@ def __init__(self, bind, **kwargs): """ if not isinstance(bind, GroupID): - raise ValueError("%s is not a GroupID" % bind) + raise ValueError(f"{bind} is not a GroupID") HLObject.__init__(self, bind, **kwargs) self._req_prefix = "/groups/" + self.id.uuid self._link_db = {} # cache for links @@ -544,7 +544,7 @@ def require_group(self, name): return self.create_group(name) grp = self[name] if not isinstance(grp, Group): - raise TypeError("Incompatible object (%s) already exists" % grp.__class__.__name__) + raise TypeError(f"Incompatible object ({grp.__class__.__name__}) already exists") return grp def getObjByUuid(self, uuid, collection_type=None): @@ -1165,7 +1165,7 @@ def __init__(self, path): self._path = str(path) def __repr__(self): - return '' % self.path + return f'' class ExternalLink(object): @@ -1188,7 +1188,7 @@ def __init__(self, filename, path): self._path = str(path) def __repr__(self): - return '' % (self.path, self.filename) + return f'' class UserDefinedLink(object): diff --git a/h5pyd/_hl/h5type.py b/h5pyd/_hl/h5type.py index eb2afe2..9ccd0f5 100644 --- a/h5pyd/_hl/h5type.py +++ b/h5pyd/_hl/h5type.py @@ -168,7 +168,7 @@ def special_dtype(**kwds): return dt - raise TypeError('Unknown special type "%s"' % name) + raise TypeError(f'Unknown special type "{name}"') def check_vlen_dtype(dt): @@ -265,7 +265,7 @@ class (either Reference or RegionReference). Returns None if the dtype name, dt = kwds.popitem() if name not in ('vlen', 'enum', 'ref'): - raise TypeError('Unknown special type "%s"' % name) + raise TypeError(f'Unknown special type "{name}"') try: return dt.metadata[name] diff --git a/h5pyd/_hl/selections.py b/h5pyd/_hl/selections.py index 26cacd7..72b7e5a 100644 --- a/h5pyd/_hl/selections.py +++ b/h5pyd/_hl/selections.py @@ -400,7 +400,7 @@ def broadcast(self, target_shape): """ if self._shape == (): if np.product(target_shape) != 1: - raise TypeError("Can't broadcast %s to scalar" % target_shape) + raise TypeError(f"Can't broadcast {target_shape} to scalar") self._id.select_all() yield self._id return @@ -419,7 +419,7 @@ def broadcast(self, target_shape): if t == 1 or count[-idx] == t: tshape.append(t) else: - raise TypeError("Can't broadcast %s -> %s" % (target_shape, count)) + raise TypeError(f"Can't broadcast {target_shape} -> {count}") tshape.reverse() tshape = tuple(tshape) @@ -708,7 +708,7 @@ def guess_shape(sid): return tuple() elif sel_class != 'H5S_SIMPLE': - raise TypeError("Unrecognized dataspace class %s" % sel_class) + raise TypeError(f"Unrecognized dataspace class {sel_class}") # We have a "simple" (rank >= 1) dataspace @@ -727,7 +727,7 @@ def guess_shape(sid): return (N,) elif sel_type != H5S_SELECT_HYPERSLABS: - raise TypeError("Unrecognized selection method %s" % sel_type) + raise TypeError(f"Unrecognized selection method {sel_type}") # We have a hyperslab-based selection diff --git a/h5pyd/_hl/table.py b/h5pyd/_hl/table.py index bb78cef..16e6588 100644 --- a/h5pyd/_hl/table.py +++ b/h5pyd/_hl/table.py @@ -87,7 +87,7 @@ def __init__(self, bind): """ if not isinstance(bind, DatasetID): - raise ValueError("%s is not a DatasetID" % bind) + raise ValueError(f"{bind} is not a DatasetID") Dataset.__init__(self, bind) if len(self._dtype) < 1: @@ -144,7 +144,7 @@ def readtime_dtype(basetype, names): for name in names: # Check all names are legal if name not in basetype.names: - raise ValueError("Field %s does not appear in this type." % name) + raise ValueError(f"Field {name} does not appear in this type.") return numpy.dtype([(name, basetype.fields[name][0]) for name in names]) From 7b9a2e07552c757287f3d24158f4f1bb921385e8 Mon Sep 17 00:00:00 2001 From: Matthew Larson Date: Tue, 30 Apr 2024 10:00:05 -0500 Subject: [PATCH 3/3] Remove py2 fstrings in tests --- test/apps/common.py | 20 ++++++++++---------- test/hl/common.py | 20 ++++++++++---------- test/hl/test_dataset.py | 2 +- 3 files changed, 21 insertions(+), 21 deletions(-) diff --git a/test/apps/common.py b/test/apps/common.py index 3ef97a5..836206c 100644 --- a/test/apps/common.py +++ b/test/apps/common.py @@ -113,7 +113,7 @@ def assertSameElements(self, a, b): if x == y: match = True if not match: - raise AssertionError("Item '%s' appears in a but not b" % x) + raise AssertionError(f"Item '{x}' appears in a but not b") for x in b: match = False @@ -121,7 +121,7 @@ def assertSameElements(self, a, b): if x == y: match = True if not match: - raise AssertionError("Item '%s' appears in b but not a" % x) + raise AssertionError(f"Item '{x}' appears in b but not a") def assertArrayEqual(self, dset, arr, message=None, precision=None): """ Make sure dset and arr have the same shape, dtype and contents, to @@ -134,41 +134,41 @@ def assertArrayEqual(self, dset, arr, message=None, precision=None): if message is None: message = '' else: - message = ' (%s)' % message + message = f' ({message})' if np.isscalar(dset) or np.isscalar(arr): self.assertTrue( np.isscalar(dset) and np.isscalar(arr), - 'Scalar/array mismatch ("%r" vs "%r")%s' % (dset, arr, message) + f'Scalar/array mismatch ("{dset}" vs "{arr}"){message}' ) self.assertTrue( dset - arr < precision, - "Scalars differ by more than %.3f%s" % (precision, message) + f"Scalars differ by more than {precision:.3}{message}" ) return self.assertTrue( dset.shape == arr.shape, - "Shape mismatch (%s vs %s)%s" % (dset.shape, arr.shape, message) + f"Shape mismatch ({dset.shape} vs {arr.shape}){message}" ) self.assertTrue( dset.dtype == arr.dtype, - "Dtype mismatch (%s vs %s)%s" % (dset.dtype, arr.dtype, message) + f"Dtype mismatch ({dset.dtype} vs {arr.dtype}){message}" ) if arr.dtype.names is not None: for n in arr.dtype.names: - message = '[FIELD %s] %s' % (n, message) + message = f'[FIELD {n}] {message}' self.assertArrayEqual(dset[n], arr[n], message=message, precision=precision) elif arr.dtype.kind in ('i', 'f'): self.assertTrue( np.all(np.abs(dset[...] - arr[...]) < precision), - "Arrays differ by more than %.3f%s" % (precision, message) + f"Arrays differ by more than {precision:.3}{message}" ) else: self.assertTrue( np.all(dset[...] == arr[...]), - "Arrays are not equal (dtype %s) %s" % (arr.dtype.str, message) + f"Arrays are not equal (dtype {arr.dtype.str}) {message}" ) def assertNumpyBehavior(self, dset, arr, s): diff --git a/test/hl/common.py b/test/hl/common.py index 9abccce..f13704d 100644 --- a/test/hl/common.py +++ b/test/hl/common.py @@ -148,7 +148,7 @@ def assertSameElements(self, a, b): if x == y: match = True if not match: - raise AssertionError("Item '%s' appears in a but not b" % x) + raise AssertionError(f"Item '{x}' appears in a but not b") for x in b: match = False @@ -156,7 +156,7 @@ def assertSameElements(self, a, b): if x == y: match = True if not match: - raise AssertionError("Item '%s' appears in b but not a" % x) + raise AssertionError(f"Item '{x}' appears in b but not a") def assertArrayEqual(self, dset, arr, message=None, precision=None): """ Make sure dset and arr have the same shape, dtype and contents, to @@ -169,41 +169,41 @@ def assertArrayEqual(self, dset, arr, message=None, precision=None): if message is None: message = '' else: - message = ' (%s)' % message + message = f' ({message})' if np.isscalar(dset) or np.isscalar(arr): self.assertTrue( np.isscalar(dset) and np.isscalar(arr), - 'Scalar/array mismatch ("%r" vs "%r")%s' % (dset, arr, message) + f'Scalar/array mismatch ("{dset}" vs "{arr}"){message}' ) self.assertTrue( dset - arr < precision, - "Scalars differ by more than %.3f%s" % (precision, message) + f"Scalars differ by more than {precision:.3}{message}" ) return self.assertTrue( dset.shape == arr.shape, - "Shape mismatch (%s vs %s)%s" % (dset.shape, arr.shape, message) + f"Shape mismatch ({dset.shape} vs {arr.shape}){message}" ) self.assertTrue( dset.dtype == arr.dtype, - "Dtype mismatch (%s vs %s)%s" % (dset.dtype, arr.dtype, message) + f"Dtype mismatch ({dset.dtype} vs {arr.dtype}){message}" ) if arr.dtype.names is not None: for n in arr.dtype.names: - message = '[FIELD %s] %s' % (n, message) + message = f'[FIELD {n}] {message}' self.assertArrayEqual(dset[n], arr[n], message=message, precision=precision) elif arr.dtype.kind in ('i', 'f'): self.assertTrue( np.all(np.abs(dset[...] - arr[...]) < precision), - "Arrays differ by more than %.3f%s" % (precision, message) + f"Arrays differ by more than {precision:.3}{message}" ) else: self.assertTrue( np.all(dset[...] == arr[...]), - "Arrays are not equal (dtype %s) %s" % (arr.dtype.str, message) + f"Arrays are not equal (dtype {arr.dtype.str}) {message}" ) def assertNumpyBehavior(self, dset, arr, s): diff --git a/test/hl/test_dataset.py b/test/hl/test_dataset.py index dd0043f..c088774 100644 --- a/test/hl/test_dataset.py +++ b/test/hl/test_dataset.py @@ -125,7 +125,7 @@ def test_long_double(self): """ Confirm that the default dtype is float """ dset = self.f.create_dataset('foo', (63,), dtype=np.longdouble) if platform.machine() in ['ppc64le']: - print("Storage of long double deactivated on %s" % platform.machine()) + print(f"Storage of long double deactivated on {platform.machine()}") else: self.assertEqual(dset.dtype, np.longdouble)