dag_cbor.random

Functions to generate random data.

default_options

default_options()[source]

Readonly view of the default random generation options.

Return type:

Mapping[str, Any]

get_options

get_options()[source]

Readonly view of the current random generation options.

Return type:

Mapping[str, Any]

options

options(*, seed=None, min_int=None, max_int=None, min_bytes=None, max_bytes=None, min_chars=None, max_chars=None, min_codepoint=None, max_codepoint=None, min_len=None, max_len=None, max_nesting=None, canonical=None, min_float=None, max_float=None, float_decimals=None, include_cid=None)[source]

Returns with-statement context manager for temporary option setting:

with options(**options):
    for value in rand_data(num_samples):
        ...

Options available:

seed: int           # set new random number generator, with this seed
min_int: int        # smallest `int` value
max_int: int        # largest `int` value
min_bytes: int      # min length of `bytes` value
max_bytes: int      # max length of `bytes` value
min_chars: int      # min length of `str` value
max_chars: int      # max length of `str` value
min_codepoint: int  # min utf-8 codepoint in `str` value
max_codepoint: int  # max utf-8 codepoint in `str` value
min_len: int        # min length of `list` and `dict` values
max_len: int        # max length of `list` and `dict` values
max_nesting: int    # max nesting of collections
canonical: bool     # whether `dict` values have canonically ordered keys
min_float: float    # smallest `float` value
max_float: float    # largest `float` value
float_decimals: int # number of decimals to keep in floats
include_cid: bool   # whether to generate CID values
Parameters:
Return type:

Iterator[None]

rand_bool

rand_bool(n=None)[source]

Generates a stream of random bool data.

Parameters:

n (Optional[int]) – the number of samples to be yielded; if None, an infinite stream is yielded

Return type:

Iterator[bool]

rand_bool_none

rand_bool_none(n=None)[source]

Generates a stream of random bool or None data.

Parameters:

n (Optional[int]) – the number of samples to be yielded; if None, an infinite stream is yielded

Return type:

Iterator[Optional[bool]]

rand_bytes

rand_bytes(n=None, *, length=None)[source]

Generates a stream of random bytes data.

Parameters:
  • n (Optional[int]) – the number of samples to be yielded; if None, an infinite stream is yielded

  • length (Optional[int]) – length of the bytestrings; if None, a random value is sampled according to the options

Return type:

Iterator[bytes]

rand_cid

rand_cid(n=None, *, max_nesting=None)[source]

Generates a stream of random CIDs:

  • multibase ‘base32’

  • CIDv1

  • multicodec ‘dag-cbor’

  • multihash ‘sha3-512’, with full 512-bit digest

Parameters:
  • n (Optional[int]) – the number of samples to be yielded; if None, an infinite stream is yielded

  • max_nesting (Optional[int]) – the maximum nesting level for containers; if None, value from get_options is used

Return type:

Iterator[CID]

rand_data

rand_data(n=None, *, max_nesting=None)[source]

Generates a stream of random data.

Parameters:
  • n – the number of samples to be yielded; if None, an infinite stream is yielded

  • max_nesting – the maximum nesting level for containers; if None, value from get_options is used

Maximum nesting level for containers:

  • the integer value -1 means no containers will be generated

  • integer values >= 0 mean that containers will be generated, with items generated by random_data(max_nesting=max_nesting-1)

  • no other values are valid

Return type:

Iterator[IPLDKind]

rand_data_cid

rand_data_cid(n=None, *, max_nesting=None)[source]

Generates a stream of random DAG-CBOR data and associated CIDs:

  • multibase ‘base32’

  • CIDv1

  • multicodec ‘dag-cbor’

  • multihash ‘sha3-512’, with full 512-bit digest

Parameters:
  • n (Optional[int]) – the number of samples to be yielded; if None, an infinite stream is yielded

  • max_nesting (Optional[int]) – the maximum nesting level for containers; if None, value from get_options is used

Return type:

Iterator[Tuple[IPLDKind, CID]]

rand_dict

rand_dict(n=None, *, length=None, max_nesting=None)[source]

Generates a stream of random dict data.

Parameters:
  • n (Optional[int]) – the number of samples to be yielded; if None, an infinite stream is yielded

  • length (Optional[int]) – size for the dicts; if None, a random value is sampled according to the options

  • max_nesting (Optional[int]) – the maximum nesting level for containers; if None, value from get_options is used

Return type:

Iterator[Dict[str, Any]]

rand_float

rand_float(n=None)[source]

Generates a stream of random float data.

Parameters:

n (Optional[int]) – the number of samples to be yielded; if None, an infinite stream is yielded

Return type:

Iterator[float]

rand_int

rand_int(n=None)[source]

Generates a stream of random int data.

Parameters:

n (Optional[int]) – the number of samples to be yielded; if None, an infinite stream is yielded

Return type:

Iterator[int]

rand_list

rand_list(n=None, *, length=None, max_nesting=None)[source]

Generates a stream of random list data.

Parameters:
  • n (Optional[int]) – the number of samples to be yielded; if None, an infinite stream is yielded

  • length (Optional[int]) – size for the lists; if None, a random value is sampled according to the options

  • max_nesting (Optional[int]) – the maximum nesting level for containers; if None, value from get_options is used

Return type:

Iterator[List[Any]]

rand_str

rand_str(n=None, *, length=None)[source]

Generates a stream of random str data.

Parameters:
  • n (Optional[int]) – the number of samples to be yielded; if None, an infinite stream is yielded

  • length (Optional[int]) – length of the strings; if None, a random value is sampled according to the options

Return type:

Iterator[str]

reset_options

reset_options()[source]

Resets random generation options to their default values.

Return type:

None

set_options

set_options(*, seed=None, min_int=None, max_int=None, min_bytes=None, max_bytes=None, min_chars=None, max_chars=None, min_codepoint=None, max_codepoint=None, min_len=None, max_len=None, max_nesting=None, canonical=None, min_float=None, max_float=None, float_decimals=None, include_cid=None)[source]

Permanently sets random generation options. See options for the available options.

Parameters:
Return type:

None