0

What is a good way to create a mechanism to control which features to use in a Python application? My idea is to have like a list of various implementations of an abstract class. All the specific features implement the same methods in different ways. For example:

import abc
from typing import List


class BasicFeature:
    @abc.abstractmethod
    def run_calculation(self, data: List[float]) -> List[float]:
        pass


class FirstFeature(BasicFeature):
    def __init__(self, operand: int):
        BasicFeature.__init__(self)
        self.operand = operand

    def run_calculation(self, data: List[float]) -> List[float]:
        result=[]
        for number in data:
            result.append(number*self.operand)

class SecondFeature(BasicFeature):
    def __init__(self, operand: int):
        BasicFeature.__init__(self)
        self.operand = operand

    def run_calculation(self, data: List[float]) -> List[float]:
        result=[]
        for number in data:
            result.append(number/self.operand)

class ThirdFeature(BasicFeature):
    def __init__(self, operand: int):
        BasicFeature.__init__(self)
        self.operand = operand
        
    def run_calculation(self, data: List[float]) -> List[float]:
        result=[]
        for number in data:
            result.append(number**self.operand)

I want to have a way to control which of these features I will use each time through a config file. I was thinking is there a way to create something like a dictionary maybe with all the feature class names as keys and add a True/False value if they are meant to be added to the active feature list? Something like:

# Dictionary with key value pairs where keys are my class name and
# value indicates if feature should be added to active features
features = {FirstFeature(): True, SecondFeature(): False, ThirdFeature():True} 

active_features = [key for key,value in features if value == True ]

data= [0.77,98.3,435.78,456.0,23.1]
for feature in active_features:
    data = feature.run_calculation(data)

Obviously I can't use the class names as keys and I need to find a way to pas them the initialization arguments but how can I implement a mechanism like that?

KZiovas
  • 3,491
  • 3
  • 26
  • 47
  • 1
    You can find answer in this [post](https://stackoverflow.com/questions/1176136/convert-string-to-python-class-object). So you can use a dictionary of string. – Ptit Xav Nov 17 '21 at 11:23
  • so I use a dictionary with string and the the getattr to return and initialize the objects? Cool thanks! – KZiovas Nov 17 '21 at 11:31
  • You can also go further as you can also import module with __inport__() function. This way you can have classes in different files. You just have to add the entry in dictionary without modifying code. – Ptit Xav Nov 17 '21 at 13:51
  • I am sorry I didnt get how the __import__ helps with this? You mean dynamically import module as well? I f yes in my case I dont need it I could just import all the class normally at the top with normal import, I just need to dynamically choose the one that re going to be used. But yeah if you want dynamic importing it could help – KZiovas Nov 17 '21 at 19:20
  • Yes, just for dynamic use : if you save the dictionary in a file, you can add other class without making your python program getting bigger with all the code for each class. For maintenance you can make different dictionary files to test different list of class. – Ptit Xav Nov 17 '21 at 21:54

0 Answers0