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
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
- scenicToJSON(obj)[source]
Utility function to help serialize Scenic objects to JSON.
Suitable for passing as the
default
argument 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:
Exception
An 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 ofDistribution
whose 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
Distribution
is encoded as follows:If the
Distribution
is_deterministic
, recursively encode the sampled values of its dependencies.If its valueType is a type for which we have a “codec” (like
int
orfloat
), use the encoding function provided by the codec.If the valueType provides a
encodeTo
method, 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
Distribution
cannot 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 implementencodeTo
anddecodeFrom
methods: seeVector
for example. If for some reason you can’t add those methods to the class in question, you can useSerializer.addCodec
to 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 usepickle
to encode any objects for which no specialized encoder is known.- classmethod sceneFormatVersion()[source]
Current version of the
Scene
serialization format.Must be incremented if the
writeScene
method or any of its helper methods (e.g.writeValue
) change, or if a new codec is added.
- classmethod replayFormatVersion()[source]
Current version of the
Simulation
replay serialization format.Must be incremented if the
writeReplayHeader
orwriteValue
methods change, or if a new codec is added.
- writeReplayHeader(flags)[source]
Begin the encoding of a
Simulation
replay.
- 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.