Skip to content

Commit 3fb99d8

Browse files
authored
Merge pull request #394 from eumiro/fstring
Use f-strings
2 parents 595d847 + 8091c24 commit 3fb99d8

File tree

9 files changed

+22
-24
lines changed

9 files changed

+22
-24
lines changed

cairosvg/bounding_box.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def bounding_box_path(surface, node):
8080

8181
# Normalize path data for correct parsing
8282
for letter in PATH_LETTERS:
83-
path_data = path_data.replace(letter, ' {} '.format(letter))
83+
path_data = path_data.replace(letter, f' {letter} ')
8484
path_data = normalize(path_data)
8585

8686
bounding_box = EMPTY_BOUNDING_BOX

cairosvg/defs.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def update_def_href(surface, def_name, def_dict):
3737
update_def_href(surface, href, def_dict)
3838
href_node = def_dict[href]
3939
def_dict[def_name] = Tree(
40-
url='#{}'.format(def_name), url_fetcher=def_node.url_fetcher,
40+
url=f'#{def_name}', url_fetcher=def_node.url_fetcher,
4141
parent=href_node, parent_children=(not def_node.children),
4242
tree_cache=surface.tree_cache, unsafe=def_node.unsafe)
4343
# Inherit attributes generally not inherited
@@ -64,7 +64,7 @@ def parse_def(surface, node):
6464
'marker', 'gradient', 'pattern', 'path', 'mask', 'filter',
6565
'image'):
6666
if def_type in node.tag.lower() and 'id' in node:
67-
getattr(surface, '{}s'.format(def_type))[node['id']] = node
67+
getattr(surface, f'{def_type}s')[node['id']] = node
6868

6969

7070
def gradient_or_pattern(surface, node, name, opacity):
@@ -139,9 +139,7 @@ def paint_mask(surface, node, name, opacity):
139139
if mask_node.get('maskUnits') == 'userSpaceOnUse':
140140
x = mask_node['x']
141141
y = mask_node['y']
142-
mask_node['viewBox'] = '{} {} {} {}'.format(
143-
mask_node['x'], mask_node['y'],
144-
mask_node['width'], mask_node['height'])
142+
mask_node['viewBox'] = '{x} {y} {width} {height}'.format(**mask_node)
145143

146144
from .surface import SVGSurface # circular import
147145
mask_surface = SVGSurface(mask_node, None, surface.dpi, surface)
@@ -248,8 +246,7 @@ def draw_pattern(surface, node, name, opacity):
248246
pattern_node['width'] = pattern_width
249247
pattern_node['height'] = pattern_height
250248
if pattern_node.get('patternContentUnits') == 'objectBoundingBox':
251-
pattern_node['transform'] = 'scale({}, {})'.format(
252-
width, height)
249+
pattern_node['transform'] = f'scale({width}, {height})'
253250

254251
# Fail if pattern has an invalid size
255252
if pattern_width == 0.0 or pattern_height == 0.0:

cairosvg/helpers.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,9 @@ def preserve_ratio(surface, node, width=None, height=None):
111111
viewbox_width, viewbox_height = node.image_width, node.image_height
112112
else:
113113
raise TypeError(
114-
('Root node is {}. Should be one of '
115-
'marker, svg, image, or g.').format(node.tag))
114+
f'Root node is {node.tag}. Should be one of '
115+
'marker, svg, image, or g.'
116+
)
116117

117118
translate_x = 0
118119
translate_y = 0

cairosvg/parser.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ def __init__(self, element, style, url_fetcher, parent=None,
168168
self.tag = (
169169
element.local_name
170170
if element.namespace_url in ('', 'http://www.w3.org/2000/svg') else
171-
'{%s}%s' % (element.namespace_url, element.local_name))
171+
f'{{{element.namespace_url}}}{element.local_name}')
172172
self.text = node.text
173173
self.url_fetcher = url_fetcher
174174
self.unsafe = unsafe
@@ -406,7 +406,7 @@ def __init__(self, **kwargs):
406406
break
407407
else:
408408
raise TypeError(
409-
'No tag with id="{}" found.'.format(element_id))
409+
f'No tag with id="{element_id}" found.')
410410
super().__init__(
411411
root, style, self.url_fetcher, parent, parent_children, self.url,
412412
unsafe)

