'Is there a way to copy an activity to another database?
I would like to create a copy of an activity and move it in another database.
I can see two solutions:
Option 1 - Use the method .copy() of my activity and then to change the database of the copy. The problem is that I cannot figure how to move an activity from one database to another or even if it is possible.
Option 2 - Re-create an activity in the destination database that contains the data of the activity to copy. Although it is not difficult, I find this option way less elegant. Below is an implementation of option 2.
def copy_activity(activity, target_database=None, target_name=None):
"""
Copy an activity to another database
Args:
activity: Activity to copy
target_database (str):
Name of the database in which to copy the activity default is the database of the activity
target_name (str): Name of the copy, default is the name of the activity
"""
if (target_database is None) and (target_name is None):
raise ValueError('You must specify at least a target database or a target name')
db_name = target_database or activity.key[0]
db = bw.Database(db_name)
activity_copy = db.new_activity(activity.key[1])
for attribute in activity:
activity_copy[attribute] = activity[attribute]
for exchange in activity.exchanges():
if exchange.input == exchange.output:
activity_copy.new_exchange(input=activity_copy, output=activity_copy)
else:
activity_copy.new_exchange(input=exchange.input, output=activity_copy)
activity_copy.save()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
