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?