Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call an IntEnum class from inside a dictionary

I have an example IntEnum class:

class ShapeMethod(IntEnum):
    NONE = 0
    circle = 1
    square = 2

That needs to be called by the __init__ function of another class:

class ExampleClass():
    def __init__(look_at_shapes=None):
        """
        Args:
            look_at_shapes (dict): A dictionary of shape inputs.
        """
        if look_at_shapes:
            self.shape = ShapeMethod.NONE
            if look_at_shapes["colour"]:
                self.colour = look_at_shapes["colour"]

    def do_something_with_shape:
        if self.shape == ShapeMethod.circle:
            print('awesome you got a circle'.)
        elif self.shape == ShapeMethod.square:
            print('squares have 4 sides.')
        else:
            print('nothing to see here.')

where, the self.shape attribute needs to be circle, square or NONE.

The do_something_with_shape function would then be called by:

input = {"colour" = blue}
my_object = ExampleClass(look_at_shape=input)
my_object.do_something_with_shape

The structure of input needs to be a dictionary, and it is reasonably simple to set the colour. However, I do not know how to properly use the IntEnum class from inside a dictionary. For example, if I want to print squares have 4 sides.

Note: Do all options in ShapeMethod(IntEnum) need to be capitalized?


What I have looked at so far:

The documentation for Python gives a number of examples; however, none fit my exact case.

like image 588
Wychh Avatar asked Dec 31 '22 22:12

Wychh


2 Answers

One way to use would be the following:

from enum import IntEnum


class ShapeMethod(IntEnum):

    NONE = 0
    circle = 1
    square = 2


class ExampleClass:

    def __init__(self, look_at_shapes=None):
        """
        Args:
            look_at_shapes (dict): A dictionary of shape inputs.
        """
        
        self.shape = None
        self.colour = None

        if look_at_shapes:
            if look_at_shapes[ShapeMethod]:
                self.shape = look_at_shapes[ShapeMethod]

            if look_at_shapes["colour"]:
                self.colour = look_at_shapes["colour"]


    def do_something_with_shape(self):
        if self.shape is ShapeMethod.NONE:
            print('awesome you got a circle.')
        elif self.shape is ShapeMethod.square:
            print('squares have 4 sides.')
        else:
            print('nothing to see here.')


input_var = {
    "colour": 'blue',
    ShapeMethod: ShapeMethod.square
}
my_object = ExampleClass(look_at_shapes=input_var)

my_object.do_something_with_shape()

Regarding your side question:

Note: Do all options in ShapeMethod(IntEnum) need to be capitalized?

It's entierly up to you. They can be all upper-case, which is in acordance with PEP 8 since they'll be constants. However, a lot of folks decide to capitalize only the first letter and it's a valid choice of style. The least common option is using all lower-case or mixing different capitalizations, however there is nothing stopping you from doing so. See this answer for a complete reference.

like image 72
bad_coder Avatar answered Jan 06 '23 03:01

bad_coder


input = {
        "shape": ShapeMethod.square,
        "colour": "blue",
        }


def __init__(look_at_shapes=None):
    """
    Args:
        look_at_shapes (dict): A dictionary of shape inputs.
    """
    look_at_shapes = look_at_shapes or {}
    self.shape = look_at_shapes.get("shape", ShapeMethod.NONE)
    self.colour = look_at_shapes.get("colour")  # None if "colour" doesn't exist

    def do_something_with_shape:
        if self.shape == ShapeMethod.circle:
            print('awesome you got a circle'.)
        elif self.shape == ShapeMethod.square:
            print('squares have 4 sides.')
        else:
            print('nothing to see here.')
like image 26
Ethan Furman Avatar answered Jan 06 '23 03:01

Ethan Furman