Skip to content

Joel-Edem/immutable-py-enum

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Immutable Python Enums

A cursed implementation of enums in python.

Usage

import Enum

COLOR = Enum("RED", "BLUE", "GREEN", PINK=99)

colors = [COLOR.RED, COLOR.BLUE, COLOR.GREEN, COLOR.PINK]
for color in colors:
    match color:
        case COLOR.RED:
            print("Matched RED color")
        case COLOR.BLUE:
            print(f"Matched {COLOR[color]} color")
        case _:
            print(f"Sorry, couldn't match {COLOR(color)}")

Output

Matched RED color
Matched BLUE color
Sorry, couldn't match GREEN

Using Enums

With enums you can easily use the . or [] operator like classes or dicts, with the benefit of immutability and much faster lookups.

Creating

COLOR =  Enum("RED", "BLUE", "GREEN")
SHAPES = Enum(SQUARE=5, RECTANGLE=6, CIRCLE=9)
FRUITS = Enum("ORANGE", "BANANA", MANGO=2, LEMON=0)

READING LABELS

# by calling        PRINTS
print(FRUITS(2))   #   'MANGO'
print(SHAPES(9))   #   'CIRCLE'

# by index
print(FRUITS[0])    #  'LEMON'
print(SHAPES[9])    #   'CIRCLE'

READING VALUES

# BY INDEX
print(SHAPES['SQUARE'])  # 5
print(FRUITS['MANGO'])   # 2
# BY CALLING
print( FRUITS('MANGO'))  # 2    
print( FRUITS('LEMON'))  # 0
# BY ATTRIBUTE
print(FRUITS.BANANA)     # 4
print(COLOR.BLUE)        # 1

About

Immutable enums in python

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages