bw2io.package
#
Module Contents#
Classes#
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. |
Functions#
- 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 }
- 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.
Note
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 to export.
filename (*) – Name of file to create. Default is
obj.name
.folder (*) – Folder to create file in. Default is
export
.backwards_compatible (*) – Create package compatible with bw2data version 1.
- Returns
Filepath of created file.
- classmethod export_objs(objs, filename, folder='export', backwards_compatible=False)[source]#
Export a list of objects. Can have heterogeneous types.
- Parameters
objs (*) – List of objects to export.
filename (*) – Name of file to create.
folder (*) – Folder to create file in. Default is
export
.backwards_compatible (*) – Create package compatible with bw2data version 1.
- Returns
Filepath of created file.
- 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 (*) – Path of file to import
whitelist (*) – Apply whitelist to allowed types. Default is
True
.
- Returns
Created object or list of created objects.
- classmethod load_file(filepath, whitelist=True)[source]#
Load a bw2package file with one or more objects. Does not create new objects.
- Parameters
filepath (*) – Path of file to import
whitelist (*) – Apply whitelist of approved classes to allowed types. Default is
True
.
- Returns the loaded data in the bw2package dict data format, with the following changes:
"class"
is an actual Python class object (but not instantiated).