SynBioDex / pySBOL3

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

Make SBOL objects sortable #320

Closed jakebeal closed 3 years ago

jakebeal commented 3 years ago

In order to allow SBOL objects to be sortable I have found it useful to monkey-patch the following routine:

def identity_lt(self: sbol3.Identified, other: sbol3.Identified):
    return self.identity < other.identity
sbol3.Identified.__lt__ = identity_lt

Would it be generally a good idea to implement the ordering functions for Identified?

tcmitchell commented 3 years ago

Instead of monkey patching, can you use the key argument to sorted or sort?

sorted(doc.objects, key=lambda x: x.identity)

I'm reluctant to make objects sortable by their identity because it seems an arbitrary field to choose, and it is very easy for applications to establish their own sort order using the standard Python sorting routines. I also think this makes the code that wants a sorted order more clear because it will explicitly state what the sort criteria is.

Can we make Identified objects sortable? Yes. Should we? For me the answer is no.

jakebeal commented 3 years ago

I hadn't noticed that I would give sorted an argument so compactly; let me try using that

jakebeal commented 3 years ago

Yes, that will work.