audeering / audformat

Format to store media files and annotations
https://audeering.github.io/audformat/
Other
11 stars 1 forks source link

Iterating over db.tables can raise RuntimeError error #65

Closed hagenw closed 3 years ago

hagenw commented 3 years ago

Try the following:

import audformat.testing

db = audformat.testing.create_db()
for table in db.tables:
    db[f'{table}.new'] = audformat.Table()

This results in:

RuntimeError                              Traceback (most recent call last)
<ipython-input-4-a72911a3944b> in <module>
----> 1 for table in db.tables:
      2     db[f'{table}.new'] = audformat.Table()
      3 

RuntimeError: OrderedDict mutated during iteration

A work around is to first get a list of tables:

import audformat.testing

db = audformat.testing.create_db()
tables = list(db.tables)
for table in tables:
    db[f'{table}.new'] = audformat.Table()

This works as expected.

As this is very hard to debug for a user, I think we should try to fix it to also work when directly iterating through db.tables.

I haven't tested it for other entries, but I guess it might be the same for db.schemes etc.

frankenjoe commented 3 years ago

Yes, you should never change an object that you iterate over in Python. Copying (e.g. to a list as you do here) is best practise in such cases. So I would not call this a bug, it's how Python is designed.

frankenjoe commented 3 years ago

What it makes it a bit hard to spot here is that we usually use db[...] notation to access tables. If you rewrite your example to:

db = audformat.testing.create_db()
for table in db.tables:
    db.tables[f'{table}.new'] = audformat.Table()

it's more obvious of course.

hagenw commented 3 years ago

What it makes it a bit hard to spot here is that we usually use db[...] notation to access tables. If you rewrite your example to:

db = audformat.testing.create_db()
for table in db.tables:
    db.tables[f'{table}.new'] = audformat.Table()

it's more obvious of course.

That's true. I know there would be a reason to not use short cut notation for tables :)

But otherwise it is very convenient of course.

So, I agree that this is not a bug and will close this.