SynBioDex / pySBOL3

Native python implementation of SBOL 3.0 specification
MIT License
37 stars 16 forks source link

What is the recommended way to concatenate list-valued properties? #206

Closed jakebeal closed 3 years ago

jakebeal commented 3 years ago

I'm collecting together the values from some list-valued properties, and it seems awkward that we can't add together ReferencedObjectLists. Is there a recommended way to convert them into normal lists, for such purposes?

Minimal example, with both error and (ugly) workaround:

import sbol3

doc = sbol3.Document()
sbol3.set_namespace('https://example.org')

c = sbol3.Collection("foo")
error_list = [] + c.members        # TypeError: can only concatenate list (not "ReferencedObjectList") to list
non_error = [] + [m for m in c.members]  # works, but ugly
tcmitchell commented 3 years ago

If I were to define a list, as your example does, I would probably:

error_list = list(c.members)

If I were to add elements from a list of referenced objects to an existing list I would probably:

error_list = []
error_list.extend(c.members)

You can do it your way if you do it like this:

error_list = [] + list(c.members)

But I don't know why you'd use concatenation with an empty list to set a variable. I would instead use my first example above.

jakebeal commented 3 years ago

Ah, thank you; in my python naivete I didn't realize that list(x) was a conversion rather than equivalent to [x].

To the question of empty list... that's just the minimal error example; in practice, I'm actually collecting together lists based on several different properties in the process of expanding a CombinatorialDerivation.