[Python] Unneeded Abstract Method

Discussão em 'Python' iniciado por Stack, Fevereiro 28, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to write some abstract classes that can calculate measurements. Measurements can have one or multiple submeasures. Submeasures have a denominator, numerator, and a stratification. So the code looks something like this:

    class Denominator(ABC):
    """
    Doc String
    """

    @abstractmethod
    def _set_populace(self) -> None:
    """
    Doc String
    """
    pass

    @abstractmethod
    def _remove_exclusions(self) -> None:
    """
    Doc String
    """
    pass

    class Submeasure(Denominator,Numerator,Stratification):
    """
    Doc String
    """
    # call denominator methods...
    # call numerator methods...
    # call stratification methods...


    and then (concrete) Measurements can look like

    class ASC(Measurement):
    """
    Doc String
    """

    def __init__(self,sub1_data:list[pd.DataFrame],sub2_data:list[pd.DataFrame] = None):
    super().__init__("ASC")
    self.__sub1__: Submeasure = _Sub_1(self.get_name() + "_sub_1",sub1_data)
    self.__sub2__: Submeasure = _Sub_2(self.get_name() + "_sub_2",sub2_data)


    Most submeasure 2s are subsets of submeasure 1s, so I can something like this do avoid doing excess calculations

    def __get_sub2_subset(self,sub1_results:dict[str:pd.DataFrame]) -> tuple[pd.DataFrame,pd.DataFrame]:
    """
    Doc String
    """
    # sub 2's populace is a subset of sub 1's numerator,
    # so all that's needed to do is filter sub 1's results where numerator == True
    populace = self.__sub1__.get_name()
    stratification = self.__sub1__.get_name() + '_stratification'

    populace = sub1_results.get(populace)
    populace = populace[populace['numerator']].copy()
    populace = populace.drop('numerator',axis=1)

    stratification = sub1_results.get(stratification)
    stratification = stratification[stratification['patient_id'].isin(populace['patient_id'])].copy()
    return populace,stratification


    and then just pass that to self.__sub2

    sub1_subset = self.__get_sub2_subset(sub1_results)
    self.__sub2__._set_sub1_subset(sub1_subset)


    But now sub2's denomiator method (remove_exclusions) doesn't need to be implemented and is just taking up space...

    @override
    def _remove_exclusions(self):
    """
    Doc String
    """
    # not sure what to do with this b/c sub2 piggy backs off of sub1's final data
    pass


    How am i supposed fix this?

    I could just make another abstraction called Exclusions but then I'd need to somehow have some Submeasure(Denominator,Exclusions,Numerator,Stratification) and some Submeasure(Denominator,Numerator,Stratification)... but that doesn't seem right to me because how do i make sure that sub1 is the first Submeasure and that sub2 is the second Submeasure? would the code be able to figure it out by me implementing the Exclusion abstract methods?

    It's been a while since I've done this type of coding so I know that this should be a common question, I just cant remember what it's called/where to look it up.

    Thanks

    Continue reading...

Compartilhe esta Página