8000 Some more f-strings. · matplotlib/matplotlib@be7c002 · GitHub
[go: up one dir, main page]

Skip to content

Commit be7c002

Browse files
committed
Some more f-strings.
`"..." % locals()` is perhaps *the* construct that f-strings most obviously replace, so let's do that replacement.
1 parent 1c06de3 commit be7c002

File tree

2 files changed

+59
-67
lines changed

2 files changed

+59
-67
lines changed

lib/matplotlib/backends/backend_ps.py

Lines changed: 51 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -299,31 +299,28 @@ def create_hatch(self, hatch):
299299
name = 'H%d' % len(self._hatches)
300300
linewidth = rcParams['hatch.linewidth']
301301
pageheight = self.height * 72
302-
self._pswriter.write("""\
302+
self._pswriter.write(f"""\
303303
<< /PatternType 1
304304
/PaintType 2
305305
/TilingType 2
306-
/BBox[0 0 %(sidelen)d %(sidelen)d]
307-
/XStep %(sidelen)d
308-
/YStep %(sidelen)d
306+
/BBox[0 0 {sidelen:d} {sidelen:d}]
307+
/XStep {sidelen:d}
308+
/YStep {sidelen:d}
309309
310-
/PaintProc {
310+
/PaintProc {{
311311
pop
312-
%(linewidth)f setlinewidth
313-
""" % locals())
314-
self._pswriter.write(
315-
self._convert_path(Path.hatch(hatch), Affine2D().scale(sidelen),
316-
simplify=False))
317-
self._pswriter.write("""\
312+
{linewidth:f} setlinewidth
313+
{self._convert_path(
314+
Path.hatch(hatch), Affine2D().scale(sidelen), simplify=False)}
318315
fill
319316
stroke
320-
} bind
317+
}} bind
321318
>>
322319
matrix
323-
0.0 %(pageheight)f translate
320+
0.0 {pageheight:f} translate
324321
makepattern
325-
/%(name)s exch def
326-
""" % locals())
322+
/{name} exch def
323+
""")
327324
self._hatches[hatch] = name
328325
return name
329326

@@ -369,20 +366,20 @@ def draw_image(self, gc, x, y, im, transform=None):
369366
clip.append('%s' % id)
370367
clip = '\n'.join(clip)
371368

372-
ps = """gsave
373-
%(clip)s
374-
%(x)s %(y)s translate
375-
[%(matrix)s] concat
376-
%(xscale)s %(yscale)s scale
377-
/DataString %(w)s string def
378-
%(w)s %(h)s 8 [ %(w)s 0 0 -%(h)s 0 %(h)s ]
379-
{
369+
self._pswriter.write(f"""\
370+
gsave
371+
{clip}
372+
{x:f} {y:f} translate
373+
[{matrix}] concat
374+
{xscale:f} {yscale:f} scale
375+
/DataString {w:d} string def
376+
{w:d} {h:d} 8 [ {w:d} 0 0 -{h:d} 0 {h:d} ]
377+
{{
380378
currentfile DataString readhexstring pop
381-
} bind %(imagecmd)s
382-
%(hexlines)s
379+
}} bind {imagecmd}
380+
{hexlines}
383381
grestore
384-
""" % locals()
385-
self._pswriter.write(ps)
382+
""")
386383

387384
def _convert_path(self, path, transform, clip=False, simplify=None):
388385
if clip:
@@ -548,15 +545,13 @@ def draw_tex(self, gc, x, y, s, prop, angle, ismath='TeX!', mtext=None):
548545
r'\psfrag{%s}[bl][bl][1][%f]{\fontsize{%f}{%f}%s}' % (
549546
thetext, angle, fontsize, fontsize*1.25, tex))
550547

551-
ps = """\
548+
self._pswriter.write(f"""\
552549
gsave
553-
%(pos)s moveto
554-
(%(thetext)s)
550+
{pos} moveto
551+
({thetext})
555552
show
556553
grestore
557-
""" % locals()
558-
559-
self._pswriter.write(ps)
554+
""")
560555
self.textcnt += 1
561556

562557
def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
@@ -607,17 +602,16 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
607602
thisx += width * scale
608603

