monty.serialization module¶
This module implements serialization support for common formats such as json and yaml.
-
dumpfn
(obj, fn, *args, fmt=None, **kwargs)[source]¶ Dump to a json/yaml directly by filename instead of a File-like object. File may also be a BZ2 (“.BZ2”) or GZIP (“.GZ”, “.Z”) compressed file. For YAML, ruamel.yaml must be installed. The file type is automatically detected from the file extension (case insensitive). YAML is assumed if the filename contains “.yaml” or “.yml”. Msgpack is assumed if the filename contains “.mpk”. JSON is otherwise assumed. The file type can be specified manually with “fmt=’type’”.
- Parameters
obj (object) – Object to dump.
fn (str/Path) – filename or pathlib.Path.
*args – Any of the args supported by json/yaml.dump.
fmt (str) – manually specify file format to read. Options are “json”, “yaml”, or “mpk”. If None, the file type will be detected automatically.
**kwargs – Any of the kwargs supported by json/yaml.dump.
- Returns
(object) Result of json.load.
-
loadfn
(fn, *args, fmt=None, **kwargs)[source]¶ Loads json/yaml/msgpack directly from a filename instead of a File-like object. File may also be a BZ2 (“.BZ2”) or GZIP (“.GZ”, “.Z”) compressed file. For YAML, ruamel.yaml must be installed. The file type is automatically detected from the file extension (case insensitive). YAML is assumed if the filename contains “.yaml” or “.yml”. Msgpack is assumed if the filename contains “.mpk”. JSON is otherwise assumed. The file type can be specified manually with “fmt=’type’”.
- Parameters
fn (str/Path) – filename or pathlib.Path.
*args – Any of the args supported by json/yaml.load.
fmt (str) – manually specify file format to read. Options are “json”, “yaml”, or “mpk”. If None, the file type will be detected automatically.
**kwargs – Any of the kwargs supported by json/yaml.load.
- Returns
(object) Result of json/yaml/msgpack.load.