cairosvg/path.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def draw_markers(surface, node):
2020
markers = {}
2121
common_marker = parse_url(node.get('marker', '')).fragment
2222
for position in ('start', 'mid', 'end'):
23-
attribute = 'marker-{}'.format(position)
23+
attribute = f'marker-{position}'
2424
if attribute in node:
2525
markers[position] = parse_url(node[attribute]).fragment
2626
else:
@@ -125,7 +125,7 @@ def path(surface, node):
125125
node.vertices = []
126126

127127
for letter in PATH_LETTERS:
128-
string = string.replace(letter, ' {} '.format(letter))
128+
string = string.replace(letter, f' {letter} ')
129129

130130
last_letter = None
131131
string = normalize(string)
@@ -190,10 +190,10 @@ def path(surface, node):
190190
# As we replace the current operation by l, we must be sure
191191
# that the next letter is set to the real current letter (a
192192
# or A) in case it’s omitted
193-
next_letter = '{} '.format(letter)
193+
next_letter = f'{letter} '
194194
else:
195195
next_letter = ''
196-
string = 'l {} {} {}{}'.format(x3, y3, next_letter, string)
196+
string = f'l {x3} {y3} {next_letter}{string}'
197197
continue
198198

199199
radii_ratio = ry / rx

cairosvg/test_api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def test_api():
7474
# Read from a filename
7575
assert svg2png(url=temp_0) == expected_content
7676
assert svg2png(
77-
url='file://{}'.format(temp_0)) == expected_content
77+
url=f'file://{temp_0}') == expected_content
7878

7979
with open(temp_0, 'rb') as file_object:
8080
# Read from a real file object

cairosvg/text.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,14 @@ def text(surface, node, draw_as_text=False):
5454
font_family = (
5555
(node.get('font-family') or 'sans-serif').split(',')[0].strip('"\' '))
5656
font_style = getattr(
57-
cairo, ('font_slant_{}'.format(node.get('font-style')).upper()),
57+
cairo, f'font_slant_{node.get("font-style")}'.upper(),
5858
cairo.FONT_SLANT_NORMAL)
5959
node_font_weight = node.get('font-weight')
6060
if (node_font_weight and node_font_weight.isdigit()
6161
and int(node_font_weight) >= 550):
6262
node_font_weight = 'bold'
6363
font_weight = getattr(
64-
cairo, ('font_weight_{}'.format(node_font_weight).upper()),
64+
cairo, (f'font_weight_{node_font_weight}'.upper()),
6565
cairo.FONT_WEIGHT_NORMAL)
6666
surface.context.select_font_face(font_family, font_style, font_weight)
6767
surface.context.set_font_size(surface.font_size)

cairosvg/url.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
from . import VERSION
1313

14-
HTTP_HEADERS = {'User-Agent': 'CairoSVG {}'.format(VERSION)}
14+
HTTP_HEADERS = {'User-Agent': f'CairoSVG {VERSION}'}
1515

1616
URL = re.compile(r'url\((.+)\)')
1717

@@ -132,7 +132,7 @@ def parse_url(url, base=None):
132132
else:
133133
url = ''
134134
if parsed_url.fragment:
135-
url = '{}#{}'.format(url, parsed_url.fragment)
135+
url = f'{url}#{parsed_url.fragment}'
136136
elif parsed_url.scheme in ('', parsed_base.scheme):
137137
# `urljoin` automatically uses the "folder" part of `base`
138138
url = urljoin(base, url)
@@ -148,7 +148,7 @@ def read_url(url, url_fetcher, resource_type):
148148
if url.scheme:
149149
url = url.geturl()
150150
else:
151-
url = 'file://{}'.format(os.path.abspath(url.geturl()))
151+
url = f'file://{os.path.abspath(url.geturl())}'
152152
url = normalize_url(url)
153153

154154
return url_fetcher(url, resource_type)

test_non_regression/test_non_regression.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,5 @@ def test_image(svg_filename):
4040
test_surface.finish()
4141

4242
raise AssertionError(
43-
'Images are different: {} {}'.format(
44-
ref_png.name, test_png.name))
43+
f'Images are different: {ref_png.name} {test_png.name}'
44+
)

0 commit comments

Comments
 (0)