scenic.core.serialization
Utilities to help serialize Scenic objects.
The functions in this module usually do not need to be used directly.
For high-level serialization APIs, see Scenario.sceneToBytes,
Scenario.simulationToBytes, and Scene.dumpAsScenicCode.
Summary of Module Members
Functions
Compute a deterministic hash for a mapping of options. |
|
Utility function to help export Scenic objects as Scenic code. |
|
|
|
|
|
|
|
|
|
|
|
|
|
Utility function to help serialize Scenic objects to JSON. |
|
|
|
|
|
|
|
|
|
|
Classes
Class for (de)serializing scenes, etc. |
Exceptions
An error occurring during serialization/deserialization of Scenic objects. |
Member Details
- deterministicHash(mapping, *, digest_size=8)[source]
Compute a deterministic hash for a mapping of options.
Keys are sorted (by their string representation) and encoded with explicit separators so that different key/value combinations do not collide under simple concatenation.
Only int/float/str (and bool, as a subclass of int) values are encoded directly; any other value is replaced by a generic placeholder byte, to avoid nondeterminism from reprs containing memory addresses or other run-specific data.
- scenicToJSON(obj)[source]
Utility function to help serialize Scenic objects to JSON.
Suitable for passing as the
defaultargument tojson.dump. At the moment this only supports very basic types like scalars and vectors: it does not allow encoding of an entireObject.
- dumpAsScenicCode(value, stream)[source]
Utility function to help export Scenic objects as Scenic code.
- exception SerializationError[source]
Bases:
ExceptionAn error occurring during serialization/deserialization of Scenic objects.
- class Serializer(data=b'', allowPickle=False, detectEnd=False)[source]
Class for (de)serializing scenes, etc.
Ordinary Scenic users do not need to know about this class: they can use public APIs such as
Scenario.sceneToBytes. If you have defined a custom type ofDistributionwhose valueType isn’t one of the types used by the built-in primitive distributions (i.e.int,float,Vector), read on.The sampled value of a
Distributionis encoded as follows:If the
Distributionis_deterministic, recursively encode the sampled values of its dependencies.If its valueType is a type for which we have a “codec” (like
intorfloat), use the encoding function provided by the codec.If the valueType provides a
encodeTomethod, use that.If the user has allowed the use of
pickle, pickle the value.Otherwise raise a
SerializationError.
Thus, you need only extend the serialization mechanism if your
Distributioncannot be made deterministic (by adding appropriate dependencies with simpler valueTypes) and it has an unusual valueType. In that case, it’s best to have your valueType implementencodeToanddecodeFrommethods: seeVectorfor example. If for some reason you can’t add those methods to the class in question, you can useSerializer.addCodecto register encoder/decoder functions. Finally, if you’re only using serialization internally and aren’t concerned about security issues or making the encoding as compact as possible, you can turn on the allowPickle option: this will usepickleto encode any objects for which no specialized encoder is known.- classmethod sceneFormatVersion()[source]
Current version of the
Sceneserialization format.Must be incremented if the
writeScenemethod or any of its helper methods (e.g.writeValue) change, or if a new codec is added.
- classmethod replayFormatVersion()[source]
Current version of the
Simulationreplay serialization format.Must be incremented if the
writeReplayHeaderorwriteValuemethods change, or if a new codec is added.
- writeReplayHeader(flags)[source]
Begin the encoding of a
Simulationreplay.
- classmethod addCodec(ty, encoder, decoder)[source]
Register encoder and decoder functions for the given type.
The encoder function should have signature
encoder(value, stream)with stream a binary file-like object. The decoder function should have signaturedecoder(stream)and return the decoded value.