I would like to modify an activity\'s exchanges and save the activity back to the database.
It is possible to change other aspects of the activity, like its name:
Actiities and exchanges are stored in separate tables in the SQLite database, and they each have their own object. In the journey to and from the database, several translation layers are used:
However, we almost always work with Activity
or Exchange
objects. The key point here is that because activities and exchanges are two separate tables, they have to be treated separately.
To create a new exchange, use Activity.new_exchange()
:
In [1] from brightway2 import *
In [2]: act = Database("something").random()
In [3]: exc = act.new_exchange()
In [4]: type(exc)
Out[4]: bw2data.backends.peewee.proxies.Exchange
You can also specify data attributes in the new_exchange
method call:
In [5]: exc = act.new_exchange(amount=1)
In [6]: exc['amount']
Out[6]: 1
To delete an Exchange
, call Exchange.delete()
. If you are doing a lot of data manipulation, you can either execute SQL directly against the database, or write peewee queries with ActivityDataset
or ExchangeDataset
(see e.g. the queries built in the construction of an Exchanges
object).