Brightway2: Modifying/deleting exchanges from activity without using activity as dict

后端 未结 1 1872
自闭症患者
自闭症患者 2021-01-16 17:54

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:

相关标签:
1条回答
  • 2021-01-16 18:00

    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).

    0 讨论(0)
提交回复
热议问题