Bytes factory

You can create a factory that generates bytes values with randbytes.

Note

Although a factory of bytes can be created by passing a bytes as an example to from_example, it is not possible to specify the creation condition.

Note

You can also create a factory that generates bytearray (mutable bytes) values with randbytearray. Arguments is same as for randbytes.

Simple factory

If all that is needed is for the generated value to be a bytes, use randbytes with no arguments as follows:

>>> import randog.factory

>>> # create a factory
>>> factory = randog.factory.randbytes()

>>> generated = factory.next()
>>> assert isinstance(generated, bytes)
>>> assert len(generated) != 0

Specify the length

The arguments length can be used to specify the length of the generated bytes.

>>> import string
>>> import randog.factory

>>> # create a factory
>>> factory = randog.factory.randbytes(length=8)

>>> generated = factory.next()
>>> assert isinstance(generated, bytes)
>>> assert len(generated) == 8

The length of the bytes can also be randomized by specifying randint() for length as follows:

>>> import string
>>> import randog.factory

>>> # create a factory
>>> factory = randog.factory.randbytes(length=randog.factory.randint(10, 19))

>>> generated = factory.next()
>>> assert isinstance(generated, bytes)
>>> assert 10 <= len(generated) <= 19