This notebook shows you have to work with Database classes.

In [1]:
from brightway2 import *

Create a project for this notebook:

In [2]:
projects.set_current("databases demo")

Before going any further, please read about what a database is in Brightway2.

An example database

One of the nicest ideas in Brightway2 is that data is just data, and not hidden behind a database and object-relational mapping (ORM). This makes it very easy to do data manipulation, and to add new fields to datasets.

Let's define an example database:

In [3]:
db = Database("example")

example_data = {
    ("example", "A"): {
        "name": "A",
        "exchanges": [{
            "amount": 1.0,
            "input": ("example", "B"),
            "type": "technosphere"
        'unit': 'kilogram',
        'location': 'here',
        'categories': ("very", "interesting")
    ("example", "B"): {
        "name": "B",
        "exchanges": [],
        'unit': 'microgram',
        'location': 'there',
        'categories': ('quite', 'boring')

This is quite a simple example - two activities, one of whom has no inputs. In fact, this example dataset has only a few fields. Actually, there are no required fields* in datasets, only some suggested ones, and general guidelines on how to use those suggested fields. It's like not wearing underwear - Brightway2 gives you the freedom to do it, but most people you are interacting with would prefer that you didn't.

* If you are using Activity proxies, then the name field is required.

Let's talk a bit about the fields in example_data:

  • name: This one is pretty easy :)
  • exchanges: This is a list of inputs and outputs, like how much energy or material is needed for this dataset's production. Every exchange is an intput, unless it has the value type = "production". A production exchange defines how much of the output is produced. Most of the time, you can ignore this, as the default value is one - this is what we do in the example data. However, sometimes it is useful to define a dataset that produces more.

See also: What happens with a non-unitary production amount in LCA?.

Writing database data

We need to write our example data to the database.

In [4]:
Writing activities to SQLite3 database:
0%  100%
[##] | ETA[sec]: 0.000 
Total time elapsed: 0.017 sec
Title: Writing activities to SQLite3 database:
  Started: 07/15/2016 11:53:39
  Finished: 07/15/2016 11:53:39
  Total time elapsed: 0.017 sec
  CPU %: 114.000000
  Memory %: 0.350535

Get a random activity

If you just need an example dataset activity, use the .random() method:

In [5]:
'A' (kilogram, here, ('very', 'interesting'))

Examining database data

We can also loop over our database, and try to get some useful information. For example, say we were interested in the number of exchanges in each activity:

In [6]:
num_exchanges = [(activity, len(activity.exchanges())) for activity in db]
[('B' (microgram, there, ('quite', 'boring')), 0),
 ('A' (kilogram, here, ('very', 'interesting')), 1)]

Searching databases

In [7]:"*")
['B' (microgram, there, ('quite', 'boring')),
 'A' (kilogram, here, ('very', 'interesting'))]

Deleting databases

You delete databases by deleting them from the databases object:

In [8]:
del databases[]