jeudi 11 avril 2019

Strategy pattern with different parameter names

I have class that looks like this:

class RandomNoise(BaseOperation):

    SALT_AND_PEPPER = "salt_and_pepper"
    GAUSSIAN = "gaussian"
    POISSON = "poisson"
    UNIFORM = "uniform"

    def apply(self, image, method=GAUSSIAN, modifier1, modifier2=None):
        """
        :param image: The image to process
        :param method: How to apply noise?
            Options: gaussian, poisson, uniform, or salt_and_pepper
        :return: The augmented image
        """

        image = self.load_image(image)
        numeric_image = np.array(image)

        if method == self.GAUSSIAN:
            return self.add_gaussian_noise(numeric_image, mean=modifier1, std=modifier2)
        elif method == self.SALT_AND_PEPPER:
            return self.add_salt_and_pepper(numeric_image, amount=modifier1, salt_pepper_ratio=modifier2)
        elif method == self.POISSON:
            return self.add_poisson_noise(numeric_image, lambda_value=modifier1)
        elif method == self.UNIFORM:
            return self.add_uniform_noise(numeric_image, low=modifier1, high=modifier2)
        else:
            raise ValueError("Unknown method specified")

Works great, but I really don't want to call my parameters modifier1 and modifier2. What would be the best practice in this case?

Was thinking of adding some setters with the right names, but I wonder if I am over-complicating things.

Aucun commentaire:

Enregistrer un commentaire