prefer f-strings

This commit is contained in:
Jan Petykiewicz 2020-08-15 18:23:04 -07:00
parent 3ec28d4720
commit e7c8708f7f
6 changed files with 21 additions and 20 deletions

View File

@ -370,17 +370,18 @@ def disambiguate_pattern_names(patterns,
i += 1 i += 1
if sanitized_name == '': if sanitized_name == '':
logger.warning('Empty pattern name saved as "{}"'.format(suffixed_name)) logger.warning(f'Empty pattern name saved as "{suffixed_name}"')
elif suffixed_name != sanitized_name: elif suffixed_name != sanitized_name:
if dup_warn_filter is None or dup_warn_filter(pat.name): if dup_warn_filter is None or dup_warn_filter(pat.name):
logger.warning('Pattern name "{}" ({}) appears multiple times;\n renaming to "{}"'.format( logger.warning(f'Pattern name "{pat.name}" ({sanitized_name}) appears multiple times;\n' +
pat.name, sanitized_name, suffixed_name)) f' renaming to "{suffixed_name}"')
if len(suffixed_name) == 0: if len(suffixed_name) == 0:
# Should never happen since zero-length names are replaced # Should never happen since zero-length names are replaced
raise PatternError('Zero-length name after sanitize,\n originally "{}"'.format(pat.name)) raise PatternError(f'Zero-length name after sanitize,\n originally "{pat.name}"')
if len(suffixed_name) > max_name_length: if len(suffixed_name) > max_name_length:
raise PatternError('Pattern name "{!r}" length > {} after encode,\n originally "{}"'.format(suffixed_name, max_name_length, pat.name)) raise PatternError(f'Pattern name "{suffixed_name!r}" length > {max_name_length} after encode,\n' +
f' originally "{pat.name}"')
pat.name = suffixed_name pat.name = suffixed_name
used_names.append(suffixed_name) used_names.append(suffixed_name)

View File

@ -484,8 +484,8 @@ def disambiguate_pattern_names(patterns: Sequence[Pattern],
# Shorten names which already exceed max-length # Shorten names which already exceed max-length
if len(pat.name) > max_name_length: if len(pat.name) > max_name_length:
shortened_name = pat.name[:max_name_length - suffix_length] shortened_name = pat.name[:max_name_length - suffix_length]
logger.warning('Pattern name "{}" is too long ({}/{} chars),\n'.format(pat.name, len(pat.name), max_name_length) + logger.warning(f'Pattern name "{pat.name}" is too long ({len(pat.name)}/{max_name_length} chars),\n' +
' shortening to "{}" before generating suffix'.format(shortened_name)) f' shortening to "{shortened_name}" before generating suffix')
else: else:
shortened_name = pat.name shortened_name = pat.name
@ -502,19 +502,20 @@ def disambiguate_pattern_names(patterns: Sequence[Pattern],
i += 1 i += 1
if sanitized_name == '': if sanitized_name == '':
logger.warning('Empty pattern name saved as "{}"'.format(suffixed_name)) logger.warning(f'Empty pattern name saved as "{suffixed_name}"')
elif suffixed_name != sanitized_name: elif suffixed_name != sanitized_name:
if dup_warn_filter is None or dup_warn_filter(pat.name): if dup_warn_filter is None or dup_warn_filter(pat.name):
logger.warning('Pattern name "{}" ({}) appears multiple times;\n renaming to "{}"'.format( logger.warning(f'Pattern name "{pat.name}" ({sanitized_name}) appears multiple times;\n' +
pat.name, sanitized_name, suffixed_name)) f' renaming to "{suffixed_name}"')
# Encode into a byte-string and perform some final checks # Encode into a byte-string and perform some final checks
encoded_name = suffixed_name.encode('ASCII') encoded_name = suffixed_name.encode('ASCII')
if len(encoded_name) == 0: if len(encoded_name) == 0:
# Should never happen since zero-length names are replaced # Should never happen since zero-length names are replaced
raise PatternError('Zero-length name after sanitize+encode,\n originally "{}"'.format(pat.name)) raise PatternError(f'Zero-length name after sanitize+encode,\n originally "{pat.name}"')
if len(encoded_name) > max_name_length: if len(encoded_name) > max_name_length:
raise PatternError('Pattern name "{!r}" length > {} after encode,\n originally "{}"'.format(encoded_name, max_name_length, pat.name)) raise PatternError(f'Pattern name "{encoded_name!r}" length > {max_name_length} after encode,\n' +
f' originally "{pat.name}"')
pat.name = suffixed_name pat.name = suffixed_name
used_names.append(suffixed_name) used_names.append(suffixed_name)

View File

@ -593,15 +593,15 @@ def disambiguate_pattern_names(patterns,
i += 1 i += 1
if sanitized_name == '': if sanitized_name == '':
logger.warning('Empty pattern name saved as "{}"'.format(suffixed_name)) logger.warning(f'Empty pattern name saved as "{suffixed_name}"')
elif suffixed_name != sanitized_name: elif suffixed_name != sanitized_name:
if dup_warn_filter is None or dup_warn_filter(pat.name): if dup_warn_filter is None or dup_warn_filter(pat.name):
logger.warning('Pattern name "{}" ({}) appears multiple times;\n renaming to "{}"'.format( logger.warning(f'Pattern name "{pat.name}" ({sanitized_name}) appears multiple times;\n' +
pat.name, sanitized_name, suffixed_name)) f' renaming to "{suffixed_name}"')
if len(suffixed_name) == 0: if len(suffixed_name) == 0:
# Should never happen since zero-length names are replaced # Should never happen since zero-length names are replaced
raise PatternError('Zero-length name after sanitize+encode,\n originally "{}"'.format(pat.name)) raise PatternError(f'Zero-length name after sanitize+encode,\n originally "{pat.name}"')
pat.name = suffixed_name pat.name = suffixed_name
used_names.append(suffixed_name) used_names.append(suffixed_name)

View File

@ -79,8 +79,7 @@ def writefile(pattern: Pattern,
for subpat in pat.subpatterns: for subpat in pat.subpatterns:
if subpat.pattern is None: if subpat.pattern is None:
continue continue
transform = 'scale({:g}) rotate({:g}) translate({:g},{:g})'.format( transform = f'scale({subpat.scale:g}) rotate({subpat.rotation:g}) translate({subpat.offset[0]:g},{subpat.offset[1]:g})'
subpat.scale, subpat.rotation, subpat.offset[0], subpat.offset[1])
use = svg.use(href='#' + mangle_name(subpat.pattern), transform=transform) use = svg.use(href='#' + mangle_name(subpat.pattern), transform=transform)
if custom_attributes: if custom_attributes:
use['pattern_dose'] = subpat.dose use['pattern_dose'] = subpat.dose

View File

@ -322,7 +322,7 @@ class Path(Shape, metaclass=AutoSlots):
bounds[0, :] = numpy.minimum(bounds[0, :], poly_bounds[0, :]) bounds[0, :] = numpy.minimum(bounds[0, :], poly_bounds[0, :])
bounds[1, :] = numpy.maximum(bounds[1, :], poly_bounds[1, :]) bounds[1, :] = numpy.maximum(bounds[1, :], poly_bounds[1, :])
else: else:
raise PatternError('get_bounds() not implemented for endcaps: {}'.format(self.cap)) raise PatternError(f'get_bounds() not implemented for endcaps: {self.cap}')
return bounds return bounds

View File

@ -104,7 +104,7 @@ class SubPattern(PositionableImpl, DoseableImpl, RotatableImpl, ScalableImpl, Mi
def pattern(self, val: Optional['Pattern']): def pattern(self, val: Optional['Pattern']):
from .pattern import Pattern from .pattern import Pattern
if val is not None and not isinstance(val, Pattern): if val is not None and not isinstance(val, Pattern):
raise PatternError('Provided pattern {} is not a Pattern object or None!'.format(val)) raise PatternError(f'Provided pattern {val} is not a Pattern object or None!')
self._pattern = val self._pattern = val
# Mirrored property # Mirrored property