From 0c1f30e0c0a976a8941b54ab3219aeb0fb997548 Mon Sep 17 00:00:00 2001 From: Bruce Merry Date: Wed, 2 Aug 2023 10:52:03 +0200 Subject: [PATCH] Manually eliminate some calls to str.format --- src/spead2/__init__.py | 8 ++------ src/spead2/tools/bench_asyncio.py | 7 ++----- src/spead2/tools/recv_asyncio.py | 14 ++++++-------- src/spead2/tools/send_asyncio.py | 6 +----- tests/test_send.py | 8 ++++++-- 5 files changed, 17 insertions(+), 26 deletions(-) diff --git a/src/spead2/__init__.py b/src/spead2/__init__.py index 3f8c0cf33..51d6aea76 100644 --- a/src/spead2/__init__.py +++ b/src/spead2/__init__.py @@ -515,9 +515,7 @@ def set_from_raw(self, raw_item, new_order="="): elements = _shape_elements(shape) if elements > max_elements: raise ValueError( - "Item {} has too few elements for shape ({} < {})".format( - self.name, max_elements, elements - ) + f"Item {self.name} has too few elements for shape ({max_elements} < {elements})" ) size_bytes = elements * self._internal_dtype.itemsize if raw_item.is_immediate: @@ -550,9 +548,7 @@ def set_from_raw(self, raw_item, new_order="="): bits = elements * itemsize_bits if elements > max_elements: raise ValueError( - "Item {} has too few elements for shape ({} < {})".format( - self.name, max_elements, elements - ) + f"Item {self.name} has too few elements for shape ({max_elements} < {elements})" ) if raw_item.is_immediate: # Immediates get head padding instead of tail padding diff --git a/src/spead2/tools/bench_asyncio.py b/src/spead2/tools/bench_asyncio.py index 969e460be..66605d572 100644 --- a/src/spead2/tools/bench_asyncio.py +++ b/src/spead2/tools/bench_asyncio.py @@ -282,11 +282,8 @@ async def run_master(args, protocol, sender, receiver): args, protocol, sender, receiver, rate, num_heaps, num_heaps - 1 ) if not args.quiet: - print( - "Rate: {:.3f} Gbps ({:.3f} actual): {}".format( - rate * 8e-9, actual_rate * 8e-9, "GOOD" if good else "BAD" - ) - ) + result = "GOOD" if good else "BAD" + print(f"Rate: {rate * 8e-9:.3f} Gbps ({actual_rate * 8e-9:.3f} actual): {result}") if good: low = rate best_actual = actual_rate diff --git a/src/spead2/tools/recv_asyncio.py b/src/spead2/tools/recv_asyncio.py index e9edc77ec..1d887fa3a 100644 --- a/src/spead2/tools/recv_asyncio.py +++ b/src/spead2/tools/recv_asyncio.py @@ -73,14 +73,12 @@ async def run_stream(stream, name, args): for raw_descriptor in heap.get_descriptors(): descriptor = spead2.Descriptor.from_raw(raw_descriptor, heap.flavour) print( - """\ - Descriptor for {0.name} ({0.id:#x}) - description: {0.description} - format: {0.format} - dtype: {0.dtype} - shape: {0.shape}""".format( - descriptor - ) + f"""\ + Descriptor for {descriptor.name} ({descriptor.id:#x}) + description: {descriptor.description} + format: {descriptor.format} + dtype: {descriptor.dtype} + shape: {descriptor.shape}""" ) changed = item_group.update(heap) for key, item in changed.items(): diff --git a/src/spead2/tools/send_asyncio.py b/src/spead2/tools/send_asyncio.py index 1f6d9b3cf..f26c89a86 100644 --- a/src/spead2/tools/send_asyncio.py +++ b/src/spead2/tools/send_asyncio.py @@ -116,11 +116,7 @@ async def run(item_group, stream, args): elapsed = time.time() - start_time if last_error is not None: logging.warn("%d errors, last one: %s", n_errors, last_error) - print( - "Sent {} bytes in {:.6f}s, {:.6f} Gb/s".format( - n_bytes, elapsed, n_bytes * 8 / elapsed / 1e9 - ) - ) + print(f"Sent {n_bytes} bytes in {elapsed:.6f}s, {n_bytes * 8 / elapsed / 1e9:.6f} Gb/s") async def async_main(): diff --git a/tests/test_send.py b/tests/test_send.py index 6f2bcd834..3f6c642cc 100644 --- a/tests/test_send.py +++ b/tests/test_send.py @@ -157,8 +157,12 @@ def make_descriptor_numpy(self, id, name, description, shape, dtype_str, fortran b"description", b"", self.flavour.make_shape(shape), - "{{'descr': {!r}, 'fortran_order': {!r}, 'shape': {!r}}}".format( - str(dtype_str), bool(fortran_order), tuple(shape) + repr( + { + "descr": str(dtype_str), + "fortran_order": bool(fortran_order), + "shape": tuple(shape), + } ).encode(), ] payload = b"".join(payload_fields)