Skip to content

Commit

Permalink
Manually eliminate some calls to str.format
Browse files Browse the repository at this point in the history
  • Loading branch information
bmerry committed Aug 2, 2023
1 parent e60faff commit 0c1f30e
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 26 deletions.
8 changes: 2 additions & 6 deletions src/spead2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
7 changes: 2 additions & 5 deletions src/spead2/tools/bench_asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 6 additions & 8 deletions src/spead2/tools/recv_asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
6 changes: 1 addition & 5 deletions src/spead2/tools/send_asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
8 changes: 6 additions & 2 deletions tests/test_send.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 0c1f30e

Please sign in to comment.