Q:

Can't write copy of activity in newly created databases

I am trying to write a copy of an activity from an existing database (in this case ecoinvent 3.2 cut-off, but that shouldn't matter) into a new database. I have:
In:  ei = bw.Database('ecoinvent 3.2 cutoff')
     testDB = bw.Database('testdb')
     testDB.register()
     bw.databases
Out: Brightway2 databases metadata with 3 objects:
         biosphere3
         ecoinvent 3.2 cutoff
         testdb

In:  act = ei.random()
     actCopy = act.copy()
     testDB.write(actCopy)
Out: ---------------------------------------------------------------------------
     WrongDatabase                             Traceback (most recent call last)
     <ipython-input-20-831953ab6cb9> in <module>()
     ----> 1 testdb.write(act)
     [...]
     WrongDatabase: Can't write activities in databases {'a', 'd', 'u', 'c', 'r', 'n', 'f', 'l', 't', 'p'} to database testdb

What is the meaning of the single letter strings in the set in the error message?
What part of my code makes the writing to my new database break down?

A:

Calling Database('foo').write(data) requires that data is a dictionary, see the technical documentation. In this case, actCopy is an activity proxy, so the easiest solution would be to use actCopy.save(). Alternatively, you could use the following utility function to get a list of activities into the correct form:
def format_me(list_of_activities, database_obj):
    return {(database_obj.name, obj['code']): obj for obj in list_of_activities}

The reason you get the error message "Can't write activities in databases {'a', 'd', 'u', 'c', 'r', 'n', 'f', 'l', 't', 'p'} to database testdb" is that you are passing a list, and one of the things the write function does is to make sure that you are not trying to write data to the wrong database. The way it does this is to iterate over the keys in the dictionary data, and take the first element as the database name. It then checks to make sure that these names are same as the database which is being written to. When you passed a data object like this:
{
    'alpha': 'something',
    'delta': 'something',
    'umberto': 'something',
    'california': 'something',
    ...
}

Then, when iterating over the object you passed into data, it would get these database keys, i.e. alpha, delta, etc. It then takes the first element (a, d, etc.) and checks to make sure the database name is correct
Here is the actual bw2data code that raises this error:
wrong_database = {key[0] for key in data}.difference({self.name})
if wrong_database:
    raise WrongDatabase("Can't write activities in databases {} to database {}".format(
                        wrong_database, self.name))

