REGISTER NOW: DdS Autumn School! 🇨🇭 Grosshöchstetten (Switzerland) 🗓️ 6.-11. October 2024

bw2io.package#

Module Contents#

Classes#

BW2Package

This is a format for saving objects which implement the datastore API.

Functions#

download_biosphere()

download_methods()

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
}

Warnings#

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")

APPROVED[source]#
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#
objobject

Object to export.

filenamestr, optional

Name of file to create. Default is obj.name.

folderstr, optional

Folder to create file in. Default is export.

backwards_compatiblebool, optional

Create package compatible with bw2data version 1.

Returns#
str

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#
objslist

List of objects to export.

filenamestr

Name of file to create.

folderstr, optional

Folder to create file in. Default is export.

backwards_compatiblebool, optional

Create package compatible with bw2data version 1.

Returns#
str

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#
filepathstr

Path of file to import

whitelistbool

Apply whitelist to allowed types. Default is True.

Returns#
object or list of objects

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#
filepathstr

Path of file to import

whitelistbool

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).

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