pycyphal.transport.commons package

Subpackages

Module contents

This module does not implement a transport, and it is not a part of the abstract transport model. It contains a collection of software components implementing common logic reusable with different transport implementations. It is expected that some transport implementations may be unable to rely on these.

This module is unlikely to be useful for a regular library user (not a developer).

pycyphal.transport.commons.refragment(input_fragments: Iterable[memoryview], output_fragment_size: int) Iterable[memoryview][source]

Repackages the data from the arbitrarily-sized input fragments into fixed-size output fragments while minimizing the amount of data copying. The last fragment is allowed to be smaller than the requested size. If the input iterable contains no fragments or all of them are empty, nothing will be yielded.

This function is designed for use in transfer emission logic where it’s often needed to split a large payload into several frames while avoiding unnecessary copying. The best case scenario is when the size of input blocks is a multiple of the output fragment size – in this case no copy will be done.

>>> list(map(bytes, refragment([memoryview(b'0123456789'), memoryview(b'abcdef')], 7)))
[b'0123456', b'789abcd', b'ef']

The above example shows a marginally suboptimal case where one copy is required:

  • b'0123456789'[0:7] –> output b'0123456' (slicing, no copy)

  • b'0123456789'[7:10] –> temporary b'789' (slicing, no copy)

  • b'abcdef'[0:4] –> output b'789' + b'abcd' (copied into the temporary, which is then yielded)

  • b'abcdef'[4:6] –> output b'ef' (slicing, no copy)