Skip to main content

# AutoStrEnum

Project description

AutoStrEnum

This project defines an extended Enum class.
It can automatically assign the value to your Enum member, and the value is just the same as the member name!
And when you print it, you won't see the Enum name in front of the class member.

Install

pip install AutoStrEnum

Demo

import json
import pickle
from enum import auto

from AutoStrEnum import AutoStrEnum, AutoJsonEncoder


class Fruit(AutoStrEnum):
    BANANA = auto()
    WATERMELON = auto()
    DURIAN = auto()
    KIWI = auto()


class MagicFruit(AutoStrEnum):
    BANANA = auto()
    WATERMELON = auto()
    DURIAN = auto()


if __name__ == '__main__':
    print(Fruit, MagicFruit)
    print(Fruit.BANANA, Fruit.WATERMELON, Fruit.DURIAN)

    print('should be True:', Fruit.BANANA in Fruit)
    print('should be True:', Fruit.BANANA is Fruit.BANANA)
    print('should be True:', Fruit.BANANA == Fruit.BANANA)
    print('should be True:', isinstance(Fruit.BANANA, Fruit))
    print('should be False:', isinstance(Fruit.BANANA, str))
    print('should be False:', isinstance(Fruit.BANANA, MagicFruit))
    print('should be False:', isinstance(False, Fruit))

    # We also can use as dict key!
    test_dict = {
        Fruit: {
            Fruit.BANANA: 2,
            Fruit.DURIAN: 10,
            Fruit.WATERMELON: 0,
            'Love': Fruit.KIWI
        }}

    print(test_dict)

    # json dumps is also fine!
    print('json dumps', json.dumps(test_dict, indent=4, cls=AutoJsonEncoder))
$ python demo.py
Fruit MagicFruit
BANANA WATERMELON DURIAN
should be True: True
should be True: True
should be True: True
should be True: True
should be False: False
should be False: False
should be False: False
{Fruit: {BANANA: 2, DURIAN: 10, WATERMELON: 0, 'Love': KIWI}}
json dumps {
    "Fruit": {
        "Love": "KIWI",
        "BANANA": 2,
        "DURIAN": 10,
        "WATERMELON": 0
    }
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

AutoStrEnum-0.0.10.tar.gz (3.5 kB view hashes)

Uploaded Source

Built Distribution

AutoStrEnum-0.0.10-py2.py3-none-any.whl (3.9 kB view hashes)

Uploaded Python 2 Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page