klamt-lab / straindesign

StrainDesign is a python package for the computational design of metabolic networks and based on COBRApy
Apache License 2.0
33 stars 6 forks source link

Handle unknown solvers gracefully #5

Closed carrascomj closed 1 year ago

carrascomj commented 1 year ago

Hello, this is a tiny fix that I found while playing with this great package.

Description

Selecting an unknown/uninstalled solver fails at indexing of available solvers. This is because avail_solvers is represented as a set, which do not implements __getitem__.

Example Traceback from pytest:

       # first try to use selected solver                                                   
        if solver:                                                                           
            if solver in avail_solvers:                                                      
                return solver                                                                
            else:                                                                            
>               logging.warning('Selected solver ' + solver + ' not available. Using ' + avai
l_solvers[0] + " instead.")                                                                  
E               TypeError: 'set' object is not subscriptable                                 

../.virtualenvs/isofba/lib/python3.9/site-packages/straindesign/lptools.py:83: TypeError

Fix implementation

This PR converts avail_solvers into a list previous to the index operation. Additionally, a test was added to check that asking for a non-existing solver selects an existing one properly without raising.

Note that sets are unordered. Usually, you'd get the same solver by converting first to a list; but that is not guaranteed, it may change with the python implementation. Alternatively, one could just raise if the solver is not available.

VonAlphaBisZulu commented 1 year ago

Thank you so much for this PR. Excellent documentation, and thanks also for adding the test. CI-test looks good on all platforms and versions! Merged.

Happy that you like the package!