2

Let's say i have this enum class :

from enum import Enum


class HsvValues(Enum):
    BROWN = ('6, 63, 0', '23, 255, 81')
    GREY = ('23, 0, 0', '80, 105, 107')

In my other class i currently have this function to get the lower hsv bound and upper hsv bound :

def get_hsv_bounds(color):
    lower_hsv = ''
    upper_hsv = ''

    if (color == 'BROWN'):
        lower_hsv = list(map(int, HsvValues.BROWN.value[0].split(',')))
        upper_hsv = list(map(int, HsvValues.BROWN.value[1].split(',')))

    if (color == 'GREY'):
        lower_hsv = list(map(int, HsvValues.GREY.value[0].split(',')))
        upper_hsv = list(map(int, HsvValues.GREY.value[1].split(',')))

    return lower_hsv, upper_hsv

But i would like to be able to only call a getter method to get the lower bound value of the hsv value which is [0] and the upper bound value of the hsv which is [1]. I would like to do something like that which wouldn't require all the "if":

    lower_hsv = color.get_lower_bound()
    upper_hsv = color.get_upper_bound()

How can i do that in my HsvValues enum class ? I am really unsure on how to approach this. Thanks for your help.

2 Answers 2

2

property also works with Enum:

from enum import Enum

class HsvValues(Enum):
    BROWN = (6, 63, 0), (23, 255, 81)
    GREY = (23, 0, 0), (80, 105, 107)
    #
    @property
    def bounds(self):
        return self.value
    #
    @property
    def lower_bounds(self):
        return self.value[0]
    #
    @property
    def upper_bounds(self):
        return self.value[1]

I'm assuming the values don't actually have to be strings, but if they do you can adjust the *bounds methods to work with strings.

In use:

>>> print(HsvValues.BROWN.bounds)
((6, 63, 0), (23, 255, 81))
Sign up to request clarification or add additional context in comments.

Comments

1

You can create a generic method on the enum:

from enum import Enum


class HsvValues(Enum):
    BROWN = ('6, 63, 0', '23, 255, 81')
    GREY = ('23, 0, 0', '80, 105, 107')

    @staticmethod
    def get_lower_bound(enum_value):
        return list(map(int, enum_value.value[0].split(',')))

    @staticmethod
    def get_upper_bound(enum_value):
        return list(map(int, enum_value.value[1].split(',')))
    

print(HsvValues.get_lower_bound(HsvValues.BROWN))
print(HsvValues.get_upper_bound(HsvValues.BROWN))
print(HsvValues.get_lower_bound(HsvValues.GREY))
print(HsvValues.get_upper_bound(HsvValues.GREY))

outputs

[6, 63, 0]
[23, 255, 81]
[23, 0, 0]
[80, 105, 107]

An alternative will be to use a custom object for the enum values, but then you will have to use .value from the calling code:

from enum import Enum


class ComplexEnumValue:
    def __init__(self, *value):
        self.value = value

    def get_lower_bound(self):
        return list(map(int, self.value[0].split(',')))

    def get_upper_bound(self):
        return list(map(int, self.value[1].split(',')))


class HsvValues(Enum):
    BROWN = ComplexEnumValue('6, 63, 0', '23, 255, 81')
    GREY = ComplexEnumValue('23, 0, 0', '80, 105, 107')


print(HsvValues.BROWN.value.get_lower_bound())

outputs

[6, 63, 0]

5 Comments

i know i accepted the answer but would there be away to also just call color.get_low_bound() ? So i wouldn't need to do the big If that i am doing. Thanks.
i edited in original post what i would like instead, i think it would be cleaner haha sorry for changing :(
@codejourney My answer does show how to achieve this without an if
yes but how to call it in my get_hsv_bounds method ? It doesn't seem to work :O Seems to always get attribute error for self.value = value also
nvm i got it to work by adding something still gonna mark it as correct answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.