bw2data.query#

Attributes#

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)

Module Contents#

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]#
dicts = ()[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")

Parameters:
  • key (*) – The field to filter on.

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

  • value (*) – The value to test against.

Returns:

A Result object which wraps a new data dictionary.

function[source]#
key[source]#
value[source]#
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.

Parameters:

filters (*) – One or more Filter objects.

add(filter_)[source]#

Add another filter.

Parameters:

*filter_* (Filter) – A Filter object.

filters = [][source]#
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.

Parameters:

result (*) – The filtered dataset.

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

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

Parameters:
  • field (*) – The key used for sorting.

  • reverse (*) – Reverse normal sorting order.

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