From 98986c65625b1171f281eaaeb7b3c0a296e3da04 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Fri, 12 Apr 2024 15:33:40 +1000 Subject: [PATCH] Python: replace '%' with str.format for examples & templates Use modern/preferred string formatting for user facing scripts. --- doc/python_api/examples/bpy.data.py | 2 +- doc/python_api/examples/bpy.props.1.py | 5 +++-- doc/python_api/examples/bpy.types.AddonPreferences.1.py | 6 +++--- doc/python_api/examples/bpy.types.Menu.4.py | 2 +- doc/python_api/examples/bpy.types.Mesh.py | 6 +++--- doc/python_api/examples/bpy.types.Operator.1.py | 2 +- doc/python_api/examples/bpy.types.Operator.3.py | 5 ++--- doc/python_api/examples/mathutils.Color.py | 8 ++++---- doc/python_api/examples/mathutils.Euler.py | 2 +- doc/python_api/examples/mathutils.Quaternion.py | 5 ++--- scripts/templates_py/gizmo_custom_geometry.py | 2 +- scripts/templates_py/operator_file_export.py | 2 +- scripts/templates_py/operator_modal_view3d.py | 2 +- scripts/templates_py/ui_previews_dynamic_enum.py | 2 +- 14 files changed, 25 insertions(+), 26 deletions(-) diff --git a/doc/python_api/examples/bpy.data.py b/doc/python_api/examples/bpy.data.py index c06199d8af3..aa8da013cd4 100644 --- a/doc/python_api/examples/bpy.data.py +++ b/doc/python_api/examples/bpy.data.py @@ -21,4 +21,4 @@ if "Cube" in bpy.data.meshes: import os with open(os.path.splitext(bpy.data.filepath)[0] + ".txt", 'w') as fs: for image in bpy.data.images: - fs.write("%s %d x %d\n" % (image.filepath, image.size[0], image.size[1])) + fs.write("{:s} {:d} x {:d}\n".format(image.filepath, image.size[0], image.size[1])) diff --git a/doc/python_api/examples/bpy.props.1.py b/doc/python_api/examples/bpy.props.1.py index a7055fd4712..6ac2148d28c 100644 --- a/doc/python_api/examples/bpy.props.1.py +++ b/doc/python_api/examples/bpy.props.1.py @@ -21,8 +21,9 @@ class OBJECT_OT_property_example(bpy.types.Operator): def execute(self, context): self.report( - {'INFO'}, 'F: %.2f B: %s S: %r' % - (self.my_float, self.my_bool, self.my_string) + {'INFO'}, "F: {:.2f} B: {:s} S: {!r}".format( + self.my_float, self.my_bool, self.my_string, + ) ) print('My float:', self.my_float) print('My bool:', self.my_bool) diff --git a/doc/python_api/examples/bpy.types.AddonPreferences.1.py b/doc/python_api/examples/bpy.types.AddonPreferences.1.py index 0c82ff924de..c417b1304f9 100644 --- a/doc/python_api/examples/bpy.types.AddonPreferences.1.py +++ b/doc/python_api/examples/bpy.types.AddonPreferences.1.py @@ -53,9 +53,9 @@ class OBJECT_OT_addon_prefs_example(Operator): preferences = context.preferences addon_prefs = preferences.addons[__name__].preferences - info = ("Path: %s, Number: %d, Boolean %r" % - (addon_prefs.filepath, addon_prefs.number, addon_prefs.boolean)) - + info = "Path: {:s}, Number: {:d}, Boolean {!r}".format( + addon_prefs.filepath, addon_prefs.number, addon_prefs.boolean, + ) self.report({'INFO'}, info) print(info) diff --git a/doc/python_api/examples/bpy.types.Menu.4.py b/doc/python_api/examples/bpy.types.Menu.4.py index 4d1ae2d4a19..2ce8725a376 100644 --- a/doc/python_api/examples/bpy.types.Menu.4.py +++ b/doc/python_api/examples/bpy.types.Menu.4.py @@ -18,7 +18,7 @@ import bpy def dump(obj, text): for attr in dir(obj): - print("%r.%s = %s" % (obj, attr, getattr(obj, attr))) + print("{!r}.{:s} = {:s}".format(obj, attr, getattr(obj, attr))) class WM_OT_button_context_test(bpy.types.Operator): diff --git a/doc/python_api/examples/bpy.types.Mesh.py b/doc/python_api/examples/bpy.types.Mesh.py index 8872201e123..b86a85a6a6a 100644 --- a/doc/python_api/examples/bpy.types.Mesh.py +++ b/doc/python_api/examples/bpy.types.Mesh.py @@ -31,10 +31,10 @@ me = bpy.context.object.data uv_layer = me.uv_layers.active.data for poly in me.polygons: - print("Polygon index: %d, length: %d" % (poly.index, poly.loop_total)) + print("Polygon index: {:d}, length: {:d}".format(poly.index, poly.loop_total)) # range is used here to show how the polygons reference loops, # for convenience 'poly.loop_indices' can be used instead. for loop_index in range(poly.loop_start, poly.loop_start + poly.loop_total): - print(" Vertex: %d" % me.loops[loop_index].vertex_index) - print(" UV: %r" % uv_layer[loop_index].uv) + print(" Vertex: {:d}".format(me.loops[loop_index].vertex_index)) + print(" UV: {!r}".format(uv_layer[loop_index].uv)) diff --git a/doc/python_api/examples/bpy.types.Operator.1.py b/doc/python_api/examples/bpy.types.Operator.1.py index a28db7e84bb..dbdf009a3be 100644 --- a/doc/python_api/examples/bpy.types.Operator.1.py +++ b/doc/python_api/examples/bpy.types.Operator.1.py @@ -34,7 +34,7 @@ class SimpleMouseOperator(bpy.types.Operator): def execute(self, context): # rather than printing, use the report function, # this way the message appears in the header, - self.report({'INFO'}, "Mouse coords are %d %d" % (self.x, self.y)) + self.report({'INFO'}, "Mouse coords are {:d} {:d}".format(self.x, self.y)) return {'FINISHED'} def invoke(self, context, event): diff --git a/doc/python_api/examples/bpy.types.Operator.3.py b/doc/python_api/examples/bpy.types.Operator.3.py index fb40616d15d..264b72f27c6 100644 --- a/doc/python_api/examples/bpy.types.Operator.3.py +++ b/doc/python_api/examples/bpy.types.Operator.3.py @@ -16,9 +16,8 @@ class DialogOperator(bpy.types.Operator): my_string: bpy.props.StringProperty(name="String Value") def execute(self, context): - message = ( - "Popup Values: %f, %d, '%s'" % - (self.my_float, self.my_bool, self.my_string) + message = "Popup Values: {:f}, {:d}, '{:s}'".format( + self.my_float, self.my_bool, self.my_string, ) self.report({'INFO'}, message) return {'FINISHED'} diff --git a/doc/python_api/examples/mathutils.Color.py b/doc/python_api/examples/mathutils.Color.py index 3b33003cac1..be6ab6b29ea 100644 --- a/doc/python_api/examples/mathutils.Color.py +++ b/doc/python_api/examples/mathutils.Color.py @@ -10,21 +10,21 @@ col.s *= 0.5 print("Color R:", col.r) print("Color G:", col[1]) print("Color B:", col[-1]) -print("Color HSV: %.2f, %.2f, %.2f", col[:]) +print("Color HSV: {:.2f}, {:.2f}, {:.2f}".format(*col)) # components of an existing color can be set col[:] = 0.0, 0.5, 1.0 # components of an existing color can use slice notation to get a tuple -print("Values: %f, %f, %f" % col[:]) +print("Values: {:f}, {:f}, {:f}".format(*col)) # colors can be added and subtracted col += mathutils.Color((0.25, 0.0, 0.0)) # Color can be multiplied, in this example color is scaled to 0-255 # can printed as integers -print("Color: %d, %d, %d" % (col * 255.0)[:]) +print("Color: {:d}, {:d}, {:d}".format(*(int(c) for c in (col * 255.0)))) # This example prints the color as hexadecimal -print("Hexadecimal: %.2x%.2x%.2x" % (int(col.r * 255), int(col.g * 255), int(col.b * 255))) +print("Hexadecimal: {:02x}{:02x}{:02x}".format(int(col.r * 255), int(col.g * 255), int(col.b * 255))) diff --git a/doc/python_api/examples/mathutils.Euler.py b/doc/python_api/examples/mathutils.Euler.py index f1fcd53c70f..2e36cf7f6b5 100644 --- a/doc/python_api/examples/mathutils.Euler.py +++ b/doc/python_api/examples/mathutils.Euler.py @@ -16,7 +16,7 @@ print("Euler Z", eul[-1]) eul[:] = 1.0, 2.0, 3.0 # components of an existing euler can use slice notation to get a tuple -print("Values: %f, %f, %f" % eul[:]) +print("Values: {:f}, {:f}, {:f}".format(*eul)) # the order can be set at any time too eul.order = 'ZYX' diff --git a/doc/python_api/examples/mathutils.Quaternion.py b/doc/python_api/examples/mathutils.Quaternion.py index 315a0024ee9..099f5798b8e 100644 --- a/doc/python_api/examples/mathutils.Quaternion.py +++ b/doc/python_api/examples/mathutils.Quaternion.py @@ -18,9 +18,8 @@ quat_out = quat_a @ quat_b # print the quat, euler degrees for mere mortals and (axis, angle) print("Final Rotation:") print(quat_out) -print("%.2f, %.2f, %.2f" % tuple(math.degrees(a) for a in quat_out.to_euler())) -print("(%.2f, %.2f, %.2f), %.2f" % (quat_out.axis[:] + - (math.degrees(quat_out.angle), ))) +print("{:.2f}, {:.2f}, {:.2f}".format(*(math.degrees(a) for a in quat_out.to_euler()))) +print("({:.2f}, {:.2f}, {:.2f}), {:.2f}".format(*quat_out.axis, math.degrees(quat_out.angle))) # multiple rotations can be interpolated using the exponential map quat_c = mathutils.Quaternion((1.0, 0.0, 0.0), math.radians(15.0)) diff --git a/scripts/templates_py/gizmo_custom_geometry.py b/scripts/templates_py/gizmo_custom_geometry.py index 699f856b2a7..f0fe9015d9e 100644 --- a/scripts/templates_py/gizmo_custom_geometry.py +++ b/scripts/templates_py/gizmo_custom_geometry.py @@ -108,7 +108,7 @@ class MyCustomShapeWidget(Gizmo): delta /= 10.0 value = self.init_value - delta self.target_set_value("offset", value) - context.area.header_text_set("My Gizmo: %.4f" % value) + context.area.header_text_set("My Gizmo: {:.4f}".format(value)) return {'RUNNING_MODAL'} diff --git a/scripts/templates_py/operator_file_export.py b/scripts/templates_py/operator_file_export.py index 39118a2a3b9..31b44fcb7f2 100644 --- a/scripts/templates_py/operator_file_export.py +++ b/scripts/templates_py/operator_file_export.py @@ -4,7 +4,7 @@ import bpy def write_some_data(context, filepath, use_some_setting): print("running write_some_data...") f = open(filepath, 'w', encoding='utf-8') - f.write("Hello World %s" % use_some_setting) + f.write("Hello World {:s}".format(use_some_setting)) f.close() return {'FINISHED'} diff --git a/scripts/templates_py/operator_modal_view3d.py b/scripts/templates_py/operator_modal_view3d.py index d0200bccc05..26d2b2a6e40 100644 --- a/scripts/templates_py/operator_modal_view3d.py +++ b/scripts/templates_py/operator_modal_view3d.py @@ -26,7 +26,7 @@ class ViewOperator(bpy.types.Operator): if event.type == 'MOUSEMOVE': self.offset = (self._initial_mouse - Vector((event.mouse_x, event.mouse_y, 0.0))) * 0.02 self.execute(context) - context.area.header_text_set("Offset %.4f %.4f %.4f" % tuple(self.offset)) + context.area.header_text_set("Offset {:.4f} {:.4f} {:.4f}".format(*self.offset)) elif event.type == 'LEFTMOUSE': context.area.header_text_set(None) diff --git a/scripts/templates_py/ui_previews_dynamic_enum.py b/scripts/templates_py/ui_previews_dynamic_enum.py index 2648a080592..48693a713f4 100644 --- a/scripts/templates_py/ui_previews_dynamic_enum.py +++ b/scripts/templates_py/ui_previews_dynamic_enum.py @@ -39,7 +39,7 @@ def enum_previews_from_directory_items(self, context): if directory == pcoll.my_previews_dir: return pcoll.my_previews - print("Scanning directory: %s" % directory) + print("Scanning directory:", directory) if directory and os.path.exists(directory): # Scan the directory for `*.png` files