From 7340d41f10a8397e0d166fc1d9940f8024ddfa00 Mon Sep 17 00:00:00 2001 From: Jan Petykiewicz Date: Sun, 24 Oct 2021 19:05:48 -0700 Subject: [PATCH] Use f-strings where possible --- gridlock/draw.py | 10 +++++----- gridlock/grid.py | 6 +++--- gridlock/position.py | 8 ++++---- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/gridlock/draw.py b/gridlock/draw.py index 848e246..1142528 100644 --- a/gridlock/draw.py +++ b/gridlock/draw.py @@ -48,7 +48,7 @@ def draw_polygons(self, surface = numpy.delete(range(3), surface_normal) for i, polygon in enumerate(polygons): - malformed = 'Malformed polygon: (%i)' % i + malformed = f'Malformed polygon: ({i})' if polygon.shape[1] not in (2, 3): raise GridError(malformed + 'must be a Nx2 or Nx3 ndarray') if polygon.shape[1] == 3: @@ -106,9 +106,9 @@ def draw_polygons(self, # evaluate on the meshgrid eps_i = eps[i](x0, y0, z0) if not numpy.isfinite(eps_i).all(): - raise GridError('Non-finite values in eps[%u]' % i) - raise GridError('Unsupported eps[{}]: {}'.format(i, type(eps[i]))) + raise GridError(f'Non-finite values in eps[{i}]') elif numpy.size(eps[i]) != 1: + raise GridError(f'Unsupported eps[{i}]: {type(eps[i])}') else: # eps[i] is scalar non-callable eps_i = eps[i] @@ -232,7 +232,7 @@ def draw_slab(self, if len(center) == 3: center = center[surface_normal] else: - raise GridError('Bad center: {}'.format(center)) + raise GridError(f'Bad center: {center}') # Find center of slab center_shift = self.center @@ -331,7 +331,7 @@ def draw_extrude_rectangle(self, if s == 0: raise GridError('0 is not a valid polarity') if direction not in range(3): - raise GridError('Invalid direction: {}'.format(direction)) + raise GridError(f'Invalid direction: {direction}') if rectangle[0, direction] != rectangle[1, direction]: raise GridError('Rectangle entries along extrusion direction do not match.') diff --git a/gridlock/grid.py b/gridlock/grid.py index f18390d..9e828d1 100644 --- a/gridlock/grid.py +++ b/gridlock/grid.py @@ -279,7 +279,7 @@ class Grid: for i in range(3): if len(self.exyz[i]) != len(pixel_edge_coordinates[i]): - warnings.warn('Dimension {} had duplicate edge coordinates'.format(i), stacklevel=2) + warnings.warn(f'Dimension {i} had duplicate edge coordinates', stacklevel=2) if isinstance(periodic, bool): self.periodic = [periodic] * 3 @@ -288,10 +288,10 @@ class Grid: if len(self.shifts.shape) != 2: raise GridError('Misshapen shifts: shifts must have two axes! ' - ' The given shifts has shape {}'.format(self.shifts.shape)) + f' The given shifts has shape {self.shifts.shape}') if self.shifts.shape[1] != 3: raise GridError('Misshapen shifts; second axis size should be 3,' - ' shape is {}'.format(self.shifts.shape)) + f' shape is {self.shifts.shape}') if (numpy.abs(self.shifts) > 1).any(): raise GridError('Only shifts in the range [-1, 1] are currently supported') diff --git a/gridlock/position.py b/gridlock/position.py index 02856e8..1224a12 100644 --- a/gridlock/position.py +++ b/gridlock/position.py @@ -46,7 +46,7 @@ def ind2pos(self, low_bound = -0.5 high_bound = -0.5 if (ind < low_bound).any() or (ind > self.shape - high_bound).any(): - raise GridError('Position outside of grid: {}'.format(ind)) + raise GridError(f'Position outside of grid: {ind}') if round_ind: rind = numpy.clip(numpy.round(ind).astype(int), 0, self.shape - 1) @@ -84,17 +84,17 @@ def pos2ind(self, """ r = numpy.squeeze(r) if r.size != 3: - raise GridError('r must be 3-element vector: {}'.format(r)) + raise GridError(f'r must be 3-element vector: {r}') if (which_shifts is not None) and (which_shifts >= self.shifts.shape[0]): - raise GridError('Invalid which_shifts: {}'.format(which_shifts)) + raise GridError(f'Invalid which_shifts: {which_shifts}') sexyz = self.shifted_exyz(which_shifts) if check_bounds: for a in range(3): if self.shape[a] > 1 and (r[a] < sexyz[a][0] or r[a] > sexyz[a][-1]): - raise GridError('Position[{}] outside of grid!'.format(a)) + raise GridError(f'Position[{a}] outside of grid!') grid_pos = numpy.zeros((3,)) for a in range(3):