plato.context module

Context management

class plato.context.Context(hasher, parent=None, meta=None)[source]

Bases: object

Context used in sampling from Provider instances.

Parameters
  • hasher – Hasher used to derive the random number seed and to derive hashers for subcontexts.

  • parent (Optional[plato.context.Context]) – The parent context if any.

  • meta (dict) – A dictionary that can be used by Provider instances to store additional information in the context. Be aware that the passed instance might be modified.

meta: dict[source]

Dictionary that can be used by providers to store additional information across invocations of Provider.sample(). Use the Provider instance or concrete class as key to avoid key collisions with other providers.

parent: Optional[plato.context.Context][source]

The parent context or None if this is a root context.

rng: random.Random[source]

A seeded random number generator that may be used for the generation of random numbers.

seed: bytes[source]

Seed to use for the generation of random numbers.

subcontext(name)[source]

Derive a subcontext.

A subcontext is derived by updating a copy of the hasher with the name, setting the parent accordingly, and (flat) copying the meta dictionary.

Parameters

name (str) – A name to identify the subcontext. Reusing the same name will give a subcontext with the same random number seed.

Returns

The derived subcontext.

Return type

Context

class plato.context.Hasher(*args, **kwargs)[source]

Bases: Protocol

Protocol of classes to perform incremental hashing.

copy()[source]

Create a copy of the current hasher state.

Return type

plato.context.Hasher

digest()[source]

Return the current hash.

Return type

bytes

update(data)[source]

Update the hash with data.

Parameters

data (bytes) –

Return type

None

plato.context.get_root_context(type_)[source]

Get a root context for a given type.

Parameters

type_ (Type) –

Return type

plato.context.Context

plato.context.seed(value)[source]

Set the global Plato base seed.

Parameters

value (int) –

Return type

None