Skip to content

Commit

Permalink
refactor: convert modulo strings to fstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcnamara committed Nov 16, 2024
1 parent b57d092 commit bb89911
Show file tree
Hide file tree
Showing 20 changed files with 287 additions and 307 deletions.
28 changes: 14 additions & 14 deletions dev/performance/bench_excel_writers.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

def print_elapsed_time(module_name, elapsed):
""" Print module run times in a consistent format. """
print(" %-28s: %6.2f" % (module_name, elapsed))
print(f" {module_name:28s}: {elapsed:6.2f}")


def time_xlsxwriter():
Expand All @@ -42,7 +42,7 @@ def time_xlsxwriter():

for row in range(row_max // 2):
for col in range(col_max):
worksheet.write_string(row * 2, col, "Row: %d Col: %d" % (row, col))
worksheet.write_string(row * 2, col, f"Row: {row} Col: {col}")
for col in range(col_max):
worksheet.write_number(row * 2 + 1, col, row + col)

Expand All @@ -62,7 +62,7 @@ def time_xlsxwriter_optimised():

for row in range(row_max // 2):
for col in range(col_max):
worksheet.write_string(row * 2, col, "Row: %d Col: %d" % (row, col))
worksheet.write_string(row * 2, col, f"Row: {row} Col: {col}")
for col in range(col_max):
worksheet.write_number(row * 2 + 1, col, row + col)

Expand All @@ -81,7 +81,7 @@ def time_openpyxl():

for row in range(row_max // 2):
for col in range(col_max):
worksheet.cell(row * 2 + 1, col + 1, "Row: %d Col: %d" % (row, col))
worksheet.cell(row * 2 + 1, col + 1, f"Row: {row} Col: {col}")
for col in range(col_max):
worksheet.cell(row * 2 + 2, col + 1, row + col)

Expand All @@ -99,7 +99,7 @@ def time_openpyxl_optimised():
worksheet = workbook.create_sheet()

for row in range(row_max // 2):
string_data = ["Row: %d Col: %d" % (row, col) for col in range(col_max)]
string_data = [f"Row: {row} Col: {col}" for col in range(col_max)]
worksheet.append(string_data)

num_data = [row + col for col in range(col_max)]
Expand All @@ -120,7 +120,7 @@ def time_pyexcelerate():

for row in range(row_max // 2):
for col in range(col_max):
worksheet.set_cell_value(row * 2 + 1, col + 1, "Row: %d Col: %d" % (row, col))
worksheet.set_cell_value(row * 2 + 1, col + 1, f"Row: {row} Col: {col}")
for col in range(col_max):
worksheet.set_cell_value(row * 2 + 2, col + 1, row + col)

Expand All @@ -139,7 +139,7 @@ def time_xlwt():

for row in range(row_max // 2):
for col in range(col_max):
worksheet.write(row * 2, col, "Row: %d Col: %d" % (row, col))
worksheet.write(row * 2, col, f"Row: {row} Col: {col}")
for col in range(col_max):
worksheet.write(row * 2 + 1, col, row + col)

Expand All @@ -151,16 +151,16 @@ def time_xlwt():

print("")
print("Versions:")
print(" %-12s: %s" % ('python', str(sys.version).split()[0]))
print(" %-12s: %s" % ('openpyxl', openpyxl.__version__))
print(" %-12s: %s" % ('pyexcelerate', pyexcelerate.__version__))
print(" %-12s: %s" % ('xlsxwriter', xlsxwriter.__version__))
print(" %-12s: %s" % ('xlwt', xlwt.__VERSION__))
print(f" python: {str(sys.version).split()[0]}")
print(f" openpyxl: {openpyxl.__version__}")
print(f" pyexcelerate: {pyexcelerate.__version__}")
print(f" xlsxwriter: {xlsxwriter.__version__}")
print(f" xlwt: {xlwt.__VERSION__}")
print("")

print("Dimensions:")
print(" Rows = %d" % row_max)
print(" Cols = %d" % col_max)
print(f" Rows = {row_max}")
print(f" Cols = {col_max}")
print("")

print("Times:")
Expand Down
18 changes: 9 additions & 9 deletions dev/performance/perf3.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def print_elapsed_time(module_name, elapsed, optimised=False):
""" Print module run times in a consistent format. """
if optimised:
module_name += " (optimised)"
print(" %-22s: %6.2f" % (module_name, elapsed))
print(f" {module_name:22s}: {elapsed:6.2f}")


def time_xlsxwriter(optimised=False):
Expand All @@ -55,7 +55,7 @@ def time_xlsxwriter(optimised=False):
worksheet = workbook.add_worksheet()

for row in range(0, row_max, 2):
string_data = ["Row: %d Col: %d" % (row, col) for col in range(col_max)]
string_data = [f"Row: {row} Col: {col}" for col in range(col_max)]
worksheet.write_row(row, 0, string_data)

num_data = [row + col for col in range(col_max)]
Expand All @@ -81,7 +81,7 @@ def time_openpyxl(optimised=False):

for row in range(row_max // 2):

string_data = ("Row: %d Col: %d" % (row, col) for col in range(col_max))
string_data = (f"Row: {row} Col: {col}" for col in range(col_max))
worksheet.append(string_data)

num_data = (row + col for col in range(col_max))
Expand All @@ -96,15 +96,15 @@ def time_openpyxl(optimised=False):

print("")
print("Versions:")
print("%s: %s" % ('python', str(sys.version).split()[0]))
print("%s: %s" % ('openpyxl', openpyxl.__version__))
print("%s: %s" % ('xlsxwriter', xlsxwriter.__version__))
print(f"python: {str(sys.version).split()[0]}")
print(f"openpyxl: {openpyxl.__version__}")
print(f"xlsxwriter: {xlsxwriter.__version__}")
print("")

print("Dimensions:")
print(" Rows = %d" % row_max)
print(" Cols = %d" % col_max)
print(" Sheets = %d" % sheets)
print(f" Rows = {row_max}")
print(f" Cols = {col_max}")
print(f" Sheets = {sheets}")
print("")

print("Times:")
Expand Down
4 changes: 2 additions & 2 deletions dev/performance/perf_pyx.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@

for row in range(0, row_max):
for col in range(0, col_max):
worksheet.write_string(row * 2, col, "Row: %d Col: %d" % (row, col))
worksheet.write_string(row * 2, col, f"Row: {row} Col: {col}")
for col in range(0, col_max + 1):
worksheet.write_number(row * 2 + 1, col, row + col)

Expand All @@ -60,4 +60,4 @@

# Print a simple CSV output for reporting.

print("%6d, %3d, %6.2f, %d" % (row_max * 2, col_max, elapsed, total_size))
print(f"{row_max * 2:6d}, {col_max:3d}, {elapsed:6.2f}, {total_size}")
2 changes: 1 addition & 1 deletion examples/chart_styles.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
for col_num in range(0, 64, 8):
chart = workbook.add_chart({"type": chart_type})
chart.add_series({"values": "=Data!$A$1:$A$6"})
chart.set_title({"name": "Style %d" % style_number})
chart.set_title({"name": f"Style {style_number}"})
chart.set_legend({"none": True})
chart.set_style(style_number)

Expand Down
6 changes: 3 additions & 3 deletions examples/check_close.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
workbook.close()
except xlsxwriter.exceptions.FileCreateError as e:
decision = input(
"Exception caught in workbook.close(): %s\n"
"Please close the file if it is open in Excel.\n"
"Try to write file again? [Y/n]: " % e
f"Exception caught in workbook.close(): {e}\n"
f"Please close the file if it is open in Excel.\n"
f"Try to write file again? [Y/n]: "
)
if decision != "n":
continue
Expand Down
2 changes: 1 addition & 1 deletion examples/django_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,6 @@ def get(self, request):
output,
content_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
)
response["Content-Disposition"] = "attachment; filename=%s" % filename
response["Content-Disposition"] = f"attachment; filename={filename}"

return response
14 changes: 7 additions & 7 deletions examples/vba_extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,30 +49,30 @@ def extract_file(xlsm_zip, filename):

# Read the xl/vbaProject.bin file.
extract_file(xlsm_zip, vba_filename)
print("Extracted: %s" % vba_filename)
print(f"Extracted: {vba_filename}")

if "xl/" + vba_signature_filename in xlsm_zip.namelist():
extract_file(xlsm_zip, vba_signature_filename)
print("Extracted: %s" % vba_signature_filename)
print(f"Extracted: {vba_signature_filename}")


except IOError as e:
print("File error: %s" % str(e))
print(f"File error: {str(e)}")
exit()

except KeyError as e:
# Usually when there isn't a xl/vbaProject.bin member in the file.
print("File error: %s" % str(e))
print("File may not be an Excel xlsm macro file: '%s'" % xlsm_file)
print(f"File error: {str(e)}")
print(f"File may not be an Excel xlsm macro file: '{xlsm_file}'")
exit()

except BadZipFile as e:
# Usually if the file is an xls file and not an xlsm file.
print("File error: %s: '%s'" % (str(e), xlsm_file))
print(f"File error: {str(e)}: '{xlsm_file}'")
print("File may not be an Excel xlsm macro file.")
exit()

except Exception as e:
# Catch any other exceptions.
print("File error: %s" % str(e))
print(f"File error: {str(e)}")
exit()
2 changes: 1 addition & 1 deletion xlsxwriter/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def _write_vt_vector(self, base_type, vector_data):

def _write_vt_data(self, vt_data):
# Write the <vt:*> elements such as <vt:lpstr> and <vt:if>.
self._xml_data_element("vt:%s" % vt_data[0], vt_data[1])
self._xml_data_element(f"vt:{vt_data[0]}", vt_data[1])

def _write_company(self):
company = self.properties.get("company", "")
Expand Down
36 changes: 17 additions & 19 deletions xlsxwriter/chart.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ def show_blanks_as(self, option):
}

if option not in valid_options:
warn("Unknown show_blanks_as() option '%s'" % option)
warn(f"Unknown show_blanks_as() option '{option}'")
return

self.show_blanks = option
Expand Down Expand Up @@ -850,8 +850,8 @@ def _list_to_formula(self, data):
# Check for unquoted sheetnames.
if data and " " in data and "'" not in data and self.warn_sheetname:
warn(
"Sheetname in '%s' contains spaces but isn't quoted. "
"This may cause errors in Excel." % data
f"Sheetname in '{data}' contains spaces but isn't quoted. "
f"This may cause an error in Excel."
)
return data

Expand Down Expand Up @@ -972,7 +972,7 @@ def _get_marker_properties(self, marker):
if marker_type in types:
marker["type"] = types[marker_type]
else:
warn("Unknown marker type '%s" % marker_type)
warn(f"Unknown marker type '{marker_type}")
return

# Set the line properties for the marker.
Expand Down Expand Up @@ -1031,7 +1031,7 @@ def _get_trendline_properties(self, trendline):
if trend_type in types:
trendline["type"] = types[trend_type]
else:
warn("Unknown trendline type '%s'" % trend_type)
warn(f"Unknown trendline type '{trend_type}'")
return

# Set the line properties for the trendline.
Expand Down Expand Up @@ -1137,7 +1137,7 @@ def _get_error_bars_props(self, options):
if error_type in types:
error_bars["type"] = types[error_type]
else:
warn("Unknown error bars type '%s" % error_type)
warn(f"Unknown error bars type '{error_type}")
return

# Set the value for error types that require it.
Expand Down Expand Up @@ -1199,7 +1199,7 @@ def _get_labels_properties(self, labels):
else:
labels["position"] = self.label_positions[position]
else:
warn("Unsupported label position '%s' for this chart type" % position)
warn(f"Unsupported label position '{position}' for this chart type")
return

# Map the user defined label separator to the Excel separator.
Expand Down Expand Up @@ -1409,34 +1409,32 @@ def _get_layout_properties(self, args, is_text):
# Check for valid properties.
for key in args.keys():
if key not in properties:
warn("Property '%s' allowed not in layout options" % key)
warn(f"Property '{key}' not supported in layout options")
return

# Set the layout properties.
for prop in properties:
if prop not in args.keys():
warn("Property '%s' must be specified in layout options" % prop)
warn(f"Property '{prop}' must be specified in layout options")
return

value = args[prop]

try:
float(value)
except ValueError:
warn(
"Property '%s' value '%s' must be numeric in layout" % (prop, value)
)
warn(f"Property '{prop}' value '{value}' must be numeric in layout")
return

if value < 0 or value > 1:
warn(
"Property '%s' value '%s' must be in range "
"0 < x <= 1 in layout options" % (prop, value)
f"Property '{prop}' value '{value}' must be in range "
f"0 < x <= 1 in layout options"
)
return

# Convert to the format used by Excel for easier testing
layout[prop] = "%.17g" % value
layout[prop] = f"{value:.17g}"

return layout

Expand Down Expand Up @@ -1523,7 +1521,7 @@ def _get_display_units(self, display_units):
if display_units in types:
display_units = types[display_units]
else:
warn("Unknown display_units type '%s'" % display_units)
warn(f"Unknown display_units type '{display_units}'")
return

return display_units
Expand All @@ -1543,7 +1541,7 @@ def _get_tick_type(self, tick_type):
if tick_type in types:
tick_type = types[tick_type]
else:
warn("Unknown tick_type '%s'" % tick_type)
warn(f"Unknown tick_type '{tick_type}'")
return

return tick_type
Expand Down Expand Up @@ -1573,8 +1571,8 @@ def _add_axis_ids(self, args):
chart_id = 5001 + int(self.id)
axis_count = 1 + len(self.axis2_ids) + len(self.axis_ids)

id1 = "%04d%04d" % (chart_id, axis_count)
id2 = "%04d%04d" % (chart_id, axis_count + 1)
id1 = f"{chart_id:04d}{axis_count:04d}"
id2 = f"{chart_id:04d}{axis_count + 1:04d}"

if args["primary_axes"]:
self.axis_ids.append(id1)
Expand Down
2 changes: 1 addition & 1 deletion xlsxwriter/chart_doughnut.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def set_hole_size(self, size):

# Ensure the size is in Excel's range.
if size < 10 or size > 90:
warn("Chart hole size %d outside Excel range: 10 <= size <= 90" % size)
warn("Chart hole size '{size}' outside Excel range: 10 <= size <= 90")
return

self.hole_size = int(size)
Expand Down
2 changes: 1 addition & 1 deletion xlsxwriter/chart_pie.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def set_rotation(self, rotation):
# Ensure the rotation is in Excel's range.
if rotation < 0 or rotation > 360:
warn(
"Chart rotation %d outside Excel range: 0 <= rotation <= 360" % rotation
f"Chart rotation '{rotation}' outside Excel range: 0 <= rotation <= 360"
)
return

Expand Down
4 changes: 2 additions & 2 deletions xlsxwriter/packager.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,8 @@ def _write_chart_files(self):
# Check that the chart has at least one data series.
if not chart.series:
raise EmptyChartSeries(
"Chart%d must contain at least one "
"data series. See chart.add_series()." % index
f"Chart{index} must contain at least one "
f"data series. See chart.add_series()."
)

chart._set_xml_writer(
Expand Down
Loading

0 comments on commit bb89911

Please sign in to comment.