609604
thetext = "\n".join(lines)
610-
ps = """\
605+
self._pswriter.write(f"""\
611606
gsave
612-
/%(fontname)s findfont
613-
%(fontsize)s scalefont
607+
/{fontname} findfont
608+
{fontsize} scalefont
614609
setfont
615-
%(x)f %(y)f translate
616-
%(angle)f rotate
617-
%(thetext)s
610+
{x:f} {y:f} translate
611+
{angle:f} rotate
612+
{thetext}
618613
grestore
619-
""" % locals()
620-
self._pswriter.write(ps)
614+
""")
621615

622616
else:
623617
font = self._get_font_ttf(prop)
@@ -655,13 +649,13 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
655649
thisx += glyph.linearHoriAdvance / 65536
656650

657651
thetext = '\n'.join(lines)
658-
ps = """gsave
659-
%(x)f %(y)f translate
660-
%(angle)f rotate
661-
%(thetext)s
652+
self._pswriter.write(f"""\
653+
gsave
654+
{x:f} {y:f} translate
655+
{angle:f} rotate
656+
{thetext}
662657
grestore
663-
""" % locals()
664-
self._pswriter.write(ps)
658+
""")
665659

666660
def new_gc(self):
667661
# docstring inherited
@@ -677,13 +671,13 @@ def draw_mathtext(self, gc, x, y, s, prop, angle):
677671
self.merge_used_characters(used_characters)
678672
self.set_color(*gc.get_rgb())
679673
thetext = pswriter.getvalue()
680-
ps = """gsave
681-
%(x)f %(y)f translate
682-
%(angle)f rotate
683-
%(thetext)s
674+
self._pswriter.write(f"""\
675+
gsave
676+
{x:f} {y:f} translate
677+
{angle:f} rotate
678+
{thetext}
684679
grestore
685-
""" % locals()
686-
self._pswriter.write(ps)
680+
""")
687681

688682
def draw_gouraud_triangle(self, gc, points, colors, trans):
689683
self.draw_gouraud_triangles(gc, points.reshape((1, 3, 2)),
@@ -720,20 +714,20 @@ def draw_gouraud_triangles(self, gc, points, colors, trans):
720714

721715
stream = quote_ps_string(streamarr.tostring())
722716

723-
self._pswriter.write("""
717+
self._pswriter.write(f"""\
724718
gsave
725719
<< /ShadingType 4
726720
/ColorSpace [/DeviceRGB]
727721
/BitsPerCoordinate 32
728722
/BitsPerComponent 8
729723
/BitsPerFlag 8
730724
/AntiAlias true
731-
/Decode [ %(xmin)f %(xmax)f %(ymin)f %(ymax)f 0 1 0 1 0 1 ]
732-
/DataSource (%(stream)s)
725+
/Decode [ {xmin:f} {xmax:f} {ymin:f} {ymax:f} 0 1 0 1 0 1 ]
726+
/DataSource ({stream})
733727
>>
734728
shfill
735729
grestore
736-
""" % locals())
730+
""")
737731

738732
def _draw_ps(self, ps, gc, rgbFace, fill=True, stroke=True, command=None):
739733
"""

lib/matplotlib/mathtext.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -237,17 +237,15 @@ def render_glyph(self, ox, oy, info):
237237
symbol_name = info.symbol_name
238238

239239
if (postscript_name, fontsize) != self.lastfont:
240-
ps = """/%(postscript_name)s findfont
241-
%(fontsize)s scalefont
242-
setfont
243-
""" % locals()
244240
self.lastfont = postscript_name, fontsize
245-
self.pswriter.write(ps)
246-
247-
ps = """%(ox)f %(oy)f moveto
248-
/%(symbol_name)s glyphshow\n
249-
""" % locals()
250-
self.pswriter.write(ps)
241+
self.pswriter.write(
242+
f"/{postscript_name} findfont\n"
243+
f"{fontsize} scalefont\n"
244+
f"setfont\n")
245+
246+
self.pswriter.write(
247+
f"{ox:f} {oy:f} moveto\n"
248+
f"/{symbol_name} glyphshow\n")
251249

252250
def render_rect_filled(self, x1, y1, x2, y2):
253251
ps = "%f %f %f %f rectfill\n" % (

0 commit comments

Comments
 (0)
0