diff --git a/examples/airfoil3d.py b/examples/airfoil3d.py index 1845660..1f2ba48 100644 --- a/examples/airfoil3d.py +++ b/examples/airfoil3d.py @@ -72,7 +72,7 @@ def deform_wing(p): mesh_info.set_holes([(0, 0, 0.5)]) mesh = build(mesh_info) - print("%d elements" % len(mesh.elements)) + print(f"{len(mesh.elements)} elements") mesh.write_vtk("airfoil3d.vtk") diff --git a/examples/box-in-box.py b/examples/box-in-box.py index 8129ec7..9e4bdde 100644 --- a/examples/box-in-box.py +++ b/examples/box-in-box.py @@ -35,7 +35,7 @@ def main(): mesh = build(mesh_info, max_volume=0.06, volume_constraints=True, attributes=True) - print("%d elements" % len(mesh.elements)) + print(f"{len(mesh.elements)} elements") mesh.write_vtk("box-in-box.vtk") diff --git a/examples/jw_meshtools.py b/examples/jw_meshtools.py index c9ed54d..affb36b 100644 --- a/examples/jw_meshtools.py +++ b/examples/jw_meshtools.py @@ -104,7 +104,7 @@ def PlotMeshNumbers(p, t, edges=None, pltshow=True): plt.triplot(p[:, 0], p[:, 1], t[:, 0:3]) # annotate nodes for i in range(len(p)): - buf = "%i" % (i) + buf = f"{i}" x = p[i, 0] y = p[i, 1] plt.plot(x, y, "ok", markersize=5) @@ -112,13 +112,13 @@ def PlotMeshNumbers(p, t, edges=None, pltshow=True): # annotate elements for n in range(len(t)): - buf = "%i" % (n) + buf = f"{n}" ps = (p[t[n, 0], :] + p[t[n, 1], :] + p[t[n, 2], :]) / 3.0 plt.text(ps[0], ps[1], buf, color="b", fontsize=9) if edges != []: for n in range(len(edges)): - buf = "%i" % (n) + buf = f"{n}" ps = (p[edges[n][0], :] + p[edges[n][1], :]) / 2.0 plt.text(ps[0], ps[1], buf, color="g", fontsize=11) @@ -2255,8 +2255,8 @@ def DoBemBoundary(AllBound, show=True, show_numbers=False): if show_numbers: for i in range(len(x)): - buf = " %i" % (Z["first"] + i) - plt.text(x[i], y[i], buf, color="k", fontsize=13) + buf = Z["first"] + i + plt.text(x[i], y[i], f" {buf}", color="k", fontsize=13) plt.show() return VecXm, VecB, Curves, D_nodes, N_nodes, I_nodes diff --git a/examples/mesh_ply.py b/examples/mesh_ply.py index 40adff0..382ed68 100644 --- a/examples/mesh_ply.py +++ b/examples/mesh_ply.py @@ -20,7 +20,7 @@ def main(): builder.set(mi) mi.set_holes([builder.center()]) mesh = build(mi) - print("%d elements" % len(mesh.elements)) + print(f"{len(mesh.elements)} elements") mesh.write_vtk("out.vtk") diff --git a/examples/write_dolfin.py b/examples/write_dolfin.py index 8a3f5e6..8ea188d 100644 --- a/examples/write_dolfin.py +++ b/examples/write_dolfin.py @@ -8,31 +8,29 @@ def main(): mesh = triangle.build(info, max_volume=1e-3, min_angle=25) print( - """ + f""" - + """ - % len(mesh.points) ) for i, pt in enumerate(mesh.points): - print('' % (i, pt[0], pt[1])) + print(f'') print( - """ + f""" - + """ - % len(mesh.elements) ) for i, element in enumerate(mesh.elements): print( - '' - % (i, element[0], element[1], element[2]) + f'' ) print( diff --git a/meshpy/common.py b/meshpy/common.py index 9f9b040..e90cbe3 100644 --- a/meshpy/common.py +++ b/meshpy/common.py @@ -118,8 +118,8 @@ def write_neu(self, outfile, bc=None, periodicity=None, # nodes --------------------------------------------------------------- outfile.write("NODAL COORDINATES 2.1.2\n") for i, pt in enumerate(self.points): - outfile.write("%d %s\n" % - (i+1, " ".join(repr(c) for c in pt))) + point = " ".join(repr(c) for c in pt) + outfile.write(f"{i + 1} {point}\n") outfile.write("ENDOFSECTION\n") # elements ------------------------------------------------------------ @@ -130,9 +130,8 @@ def write_neu(self, outfile, bc=None, periodicity=None, eltype = 6 for i, el in enumerate(self.elements): - outfile.write("%8d%3d%3d %s\n" % - (i+1, eltype, len(el), - "".join("%8d" % (p+1) for p in el))) + element = "".join(f"{p + 1:>8d}" for p in el) + outfile.write(f"{i + 1:>8d}{eltype:>3d}{len(el):>3d} {element}\n") outfile.write("ENDOFSECTION\n") # element groups ------------------------------------------------------ @@ -142,8 +141,11 @@ def write_neu(self, outfile, bc=None, periodicity=None, grp_elements = list(range(len(self.elements))) material = 1 flags = 0 - outfile.write("GROUP:%11d ELEMENTS:%11d MATERIAL:%11s NFLAGS: %11d\n" - % (1, len(grp_elements), repr(material), flags)) + outfile.write( + f"GROUP:{1:>11d} " + f"ELEMENTS:{len(grp_elements):>11d} " + f"MATERIAL:{material!r:>11} " + f"NFLAGS: {flags:>11d}\n") outfile.write((f"epsilon: {material}\n").rjust(32)) # FIXME outfile.write("0\n") outfile.write(_linebreak_list([str(i+1) for i in grp_elements], @@ -180,7 +182,7 @@ def write_neu(self, outfile, bc=None, periodicity=None, face2el.setdefault(face, []).append((ti, fi+1)) else: - raise ValueError("invalid number of dimensions (%d)" % dim) + raise ValueError(f"invalid number of dimensions: {dim}") # actually output bc sections if not self.faces.allocated: @@ -208,21 +210,16 @@ def write_neu(self, outfile, bc=None, periodicity=None, # regular BC bc_name, bc_code = bc[bc_marker] - outfile.write("%32s%8d%8d%8d%8d\n" - % (bc_name, - 1, # face BC - len(face_indices), - 0, # zero additional values per face, - bc_code) + outfile.write( + f"{bc_name:>32}{1:>8d}{len(face_indices):>8d}" + f"{0:>8d}{bc_code:>8d}\n" ) else: # periodic BC - outfile.write("%s%s%8d%8d%8d\n" - % ("periodic", " ".join(repr(p) for p in periods), - len(face_indices), - 0, # zero additional values per face, - 0) + periods_repr = " ".join(repr(p) for p in periods) + outfile.write( + f"periodic{periods_repr}{len(face_indices):>8d}{0:>8d}{0:>8d}\n" ) for fi in face_indices: @@ -232,8 +229,8 @@ def write_neu(self, outfile, bc=None, periodicity=None, el_index, el_face_number = adj_el[0] - outfile.write("%10d%5d%5d\n" % - (el_index+1, eltype, el_face_number)) + outfile.write( + f"{el_index + 1:>10d}{eltype:>5d}{el_face_number:>5d}\n") outfile.write("ENDOFSECTION\n") @@ -243,8 +240,7 @@ def write_neu(self, outfile, bc=None, periodicity=None, def dump_array(name, array): - print("array %s: %d elements, %d values per element" - % (name, len(array), array.unit)) + print(f"array {name}: {len(array)} elements, {array.unit} values per element") if len(array) == 0 or array.unit == 0: return @@ -257,6 +253,7 @@ def dump_array(name, array): for i, entry in enumerate(array): if isinstance(entry, list): - print(" %d: %s" % (i, ",".join(str(sub) for sub in entry))) + value = ",".join(str(sub) for sub in entry) + print(f" {i}: {value}") else: - print(" %d: %s" % (i, entry)) + print(f" {i}: {entry}") diff --git a/meshpy/geometry.py b/meshpy/geometry.py index 574819e..ab5d91d 100644 --- a/meshpy/geometry.py +++ b/meshpy/geometry.py @@ -158,7 +158,7 @@ def mesher_module(self): import meshpy.tet return meshpy.tet else: - raise ValueError("unsupported dimensionality %d" % dim) + raise ValueError(f"unsupported dimensionality {dim}") def bounding_box(self): return bounding_box(self.points) @@ -260,7 +260,7 @@ def make_box(a, b, subdivisions=None): facet_markers = [Marker.MINUS_Z, Marker.MINUS_Y, Marker.PLUS_X, Marker.PLUS_Y, Marker.MINUS_X, Marker.PLUS_Z] else: - raise ValueError("unsupported dimension count: %d" % len(a)) + raise ValueError(f"unsupported dimension count: {len(a)}") if subdivisions is not None: if dimensions != 2: @@ -509,10 +509,9 @@ def connect_ring(ring1_idx, ring2_idx, marker): if rz_points[i][0] == 0: assert len(ring_points) == 1 else: - assert len(ring_points) == len(base_shape), \ - ("Ring points length (%d) does not " - "match base shape length (%d)" - % (len(ring_points), len(base_shape))) + assert len(ring_points) == len(base_shape), ( + f"Ring points length ({len(ring_points)}) does not " + f"match base shape length ({len(base_shape)})") rings[i] = ring_points diff --git a/meshpy/tet.py b/meshpy/tet.py index 4c73789..b1c9cf6 100644 --- a/meshpy/tet.py +++ b/meshpy/tet.py @@ -93,10 +93,10 @@ def dump(self): for name in ["points"]: dump_array(name, getattr(self, name)) for ifacet, facet in enumerate(self.faces): - print("facet %d:" % ifacet) + print(f"facet {ifacet}:" % ifacet) for ipolygon, polygon in enumerate(facet.polygons): - print(" polygon %d: vertices [%s]" % - (ipolygon, ",".join(str(vi) for vi in polygon.vertices))) + vertices = ",".join(str(vi) for vi in polygon.vertices) + print(f" polygon {ipolygon}: vertices [{vertices}]") def write_vtk(self, filename): import pyvtk diff --git a/meshpy/triangle.py b/meshpy/triangle.py index 1af0989..34b552b 100644 --- a/meshpy/triangle.py +++ b/meshpy/triangle.py @@ -134,7 +134,7 @@ def build(mesh_info, verbose=False, refinement_func=None, attributes=False, opts += "q" if mesh_order is not None: - opts += "o%d" % mesh_order + opts += f"o{mesh_order}" if verbose: opts += "VV"