mardi 21 mars 2023

How to manage python dependencies based upon user input? A question about structuring the python code

I am trying to implement the methodology described here. The details except for the ones I will post here are irrelevant to the scope of the question

  1. The intent is to implement hyperparameter tuning for lightgbm
  2. I do it the typical way - import lightgbm via import lightgbm, write a class that does it for me
  3. There is a special way to do hyperparameter optimization for lightgbm and that requires me to import lightgbm like from optuna.integration import lightgbm and the rest of the code can remain identical
  4. I want to provide the end users the capability to choose between the vanilla methodology (in point 2) or the special methodology (point 3). To implement that I have the following in the __init__ of the main class and the __init__ takes and argument use_lightgbm_heuristics=True
global lightgbm
    if use_lightgbm_heuristics:
        print("will be using heuristics")
        from optuna.integration import lightgbm
    else:
        print("will be using vanilla lgbm")
        import lightgbm

There is an import statement in the main class of my code. Can I do better?

Aucun commentaire:

Enregistrer un commentaire