8000 Join strings instead of adding them. · matplotlib/matplotlib@1aaf38b · GitHub
[go: up one dir, main page]

Skip to content

Commit 1aaf38b

Browse files
committed
Join strings instead of adding them.
See e.g. https://docs.python.org/3/library/functions.html#sum that documents "".join as the preferred method (briefly, because summing (or repeated addition) has quadratic complexity for strings).
1 parent f77243b commit 1aaf38b

File tree

4 files changed

+12
-18
lines changed

4 files changed

+12
-18
lines changed

lib/matplotlib/animation.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -842,11 +842,9 @@ def _included_frames(frame_list, frame_format):
842842
def _embedded_frames(frame_list, frame_format):
843843
"""frame_list should be a list of base64-encoded png files"""
844844
template = ' frames[{0}] = "data:image/{1};base64,{2}"\n'
845-
embedded = "\n"
846-
for i, frame_data in enumerate(frame_list):
847-
embedded += template.format(i, frame_format,
848-
frame_data.replace('\n', '\\\n'))
849-
return embedded
845+
return "\n" + "".join(
846+
template.format(i, frame_format, frame_data.replace('\n', '\\\n'))
847+
for i, frame_data in enumerate(frame_list))
850848

851849

852850
@writers.register('html')

lib/matplotlib/backends/backend_pgf.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -582,11 +582,10 @@ def _print_pgf_path_styles(self, gc, rgbFace):
582582
if dash_list is None:
583583
writeln(self.fh, r"\pgfsetdash{}{0pt}")
584584
else:
585-
dash_str = r"\pgfsetdash{"
586-
for dash in dash_list:
587-
dash_str += r"{%fpt}" % dash
588-
dash_str += r"}{%fpt}" % dash_offset
589-
writeln(self.fh, dash_str)
585+
writeln(self.fh,
586+
r"\pgfsetdash{%s}{%fpt}"
587+
% ("".join(r"{%fpt}" % dash for dash in dash_list),
588+
dash_offset))
590589

591590
def _print_pgf_path(self, gc, path, transform, rgbFace=None):
592591
f = 1. / self.dpi

lib/matplotlib/backends/backend_wx.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1446,12 +1446,9 @@ def getActiveAxes(self):
14461446
return active
14471447

14481448
def updateButtonText(self, lst):
1449-
"""Update the list of selected axes in the menu button"""
1450-
axis_txt = ''
1451-
for e in lst:
1452-
axis_txt += '%d,' % (e + 1)
1453-
# remove trailing ',' and add to button string
1454-
self.SetLabel("Axes: %s" % axis_txt[:-1])
1449+
"""Update the list of selected axes in the menu button."""
1450+
self.SetLabel(
1451+
'Axes: ' + ','.join('%d' % (e + 1) for e in lst))
14551452

14561453

14571454
cursord = {

lib/matplotlib/legend.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1311,12 +1311,12 @@ def _get_legend_handles(axs, legend_handler_map=None):
13111311
handles_original = []
13121312
for ax in axs:
13131313
handles_original += (ax.lines + ax.patches +
1314-
ax.collections + ax.containers)
1314+
ax.collections + ax.containers)
13151315
# support parasite axes:
13161316
if hasattr(ax, 'parasites'):
13171317
for axx in ax.parasites:
13181318
handles_original += (axx.lines + axx.patches +
1319-
axx.collections + axx.containers)
1319+
axx.collections + axx.containers)
13201320

13211321
handler_map = Legend.get_default_handler_map()
13221322

0 commit comments

Comments
 (0)
0