bw2data.utils#

Module Contents#

Functions#

clean_exchanges(data)

Make sure all exchange inputs are tuples, not lists.

combine_databases(name, *dbs)

Combine databases into new database called name.

combine_methods(name, *ms)

Combine LCIA methods by adding duplicate characterization factors.

create_in_memory_zipfile_from_directory(path)

download_file(filename[, directory, url])

Download a file and write it to disk in downloads directory.

get_activity(key)

merge_databases(parent_db, other)

Merge other into parent_db, including updating exchanges.

natural_sort(l)

Sort the given list in the way that humans expect, e.g. 9 before 10.

open_activity_in_webbrowser(activity)

Open a dataset document in the Brightway2 web UI. Requires bw2-web to be running.

python_2_unicode_compatible(cls)

Adaptation of function in future library which was causing recursion.

random_string([length])

Generate a random string of letters and numbers.

recursive_str_to_unicode(data[, encoding])

Convert the strings inside a (possibly nested) python data structure to unicode strings using encoding.

set_data_dir(dirpath[, permanent])

Set the Brightway2 data directory to dirpath.

uncertainify(data[, distribution, bounds_factor, ...])

Add some rough uncertainty to exchanges.

web_ui_accessible()

Test if bw2-web is running and accessible. Returns True or False.

Attributes#

DOWNLOAD_URL

MAX_INT_32

TYPE_DICTIONARY

numpy_string

bw2data.utils.clean_exchanges(data)[source]#

Make sure all exchange inputs are tuples, not lists.

bw2data.utils.combine_databases(name, *dbs)[source]#

Combine databases into new database called name.

bw2data.utils.combine_methods(name, *ms)[source]#

Combine LCIA methods by adding duplicate characterization factors.

Parameters

ms (*) – Any number of method ids, e.g. ("my method", "wow"), ("another method", "wheee").

Returns

The new Method instance.

bw2data.utils.create_in_memory_zipfile_from_directory(path)[source]#
bw2data.utils.download_file(filename, directory='downloads', url=None)[source]#

Download a file and write it to disk in downloads directory.

If url is None, uses the Brightway2 data base URL. url should everything up to the filename, such that url + filename is the valid complete URL to download from.

Streams download to reduce memory usage.

Parameters
  • filename (*) – The filename to download.

  • directory (*) – Directory to save the file. Created if it doesn’t already exist.

  • url (*) – URL where the file is located, if not the default Brightway data URL.

Returns

The path of the created file.

bw2data.utils.get_activity(key)[source]#
bw2data.utils.merge_databases(parent_db, other)[source]#

Merge other into parent_db, including updating exchanges.

All databases must be SQLite databases.

parent_db and other should be the names of databases.

Doesn’t return anything.

bw2data.utils.natural_sort(l)[source]#

Sort the given list in the way that humans expect, e.g. 9 before 10.

bw2data.utils.open_activity_in_webbrowser(activity)[source]#

Open a dataset document in the Brightway2 web UI. Requires bw2-web to be running.

activity is a dataset key, e.g. ("foo", "bar").

bw2data.utils.python_2_unicode_compatible(cls)[source]#

Adaptation of function in future library which was causing recursion.

We check and define __unicode__ only if it doesn’t exist already.

A decorator that defines __unicode__ and __str__ methods under Python 2. Under Python 3, this decorator is a no-op.

bw2data.utils.random_string(length=8)[source]#

Generate a random string of letters and numbers.

Parameters

length (*) – Length of string, default is 8

Returns

A string (not unicode)

bw2data.utils.recursive_str_to_unicode(data, encoding='utf8')[source]#

Convert the strings inside a (possibly nested) python data structure to unicode strings using encoding.

bw2data.utils.set_data_dir(dirpath, permanent=True)[source]#

Set the Brightway2 data directory to dirpath.

If permanent is True, then set dirpath as the default data directory.

Creates dirpath if needed. Also creates basic directories, and resets metadata.

bw2data.utils.uncertainify(data, distribution=None, bounds_factor=0.1, sd_factor=0.1)[source]#

Add some rough uncertainty to exchanges.

Warning

This function only changes exchanges with no uncertainty type or uncertainty type UndefinedUncertainty, and does not change production exchanges!

Can only apply normal or uniform uncertainty distributions; default is uniform. Distribution, if specified, must be a stats_array uncertainty object.

data is a LCI data dictionary.

If using the normal distribution:

  • sd_factor will be multiplied by the mean to calculate the standard deviation.

  • If no bounds are desired, set bounds_factor to None.

  • Otherwise, the bounds will be [(1 - bounds_factor) * mean, (1 + bounds_factor) * mean].

If using the uniform distribution, then the bounds are [(1 - bounds_factor) * mean, (1 + bounds_factor) * mean].

Returns the modified data.

bw2data.utils.web_ui_accessible()[source]#

Test if bw2-web is running and accessible. Returns True or False.

bw2data.utils.DOWNLOAD_URL = 'https://brightwaylca.org/data/'[source]#
bw2data.utils.MAX_INT_32 = 4294967295[source]#
bw2data.utils.TYPE_DICTIONARY[source]#
bw2data.utils.numpy_string[source]#