From 8091c24072b65c7c3826a79109469678641a9862 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= <6774676+eumiro@users.noreply.github.com> Date: Wed, 2 Aug 2023 20:21:43 +0200 Subject: [PATCH] Use f-strings --- cairosvg/bounding_box.py | 2 +- cairosvg/defs.py | 11 ++++------- cairosvg/helpers.py | 5 +++-- cairosvg/parser.py | 4 ++-- cairosvg/path.py | 8 ++++---- cairosvg/test_api.py | 2 +- cairosvg/text.py | 4 ++-- cairosvg/url.py | 6 +++--- test_non_regression/test_non_regression.py | 4 ++-- 9 files changed, 22 insertions(+), 24 deletions(-) diff --git a/cairosvg/bounding_box.py b/cairosvg/bounding_box.py index 8bde411c..e31602c3 100644 --- a/cairosvg/bounding_box.py +++ b/cairosvg/bounding_box.py @@ -80,7 +80,7 @@ def bounding_box_path(surface, node): # Normalize path data for correct parsing for letter in PATH_LETTERS: - path_data = path_data.replace(letter, ' {} '.format(letter)) + path_data = path_data.replace(letter, f' {letter} ') path_data = normalize(path_data) bounding_box = EMPTY_BOUNDING_BOX diff --git a/cairosvg/defs.py b/cairosvg/defs.py index 98533270..717d7176 100644 --- a/cairosvg/defs.py +++ b/cairosvg/defs.py @@ -37,7 +37,7 @@ def update_def_href(surface, def_name, def_dict): update_def_href(surface, href, def_dict) href_node = def_dict[href] def_dict[def_name] = Tree( - url='#{}'.format(def_name), url_fetcher=def_node.url_fetcher, + url=f'#{def_name}', url_fetcher=def_node.url_fetcher, parent=href_node, parent_children=(not def_node.children), tree_cache=surface.tree_cache, unsafe=def_node.unsafe) # Inherit attributes generally not inherited @@ -64,7 +64,7 @@ def parse_def(surface, node): 'marker', 'gradient', 'pattern', 'path', 'mask', 'filter', 'image'): if def_type in node.tag.lower() and 'id' in node: - getattr(surface, '{}s'.format(def_type))[node['id']] = node + getattr(surface, f'{def_type}s')[node['id']] = node def gradient_or_pattern(surface, node, name, opacity): @@ -139,9 +139,7 @@ def paint_mask(surface, node, name, opacity): if mask_node.get('maskUnits') == 'userSpaceOnUse': x = mask_node['x'] y = mask_node['y'] - mask_node['viewBox'] = '{} {} {} {}'.format( - mask_node['x'], mask_node['y'], - mask_node['width'], mask_node['height']) + mask_node['viewBox'] = '{x} {y} {width} {height}'.format(**mask_node) from .surface import SVGSurface # circular import mask_surface = SVGSurface(mask_node, None, surface.dpi, surface) @@ -248,8 +246,7 @@ def draw_pattern(surface, node, name, opacity): pattern_node['width'] = pattern_width pattern_node['height'] = pattern_height if pattern_node.get('patternContentUnits') == 'objectBoundingBox': - pattern_node['transform'] = 'scale({}, {})'.format( - width, height) + pattern_node['transform'] = f'scale({width}, {height})' # Fail if pattern has an invalid size if pattern_width == 0.0 or pattern_height == 0.0: diff --git a/cairosvg/helpers.py b/cairosvg/helpers.py index 11b0ca09..3391c906 100644 --- a/cairosvg/helpers.py +++ b/cairosvg/helpers.py @@ -111,8 +111,9 @@ def preserve_ratio(surface, node, width=None, height=None): viewbox_width, viewbox_height = node.image_width, node.image_height else: raise TypeError( - ('Root node is {}. Should be one of ' - 'marker, svg, image, or g.').format(node.tag)) + f'Root node is {node.tag}. Should be one of ' + 'marker, svg, image, or g.' + ) translate_x = 0 translate_y = 0 diff --git a/cairosvg/parser.py b/cairosvg/parser.py index 06a65db5..75c89563 100644 --- a/cairosvg/parser.py +++ b/cairosvg/parser.py @@ -168,7 +168,7 @@ def __init__(self, element, style, url_fetcher, parent=None, self.tag = ( element.local_name if element.namespace_url in ('', 'http://www.w3.org/2000/svg') else - '{%s}%s' % (element.namespace_url, element.local_name)) + f'{{{element.namespace_url}}}{element.local_name}') self.text = node.text self.url_fetcher = url_fetcher self.unsafe = unsafe @@ -406,7 +406,7 @@ def __init__(self, **kwargs): break else: raise TypeError( - 'No tag with id="{}" found.'.format(element_id)) + f'No tag with id="{element_id}" found.') super().__init__( root, style, self.url_fetcher, parent, parent_children, self.url, unsafe) diff --git a/cairosvg/path.py b/cairosvg/path.py index 30c618e5..75e0d037 100644 --- a/cairosvg/path.py +++ b/cairosvg/path.py @@ -20,7 +20,7 @@ def draw_markers(surface, node): markers = {} common_marker = parse_url(node.get('marker', '')).fragment for position in ('start', 'mid', 'end'): - attribute = 'marker-{}'.format(position) + attribute = f'marker-{position}' if attribute in node: markers[position] = parse_url(node[attribute]).fragment else: @@ -125,7 +125,7 @@ def path(surface, node): node.vertices = [] for letter in PATH_LETTERS: - string = string.replace(letter, ' {} '.format(letter)) + string = string.replace(letter, f' {letter} ') last_letter = None string = normalize(string) @@ -190,10 +190,10 @@ def path(surface, node): # As we replace the current operation by l, we must be sure # that the next letter is set to the real current letter (a # or A) in case it’s omitted - next_letter = '{} '.format(letter) + next_letter = f'{letter} ' else: next_letter = '' - string = 'l {} {} {}{}'.format(x3, y3, next_letter, string) + string = f'l {x3} {y3} {next_letter}{string}' continue radii_ratio = ry / rx diff --git a/cairosvg/test_api.py b/cairosvg/test_api.py index 0089500e..a84e4fc5 100644 --- a/cairosvg/test_api.py +++ b/cairosvg/test_api.py @@ -74,7 +74,7 @@ def test_api(): # Read from a filename assert svg2png(url=temp_0) == expected_content assert svg2png( - url='file://{}'.format(temp_0)) == expected_content + url=f'file://{temp_0}') == expected_content with open(temp_0, 'rb') as file_object: # Read from a real file object diff --git a/cairosvg/text.py b/cairosvg/text.py index 559c42d5..adcf3fe4 100644 --- a/cairosvg/text.py +++ b/cairosvg/text.py @@ -54,14 +54,14 @@ def text(surface, node, draw_as_text=False): font_family = ( (node.get('font-family') or 'sans-serif').split(',')[0].strip('"\' ')) font_style = getattr( - cairo, ('font_slant_{}'.format(node.get('font-style')).upper()), + cairo, f'font_slant_{node.get("font-style")}'.upper(), cairo.FONT_SLANT_NORMAL) node_font_weight = node.get('font-weight') if (node_font_weight and node_font_weight.isdigit() and int(node_font_weight) >= 550): node_font_weight = 'bold' font_weight = getattr( - cairo, ('font_weight_{}'.format(node_font_weight).upper()), + cairo, (f'font_weight_{node_font_weight}'.upper()), cairo.FONT_WEIGHT_NORMAL) surface.context.select_font_face(font_family, font_style, font_weight) surface.context.set_font_size(surface.font_size) diff --git a/cairosvg/url.py b/cairosvg/url.py index 7b184e6e..13192822 100644 --- a/cairosvg/url.py +++ b/cairosvg/url.py @@ -11,7 +11,7 @@ from . import VERSION -HTTP_HEADERS = {'User-Agent': 'CairoSVG {}'.format(VERSION)} +HTTP_HEADERS = {'User-Agent': f'CairoSVG {VERSION}'} URL = re.compile(r'url\((.+)\)') @@ -132,7 +132,7 @@ def parse_url(url, base=None): else: url = '' if parsed_url.fragment: - url = '{}#{}'.format(url, parsed_url.fragment) + url = f'{url}#{parsed_url.fragment}' elif parsed_url.scheme in ('', parsed_base.scheme): # `urljoin` automatically uses the "folder" part of `base` url = urljoin(base, url) @@ -148,7 +148,7 @@ def read_url(url, url_fetcher, resource_type): if url.scheme: url = url.geturl() else: - url = 'file://{}'.format(os.path.abspath(url.geturl())) + url = f'file://{os.path.abspath(url.geturl())}' url = normalize_url(url) return url_fetcher(url, resource_type) diff --git a/test_non_regression/test_non_regression.py b/test_non_regression/test_non_regression.py index a09c1f43..6419cbce 100644 --- a/test_non_regression/test_non_regression.py +++ b/test_non_regression/test_non_regression.py @@ -40,5 +40,5 @@ def test_image(svg_filename): test_surface.finish() raise AssertionError( - 'Images are different: {} {}'.format( - ref_png.name, test_png.name)) + f'Images are different: {ref_png.name} {test_png.name}' + )