rcw-2 / python-digikamdb

Python library to access Digikam metadata
MIT License
11 stars 0 forks source link

How to know whether a tag has children? #5

Closed rokdd closed 6 months ago

rokdd commented 6 months ago

Thank you for your development it is really great to work with your library. I was only struggeling a bit how to find out whether a tag has children or not:

if len(dk.tags[tag.id].children)>0:

This throws an error because it returns a Query Object. So my workaround was at the end:

if len(list(dk.tags[tag.id].children))>0:

Also I was suprised that when I put the object as the key that does not use the id itself to find the tag in dk.tags (but this is solved)

So what is correct was to find out whether a tag has children (or the len)?

rcw-2 commented 6 months ago
if len(list(dk.tags[tag.id].children))>0:

You could also just use if list(...):

Also I was suprised that when I put the object as the key that does not use the id itself to find the tag in dk.tags (but this is solved)

Why not just use tag instead of dk.tags[tag.id]?

So what is correct was to find out whether a tag has children (or the len)?

I haven't really thought about that before. Your approach (convert to list) is probably the most robust since it will work as long as children is iterable. You could also use the methods of Query (all(), count() etc.), but this is an undocumented feature now. SQLAlchemy calls this API legacy, but they say most of it will not be removed, so I think I can still use it for some time...

rokdd commented 6 months ago

With count() is a good idea I will try it next time.