codejourney
codejourney

Reputation: 319

How to create a getter for an enum python

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.

Upvotes: 2

Views: 1335

Answers (2)

Ethan Furman
Ethan Furman

Reputation: 69288

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))

Upvotes: 2

DeepSpace
DeepSpace

Reputation: 81684

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]

Upvotes: 0

Related Questions