Monday 26 June 2023

automatically create Python class factory from __init__.py

I have this code that assigns the class name in a dict to the class. I've been adding to feature_expanded_factory manually and find this is inefficient especially if the class name change or a class is added.

Instead I'd like to create feature_expander_factory from the __init__.py below. So it should take every class from the __init__.py file then create a dict where the class name is assigned the to the class.

from data_processing.feature_expanders import CategoricalToOneHot, RFMSplitter, RFMSplitterAndOneHot, \
    StrToListToColumns

feature_expander_factory = dict(CategoricalToOneHot=CategoricalToOneHot, RFMSplitter=RFMSplitter,
                                RFMSplitterAndOneHot=RFMSplitterAndOneHot, ListToColumns=StrToListToColumns)

__init__.py

from data_processing.feature_expanders.AbstractFeatureExpander import AbstractFeatureExpander
from data_processing.feature_expanders.CategoricalToOneHot import CategoricalToOneHot
from data_processing.feature_expanders.RFMSplitter import RFMSplitter
from data_processing.feature_expanders.RFMSplitterAndOneHot import RFMSplitterAndOneHot
from data_processing.feature_expanders.StrToListToColumns import StrToListToColumns


from automatically create Python class factory from __init__.py

No comments:

Post a Comment