diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 7acd89b67..8607de368 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,7 +1,7 @@ files: "libpysal\/" repos: - repo: https://github.com/astral-sh/ruff-pre-commit - rev: "v0.3.5" + rev: "v0.5.0" hooks: - id: ruff - id: ruff-format diff --git a/libpysal/cg/kdtree.py b/libpysal/cg/kdtree.py index c050dbf34..6adbe7d62 100644 --- a/libpysal/cg/kdtree.py +++ b/libpysal/cg/kdtree.py @@ -126,8 +126,7 @@ def count_neighbors(self, other, r, p=2): if r > 0.5 * self.circumference: raise ValueError( "r, must not exceed 1/2 circumference of the sphere (%f)." - % self.circumference - * 0.5 + % (self.circumference * 0.5) ) r = sphere.arcdist2linear(r, self.radius) return temp_KDTree.count_neighbors(self, other, r) @@ -208,8 +207,7 @@ def query_ball_point(self, x, r, p=2, eps=0): if r > 0.5 * self.circumference: raise ValueError( "r, must not exceed 1/2 circumference of the sphere (%f)." - % self.circumference - * 0.5 + % (self.circumference * 0.5) ) r = sphere.arcdist2linear(r, self.radius) + FLOAT_EPS * 3 return temp_KDTree.query_ball_point(self, self._toXYZ(x), r, eps=eps) @@ -248,8 +246,7 @@ def query_ball_tree(self, other, r, p=2, eps=0): if r > 0.5 * self.circumference: raise ValueError( "r, must not exceed 1/2 circumference of the sphere (%f)." - % self.circumference - * 0.5 + % (self.circumference * 0.5) ) r = sphere.arcdist2linear(r, self.radius) + FLOAT_EPS * 3 return temp_KDTree.query_ball_tree(self, other, r, eps=eps) @@ -277,8 +274,7 @@ def query_pairs(self, r, p=2, eps=0): if r > 0.5 * self.circumference: raise ValueError( "r, must not exceed 1/2 circumference of the sphere (%f)." - % self.circumference - * 0.5 + % (self.circumference * 0.5) ) r = sphere.arcdist2linear(r, self.radius) + FLOAT_EPS * 3 return temp_KDTree.query_pairs(self, r, eps=eps) @@ -311,8 +307,7 @@ def sparse_distance_matrix(self, other, max_distance, p=2): if max_distance > 0.5 * self.circumference: raise ValueError( "max_distance, must not exceed 1/2 circumference of the sphere (%f)." - % self.circumference - * 0.5 + % (self.circumference * 0.5) ) max_distance = sphere.arcdist2linear(max_distance, self.radius) + FLOAT_EPS * 3 d = temp_KDTree.sparse_distance_matrix(self, other, max_distance) diff --git a/libpysal/cg/shapes.py b/libpysal/cg/shapes.py index 369c4d7d9..650b58e08 100644 --- a/libpysal/cg/shapes.py +++ b/libpysal/cg/shapes.py @@ -58,7 +58,7 @@ def asShape(obj): geo = obj.__geo_interface__ if hasattr(obj, "__geo_interface__") else obj if hasattr(geo, "type"): - raise TypeError("%r does not appear to be a shape object." % (obj)) + raise TypeError(f"{obj!r} does not appear to be a shape object.") geo_type = geo["type"].lower() @@ -68,7 +68,7 @@ def asShape(obj): if geo_type in _geoJSON_type_to_Pysal_type: obj = _geoJSON_type_to_Pysal_type[geo_type].__from_geo_interface__(geo) else: - raise NotImplementedError("%s is not supported at this time." % geo_type) + raise NotImplementedError(f"{geo_type} is not supported at this time.") return obj @@ -942,7 +942,7 @@ def __from_geo_interface__(cls, geo: dict): elif geo["type"].lower() == "multilinestring": verts = [list(map(Point, part)) for part in geo["coordinates"]] else: - raise TypeError("%r is not a Chain." % geo) + raise TypeError(f"{geo!r} is not a Chain.") return cls(verts) @property diff --git a/libpysal/cg/standalone.py b/libpysal/cg/standalone.py index 9bf16135c..e5d3ac7bb 100644 --- a/libpysal/cg/standalone.py +++ b/libpysal/cg/standalone.py @@ -1277,7 +1277,7 @@ def distance_matrix(X, p=2.0, threshold=5e7): X.shape = (X.shape[0], 1) if X.ndim > 2: - msg = "Should be 2D point coordinates: %s dimensions present." % X.ndim + msg = f"Should be 2D point coordinates: {X.ndim} dimensions present." raise TypeError(msg) n, k = X.shape diff --git a/libpysal/cg/tests/test_segmentLocator.py b/libpysal/cg/tests/test_segmentLocator.py index 4d6671411..44fa9b5f1 100644 --- a/libpysal/cg/tests/test_segmentLocator.py +++ b/libpysal/cg/tests/test_segmentLocator.py @@ -17,22 +17,22 @@ def setup_method(self): def test_nearest_1(self): # Center - assert [0, 1, 2, 3] == self.grid.nearest(Point((5.0, 5.0))) + assert self.grid.nearest(Point((5.0, 5.0))) == [0, 1, 2, 3] # Left Edge - assert [0] == self.grid.nearest(Point((0.0, 5.0))) + assert self.grid.nearest(Point((0.0, 5.0))) == [0] # Top Edge - assert [1] == self.grid.nearest(Point((5.0, 10.0))) + assert self.grid.nearest(Point((5.0, 10.0))) == [1] # Right Edge - assert [2] == self.grid.nearest(Point((10.0, 5.0))) + assert self.grid.nearest(Point((10.0, 5.0))) == [2] # Bottom Edge - assert [3] == self.grid.nearest(Point((5.0, 0.0))) + assert self.grid.nearest(Point((5.0, 0.0))) == [3] def test_nearest_2(self): # Left Edge - assert [0, 1, 3] == self.grid.nearest(Point((-100000.0, 5.0))) + assert self.grid.nearest(Point((-100000.0, 5.0))) == [0, 1, 3] # Right Edge - assert [1, 2, 3] == self.grid.nearest(Point((100000.0, 5.0))) + assert self.grid.nearest(Point((100000.0, 5.0))) == [1, 2, 3] # Bottom Edge - assert [0, 2, 3] == self.grid.nearest(Point((5.0, -100000.0))) + assert self.grid.nearest(Point((5.0, -100000.0))) == [0, 2, 3] # Top Edge - assert [0, 1, 2] == self.grid.nearest(Point((5.0, 100000.0))) + assert self.grid.nearest(Point((5.0, 100000.0))) == [0, 1, 2] diff --git a/libpysal/graph/_set_ops.py b/libpysal/graph/_set_ops.py index a2cf62e76..be0cd80f4 100644 --- a/libpysal/graph/_set_ops.py +++ b/libpysal/graph/_set_ops.py @@ -58,9 +58,7 @@ def intersects(self, right): intersection = self._adjacency.index.drop(self.isolates).intersection( right._adjacency.index.drop(right.isolates) ) - if len(intersection) > 0: - return True - return False + return len(intersection) > 0 def intersection(self, right): """ diff --git a/libpysal/graph/_spatial_lag.py b/libpysal/graph/_spatial_lag.py index f591aaa9f..77124958e 100644 --- a/libpysal/graph/_spatial_lag.py +++ b/libpysal/graph/_spatial_lag.py @@ -137,9 +137,7 @@ def _check_ties(focal): """ max_count = focal.weight.max() - if (focal.weight == max_count).sum() > 1: - return True - return False + return (focal.weight == max_count).sum() > 1 def _get_categorical_lag(focal, ties="random"): diff --git a/libpysal/io/fileio.py b/libpysal/io/fileio.py index fbe06007d..d9e934af7 100644 --- a/libpysal/io/fileio.py +++ b/libpysal/io/fileio.py @@ -277,7 +277,7 @@ def cast(self, key, typ): except AssertionError: raise TypeError("Cast objects must be callable.") from None else: - raise KeyError("%s" % key) + raise KeyError(f"{key}") def _cast(self, row) -> list: """ @@ -294,7 +294,7 @@ def _cast(self, row) -> list: r = [] for f, v in zip(self._spec, row, strict=True): try: - if not v and f != str: + if not v and not isinstance(f, str): raise ValueError r.append(f(v)) except ValueError: diff --git a/libpysal/io/iohandlers/arcgis_dbf.py b/libpysal/io/iohandlers/arcgis_dbf.py index 18debe160..42108ba42 100644 --- a/libpysal/io/iohandlers/arcgis_dbf.py +++ b/libpysal/io/iohandlers/arcgis_dbf.py @@ -246,7 +246,7 @@ def write(self, obj, useIdIndex=False): self.pos = self.file.pos else: - raise TypeError("Expected a PySAL weights object, got: %s." % (type(obj))) + raise TypeError(f"Expected a PySAL weights object, got: {type(obj)}.") def flush(self): self._complain_ifclosed(self.closed) diff --git a/libpysal/io/iohandlers/arcgis_swm.py b/libpysal/io/iohandlers/arcgis_swm.py index 149417457..78d69508d 100644 --- a/libpysal/io/iohandlers/arcgis_swm.py +++ b/libpysal/io/iohandlers/arcgis_swm.py @@ -305,7 +305,7 @@ def write(self, obj, useIdIndex=False): # noqa: N803 self._complain_ifclosed(self.closed) if not issubclass(type(obj), W): - raise TypeError("Expected a PySAL weights object, got: %s." % (type(obj))) + raise TypeError(f"Expected a PySAL weights object, got: {type(obj)}.") if (type(obj.id_order[0]) not in (np.int32, np.int64, int)) and not useIdIndex: raise TypeError("ArcGIS SWM files support only integer IDs.") diff --git a/libpysal/io/iohandlers/arcgis_txt.py b/libpysal/io/iohandlers/arcgis_txt.py index 5196c8f52..bf9c3f8ad 100644 --- a/libpysal/io/iohandlers/arcgis_txt.py +++ b/libpysal/io/iohandlers/arcgis_txt.py @@ -233,8 +233,8 @@ def write(self, obj, useIdIndex=False): # noqa: N803 id2i = obj.id2i obj = remap_ids(obj, id2i) - header = "%s\n" % self.varName + header = f"{self.varName}\n" self.file.write(header) self._writelines(obj) else: - raise TypeError("Expected a PySAL weights object, got: %s." % (type(obj))) + raise TypeError(f"Expected a PySAL weights object, got: {type(obj)}.") diff --git a/libpysal/io/iohandlers/dat.py b/libpysal/io/iohandlers/dat.py index af13b82d3..390a16565 100644 --- a/libpysal/io/iohandlers/dat.py +++ b/libpysal/io/iohandlers/dat.py @@ -133,4 +133,4 @@ def write(self, obj): if issubclass(type(obj), W): self._writelines(obj) else: - raise TypeError("Expected a PySAL weights object, got: %s." % (type(obj))) + raise TypeError(f"Expected a PySAL weights object, got: {type(obj)}.") diff --git a/libpysal/io/iohandlers/gal.py b/libpysal/io/iohandlers/gal.py index ca24c6493..bdc941b07 100644 --- a/libpysal/io/iohandlers/gal.py +++ b/libpysal/io/iohandlers/gal.py @@ -254,7 +254,7 @@ def write(self, obj): self.file.write(" ".join(map(str, neighbors)) + "\n") self.pos += 1 else: - raise TypeError("Expected a PySAL weights object, got: %s." % (type(obj))) + raise TypeError(f"Expected a PySAL weights object, got: {type(obj)}.") def close(self): self.file.close() diff --git a/libpysal/io/iohandlers/geobugs_txt.py b/libpysal/io/iohandlers/geobugs_txt.py index 8bd423f60..3ae23d9df 100644 --- a/libpysal/io/iohandlers/geobugs_txt.py +++ b/libpysal/io/iohandlers/geobugs_txt.py @@ -272,13 +272,13 @@ def write(self, obj): weights.extend(obj.weights[i]) self.file.write("list(") - self.file.write("num=c(%s)," % ",".join(map(str, cardinalities))) - self.file.write("adj=c(%s)," % ",".join(map(str, neighbors))) + self.file.write("num=c({}),".format(",".join(map(str, cardinalities)))) + self.file.write("adj=c({}),".format(",".join(map(str, neighbors)))) self.file.write("sumNumNeigh=%i)" % sum(cardinalities)) self.pos += 1 else: - raise TypeError("Expected a PySAL weights object, got: %s." % (type(obj))) + raise TypeError(f"Expected a PySAL weights object, got: {type(obj)}.") def close(self): self.file.close() diff --git a/libpysal/io/iohandlers/gwt.py b/libpysal/io/iohandlers/gwt.py index d2a76918f..6ca87c05a 100644 --- a/libpysal/io/iohandlers/gwt.py +++ b/libpysal/io/iohandlers/gwt.py @@ -330,7 +330,7 @@ def write(self, obj): self._writelines(obj) else: - raise TypeError("Expected a PySAL weights object, got: %s." % (type(obj))) + raise TypeError(f"Expected a PySAL weights object, got: {type(obj)}.") def close(self): self.file.close() diff --git a/libpysal/io/iohandlers/mat.py b/libpysal/io/iohandlers/mat.py index 525f6e333..5e90891d5 100644 --- a/libpysal/io/iohandlers/mat.py +++ b/libpysal/io/iohandlers/mat.py @@ -199,7 +199,7 @@ def write(self, obj): sio.savemat(self.file, {"WEIGHT": w}) self.pos += 1 else: - raise TypeError("Expected a PySAL weights object, got: %s." % (type(obj))) + raise TypeError(f"Expected a PySAL weights object, got: {type(obj)}.") def close(self): self.file.close() diff --git a/libpysal/io/iohandlers/mtx.py b/libpysal/io/iohandlers/mtx.py index 87565c884..cb9d59de1 100644 --- a/libpysal/io/iohandlers/mtx.py +++ b/libpysal/io/iohandlers/mtx.py @@ -256,7 +256,7 @@ def write(self, obj): ) self.pos += 1 else: - raise TypeError("Expected a PySAL weights object, got: %s." % (type(obj))) + raise TypeError(f"Expected a PySAL weights object, got: {type(obj)}.") def close(self): self.file.close() diff --git a/libpysal/io/iohandlers/pyDbfIO.py b/libpysal/io/iohandlers/pyDbfIO.py index 191db3d4a..8a8ac1448 100644 --- a/libpysal/io/iohandlers/pyDbfIO.py +++ b/libpysal/io/iohandlers/pyDbfIO.py @@ -142,7 +142,7 @@ def _get_col(self, key: str) -> list: """ if key not in self._col_index: - raise AttributeError("Field: %s does not exist in header." % key) + raise AttributeError(f"Field: {key} does not exist in header.") prevPos = self.tell() idx, offset = self._col_index[key] diff --git a/libpysal/io/iohandlers/pyShpIO.py b/libpysal/io/iohandlers/pyShpIO.py index 4ee72c1ac..6ed8dcd9d 100644 --- a/libpysal/io/iohandlers/pyShpIO.py +++ b/libpysal/io/iohandlers/pyShpIO.py @@ -152,7 +152,7 @@ def __writer(self, shape): """ if TYPE_TO_STRING[type(shape)] != self.type: - raise TypeError("This file only supports %s type shapes." % self.type) + raise TypeError(f"This file only supports {self.type} type shapes.") rec = {} rec["Shape Type"] = shp_file.SHAPE_TYPES[self.type] diff --git a/libpysal/io/iohandlers/stata_txt.py b/libpysal/io/iohandlers/stata_txt.py index 9a94f6480..50c62e377 100644 --- a/libpysal/io/iohandlers/stata_txt.py +++ b/libpysal/io/iohandlers/stata_txt.py @@ -254,7 +254,7 @@ def write(self, obj, matrix_form=False): self._complain_ifclosed(self.closed) if issubclass(type(obj), W): - header = "%s\n" % obj.n + header = f"{obj.n}\n" self.file.write(header) if matrix_form: @@ -271,9 +271,9 @@ def wgt2line(obs_id, neighbor, _): for id_ in obj.id_order: line = wgt2line(id_, obj.neighbors[id_], obj.weights[id_]) - self.file.write("%s\n" % " ".join(line)) + self.file.write("{}\n".format(" ".join(line))) else: - raise TypeError("Expected a PySAL weights object, got: %s." % (type(obj))) + raise TypeError(f"Expected a PySAL weights object, got: {type(obj)}.") def close(self): self.file.close() diff --git a/libpysal/io/iohandlers/template.py b/libpysal/io/iohandlers/template.py index 21c10562d..e016a06b3 100644 --- a/libpysal/io/iohandlers/template.py +++ b/libpysal/io/iohandlers/template.py @@ -72,7 +72,7 @@ def foobar(c): self.pos += 1 else: - raise TypeError("Expected a string, got: %s." % (type(obj))) + raise TypeError(f"Expected a string, got: {type(obj)}.") # default is to raise "NotImplementedError" def flush(self): @@ -150,7 +150,7 @@ def write(self, obj): self.fileObj.write(result + "\n") self.pos += 1 else: - raise TypeError("Expected a string, got: %s" % (type(obj))) + raise TypeError(f"Expected a string, got: {type(obj)}") def flush(self): self._complain_ifclosed(self.closed) diff --git a/libpysal/io/iohandlers/tests/test_arcgis_dbf.py b/libpysal/io/iohandlers/tests/test_arcgis_dbf.py index 17cb820cd..fc14f8989 100644 --- a/libpysal/io/iohandlers/tests/test_arcgis_dbf.py +++ b/libpysal/io/iohandlers/tests/test_arcgis_dbf.py @@ -31,7 +31,7 @@ def test_read(self): ) assert w.n == 88 assert w.mean_neighbors == 5.25 - assert [1.0, 1.0, 1.0, 1.0] == list(w[1].values()) + assert list(w[1].values()) == [1.0, 1.0, 1.0, 1.0] def test_seek(self): self.test_read() diff --git a/libpysal/io/iohandlers/tests/test_arcgis_swm.py b/libpysal/io/iohandlers/tests/test_arcgis_swm.py index 9254d3253..ece61b801 100644 --- a/libpysal/io/iohandlers/tests/test_arcgis_swm.py +++ b/libpysal/io/iohandlers/tests/test_arcgis_swm.py @@ -22,7 +22,7 @@ def test_read(self): w = self.obj.read() assert w.n == 88 assert w.mean_neighbors == 5.25 - assert [1.0, 1.0, 1.0, 1.0] == list(w[1].values()) + assert list(w[1].values()) == [1.0, 1.0, 1.0, 1.0] def test_seek(self): self.test_read() diff --git a/libpysal/io/iohandlers/tests/test_arcgis_txt.py b/libpysal/io/iohandlers/tests/test_arcgis_txt.py index 19b2b6bc5..e28936a69 100644 --- a/libpysal/io/iohandlers/tests/test_arcgis_txt.py +++ b/libpysal/io/iohandlers/tests/test_arcgis_txt.py @@ -31,7 +31,7 @@ def test_read(self): ) in str(warn[0].message) assert w.n == 3 assert w.mean_neighbors == 2.0 - assert [0.1, 0.05] == list(w[2].values()) + assert list(w[2].values()) == [0.1, 0.05] def test_seek(self): self.test_read() diff --git a/libpysal/io/iohandlers/tests/test_dat.py b/libpysal/io/iohandlers/tests/test_dat.py index d7e0f525d..9bf96da9b 100644 --- a/libpysal/io/iohandlers/tests/test_dat.py +++ b/libpysal/io/iohandlers/tests/test_dat.py @@ -22,7 +22,7 @@ def test_read(self): w = self.obj.read() assert w.n == 49 assert w.mean_neighbors == 4.7346938775510203 - assert [0.5, 0.5] == list(w[5.0].values()) + assert list(w[5.0].values()) == [0.5, 0.5] def test_seek(self): self.test_read() diff --git a/libpysal/io/iohandlers/tests/test_gal.py b/libpysal/io/iohandlers/tests/test_gal.py index c2c8da302..78fcfe67b 100644 --- a/libpysal/io/iohandlers/tests/test_gal.py +++ b/libpysal/io/iohandlers/tests/test_gal.py @@ -15,7 +15,7 @@ def setup_method(self): self.obj = GalIO(test_file, "r") def test___init__(self): - assert self.obj._typ == str + assert self.obj._typ == str # noqa: E721 def test_close(self): f = self.obj diff --git a/libpysal/io/iohandlers/tests/test_geobugs_txt.py b/libpysal/io/iohandlers/tests/test_geobugs_txt.py index 1e97005e3..3a23eb193 100644 --- a/libpysal/io/iohandlers/tests/test_geobugs_txt.py +++ b/libpysal/io/iohandlers/tests/test_geobugs_txt.py @@ -27,12 +27,12 @@ def test_read(self): w_scot = self.obj_scot.read() assert w_scot.n == 56 assert w_scot.mean_neighbors == 4.1785714285714288 - assert [1.0, 1.0, 1.0] == list(w_scot[1].values()) + assert list(w_scot[1].values()) == [1.0, 1.0, 1.0] w_col = self.obj_col.read() assert w_col.n == 49 assert w_col.mean_neighbors == 4.6938775510204085 - assert [0.5, 0.5] == list(w_col[1].values()) + assert list(w_col[1].values()) == [0.5, 0.5] def test_seek(self): self.test_read() diff --git a/libpysal/io/iohandlers/tests/test_gwt.py b/libpysal/io/iohandlers/tests/test_gwt.py index 70227804b..b28dce19e 100644 --- a/libpysal/io/iohandlers/tests/test_gwt.py +++ b/libpysal/io/iohandlers/tests/test_gwt.py @@ -23,7 +23,7 @@ def test_read(self): assert w.n == 168 assert w.mean_neighbors == 16.678571428571427 w.transform = "B" - assert [1.0] == list(w[1].values()) + assert list(w[1].values()) == [1.0] def test_seek(self): self.test_read() diff --git a/libpysal/io/iohandlers/tests/test_mat.py b/libpysal/io/iohandlers/tests/test_mat.py index bff672270..2f2f7b479 100644 --- a/libpysal/io/iohandlers/tests/test_mat.py +++ b/libpysal/io/iohandlers/tests/test_mat.py @@ -23,7 +23,7 @@ def test_read(self): w = self.obj.read() assert w.n == 46 assert w.mean_neighbors == 4.0869565217391308 - assert [1.0, 1.0, 1.0, 1.0] == list(w[1].values()) + assert list(w[1].values()) == [1.0, 1.0, 1.0, 1.0] def test_seek(self): self.test_read() diff --git a/libpysal/io/iohandlers/tests/test_mtx.py b/libpysal/io/iohandlers/tests/test_mtx.py index 11770cddd..c18ee64ba 100644 --- a/libpysal/io/iohandlers/tests/test_mtx.py +++ b/libpysal/io/iohandlers/tests/test_mtx.py @@ -22,9 +22,11 @@ def test_read(self): w = self.obj.read() assert w.n == 49 assert w.mean_neighbors == 4.7346938775510203 - assert [0.33329999999999999, 0.33329999999999999, 0.33329999999999999] == list( - w[1].values() - ) + assert list(w[1].values()) == [ + 0.33329999999999999, + 0.33329999999999999, + 0.33329999999999999, + ] s0 = w.s0 self.obj.seek(0) wsp = self.obj.read(sparse=True) diff --git a/libpysal/io/iohandlers/tests/test_pyDbfIO.py b/libpysal/io/iohandlers/tests/test_pyDbfIO.py index 09c455fde..c504dd144 100644 --- a/libpysal/io/iohandlers/tests/test_pyDbfIO.py +++ b/libpysal/io/iohandlers/tests/test_pyDbfIO.py @@ -27,7 +27,7 @@ def test_tell(self): def test_cast(self): assert self.dbObj._spec == [] self.dbObj.cast("FIPSSTCO", float) - assert self.dbObj._spec[1] == float + assert self.dbObj._spec[1] == float # noqa: E721 def test_seek(self): self.dbObj.seek(0) diff --git a/libpysal/io/iohandlers/tests/test_stata_txt.py b/libpysal/io/iohandlers/tests/test_stata_txt.py index 0c86b5a24..5421f7b74 100644 --- a/libpysal/io/iohandlers/tests/test_stata_txt.py +++ b/libpysal/io/iohandlers/tests/test_stata_txt.py @@ -27,12 +27,12 @@ def test_read(self): w_sparse = self.obj_sparse.read() assert w_sparse.n == 56 assert w_sparse.mean_neighbors == 4.0 - assert [1.0, 1.0, 1.0, 1.0, 1.0] == list(w_sparse[1].values()) + assert list(w_sparse[1].values()) == [1.0, 1.0, 1.0, 1.0, 1.0] w_full = self.obj_full.read() assert w_full.n == 56 assert w_full.mean_neighbors == 4.0 - assert [0.125, 0.125, 0.125, 0.125, 0.125] == list(w_full[1].values()) + assert list(w_full[1].values()) == [0.125, 0.125, 0.125, 0.125, 0.125] def test_seek(self): self.test_read() diff --git a/libpysal/io/iohandlers/tests/test_wk1.py b/libpysal/io/iohandlers/tests/test_wk1.py index b2f1b6180..43157aeaa 100644 --- a/libpysal/io/iohandlers/tests/test_wk1.py +++ b/libpysal/io/iohandlers/tests/test_wk1.py @@ -22,7 +22,7 @@ def test_read(self): w = self.obj.read() assert w.n == 46 assert w.mean_neighbors == 4.0869565217391308 - assert [1.0, 1.0, 1.0, 1.0] == list(w[1].values()) + assert list(w[1].values()) == [1.0, 1.0, 1.0, 1.0] def test_seek(self): self.test_read() diff --git a/libpysal/io/iohandlers/wk1.py b/libpysal/io/iohandlers/wk1.py index 33ca16755..b72860577 100644 --- a/libpysal/io/iohandlers/wk1.py +++ b/libpysal/io/iohandlers/wk1.py @@ -378,7 +378,7 @@ def write(self, obj): self.pos += 1 else: - raise TypeError("Expected a PySAL weights object, got: %s." % (type(obj))) + raise TypeError(f"Expected a PySAL weights object, got: {type(obj)}.") def close(self): self.file.close() diff --git a/libpysal/io/tables.py b/libpysal/io/tables.py index af0928915..89a1df35d 100644 --- a/libpysal/io/tables.py +++ b/libpysal/io/tables.py @@ -36,7 +36,7 @@ def __init__(self, *args, **kwargs): fileio.FileIO.__init__(self, *args, **kwargs) def __repr__(self) -> str: - return "DataTable: %s" % self.dataPath + return f"DataTable: {self.dataPath}" def __len__(self): """__len__ should be implemented by `DataTable` subclasses.""" @@ -64,7 +64,7 @@ def _get_col(self, key): if key in self.header: return self[:, self.header.index(key)] else: - raise AttributeError("Field: %s does not exist in header." % key) + raise AttributeError(f"Field: {key} does not exist in header.") def by_col_array(self, *args): """Return columns of table as a ``numpy.ndarray``. diff --git a/libpysal/io/util/tests/test_weight_converter.py b/libpysal/io/util/tests/test_weight_converter.py index 87a47b1f4..c4b292b9a 100644 --- a/libpysal/io/util/tests/test_weight_converter.py +++ b/libpysal/io/util/tests/test_weight_converter.py @@ -80,7 +80,7 @@ def test_write(self): for ext, dataformat in self.fileformats: if f.lower().endswith(ext): continue - temp_f = tempfile.NamedTemporaryFile(suffix=".%s" % ext) + temp_f = tempfile.NamedTemporaryFile(suffix=f".{ext}") temp_fname = temp_f.name temp_f.close() @@ -135,7 +135,7 @@ def test_weight_convert(self): for ext, dataformat in self.fileformats: if f.lower().endswith(ext): continue - temp_f = tempfile.NamedTemporaryFile(suffix=".%s" % ext) + temp_f = tempfile.NamedTemporaryFile(suffix=f".{ext}") out_file = temp_f.name temp_f.close() out_data_format, use_id_index, matrix_form = dataformat, False, False diff --git a/libpysal/weights/weights.py b/libpysal/weights/weights.py index f24141769..8e582953f 100644 --- a/libpysal/weights/weights.py +++ b/libpysal/weights/weights.py @@ -218,8 +218,8 @@ def __init__( ) ni = len(self.islands) if ni == 1: - message = message + "\n There is 1 island with id: %s." % ( - str(self.islands[0]) + message = ( + message + f"\n There is 1 island with id: {str(self.islands[0])}." ) elif ni > 1: message = message + "\n There are %d islands with ids: %s." % (