bw2io.package#

Classes#

BW2Package

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. do BW2Package.import_obj("foo") instead of BW2Package().import_obj("foo")

classmethod _create_class(metadata, apply_whitelist=True)[source]#
classmethod _create_obj(data)[source]#
classmethod _get_class_metadata(obj)[source]#
classmethod _is_valid_package(data)[source]#
classmethod _is_whitelisted(metadata)[source]#
classmethod _load_obj(data, whitelist=True)[source]#
classmethod _prepare_obj(obj, backwards_compatible=False)[source]#
classmethod _write_file(filepath, data)[source]#
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

APPROVED[source]#
bw2io.package.download_biosphere()[source]#
bw2io.package.download_methods()[source]#