dag_cbor.random
Functions to generate random data.
default_options
get_options
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:
rand_bool
rand_bool_none
rand_bytes
rand_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 yieldedmax_nesting – the maximum nesting level for containers; if
None
, value fromget_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
rand_data_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; ifNone
, an infinite stream is yieldedlength (
Optional
[int
]) – size for the dicts; ifNone
, a random value is sampled according to theoptions
max_nesting (
Optional
[int
]) – the maximum nesting level for containers; ifNone
, value fromget_options
is used
- Return type:
rand_float
rand_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; ifNone
, an infinite stream is yieldedlength (
Optional
[int
]) – size for the lists; ifNone
, a random value is sampled according to theoptions
max_nesting (
Optional
[int
]) – the maximum nesting level for containers; ifNone
, value fromget_options
is used
- Return type:
rand_str
reset_options
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: