bw2data.query#

Module Contents#

Classes#

Dictionaries

Pretends to be a single dictionary when applying a Query to multiple databases.

Filter

A filter on a dataset.

Query

A container for a set of filters applied to a dataset.

Result

A container that wraps a filtered dataset. Returned by a calling a Query object. A result object functions like a read-only dictionary; you can call Result[some_key], or some_key in Result, or len(Result).

Functions#

NF(value)

Shortcut for a name filter

PF(value)

Shortcut for a reference product filter

try_op(f, x, y)

Attributes#

operators

class bw2data.query.Dictionaries(*args)[source]#

Pretends to be a single dictionary when applying a Query to multiple databases.

Usage:

first_database = Database(…).load() second_database = Database(…).load() my_joined_dataset = Dictionaries(first_database, second_database) search_results = Query(filter_1, filter_2)(my_joined_dataset)

items()[source]#
class bw2data.query.Filter(key, function, value)[source]#

A filter on a dataset.

The following functions are supported:

  • “<”, “<=”, “==”, “>”, “>=”: Mathematical relations

  • “is”, “not”: Identity relations. Work on any Python object.

  • “in”, “notin”: List or string relations.

  • “iin”, “iis”, “inot”: Case-insensitive string relations.

  • “len”: Length relation.

In addition, any function which defines a relationship between an input and an output can also be used.

Examples:

  • All name values are “foo”: Filter("name", "is", "foo")

  • All name values include the string “foo”: Filter("name", "has", "foo")

  • Category (a list of categories and subcategories) includes “foo”: Filter("category", "has", "foo")

Args:
  • key (str): The field to filter on.

  • function (str or object): One of the pre-defined filters, or a callable object.

  • value (object): The value to test against.

Returns:

A Result object which wraps a new data dictionary.

class bw2data.query.Query(*filters)[source]#

A container for a set of filters applied to a dataset.

Filters are applied by calling the Query object, and passing the dataset to filter as the argument. Calling a Query with some data returns a Result object with the filtered dataset.

Args:
  • filters (filters): One or more Filter objects.

add(filter_)[source]#

Add another filter.

Args:

filter_ (Filter): A Filter object.

class bw2data.query.Result(result)[source]#

A container that wraps a filtered dataset. Returned by a calling a Query object. A result object functions like a read-only dictionary; you can call Result[some_key], or some_key in Result, or len(Result).

The dataset can also be sorted, using sort(field); the underlying data is then a collections.OrderedDict.

Args:
  • result (dict): The filtered dataset.

items()[source]#
items()[source]#
keys()[source]#
sort(field, reverse=False)[source]#

Sort the filtered dataset. Operates in place; does not return anything.

Args:
  • field (str): The key used for sorting.

  • reverse (bool, optional): Reverse normal sorting order.

bw2data.query.NF(value)[source]#

Shortcut for a name filter

bw2data.query.PF(value)[source]#

Shortcut for a reference product filter

bw2data.query.try_op(f, x, y)[source]#
bw2data.query.operators[source]#