bw2io.package#
Classes#
This is a format for saving objects which implement the datastore API. |
Functions#
Module Contents#
- class bw2io.package.BW2Package[source]#
Bases:
object
This is a format for saving objects which implement the datastore API.
Data is stored as a BZip2-compressed file of JSON data.
This archive format is compatible across Python versions, and is, at least in theory, programming-language agnostic.
Validation is done with
bw2data.validate.bw2package_validator
. The data format is:{ 'metadata': {}, # Dictionary of metadata to be written to metadata-store. 'name': basestring, # Name of object 'class': { # Data on the underlying class. A new class is instantiated # based on these strings. See _create_class. 'module': basestring, # e.g. "bw2data.database" 'name': basestring # e.g. "Database" }, 'unrolled_dict': bool, # Flag indicating if dictionary keys needed to # be modified for JSON (as JSON keys can't be tuples) 'data': object # Object data, e.g. LCIA method or LCI database }
Warning
- Perfect roundtrips between machines are not guaranteed:
All lists are converted to tuples (because JSON does not distinguish between lists and tuples).
Absolute filepaths in metadata would be specific to a certain computer and user.
Notes
This class does not need to be instantiated, as all its methods are
classmethods
, i.e. doBW2Package.import_obj("foo")
instead ofBW2Package().import_obj("foo")
- classmethod export_obj(obj, filename=None, folder='export', backwards_compatible=False)[source]#
Export an object.
- Parameters:
obj (object) β Object to export.
filename (str, optional) β Name of file to create. Default is
obj.name
.folder (str, optional) β Folder to create file in. Default is
export
.backwards_compatible (bool, optional) β Create package compatible with bw2data version 1.
- Returns:
Filepath of created file.
- Return type:
str
- classmethod export_objs(objs, filename, folder='export', backwards_compatible=False)[source]#
Export a list of objects. Can have heterogeneous types.
- Parameters:
objs (list) β List of objects to export.
filename (str) β Name of file to create.
folder (str, optional) β Folder to create file in. Default is
export
.backwards_compatible (bool, optional) β Create package compatible with bw2data version 1.
- Returns:
Filepath of created file.
- Return type:
str
- classmethod import_file(filepath, whitelist=True)[source]#
Import bw2package file, and create the loaded objects, including registering, writing, and processing the created objects.
- Parameters:
filepath (str) β Path of file to import
whitelist (bool) β Apply whitelist to allowed types. Default is
True
.
- Returns:
Created object or list of created objects.
- Return type:
object or list of objects
- classmethod load_file(filepath, whitelist=True)[source]#
Load a bw2package file with one or more objects. Does not create new objects.
- Parameters:
filepath (str) β Path of file to import
whitelist (bool) β Apply whitelist of approved classes to allowed types. Default is
True
.
- Returns:
"class"
is an actual Python class object (but not instantiated).
- Return type:
The loaded data in the bw2package dict data format, with the following changes