Skip to main content

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

Metadata

Release files for AutoStrEnum 0.0.10

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for AutoStrEnum 0.0.10
File Size Uploaded
AutoStrEnum-0.0.10.tar.gz 3.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for AutoStrEnum 0.0.10
File Interpreter ABI Platform
AutoStrEnum-0.0.10-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 7.4 kB

Release files / AutoStrEnum-0.0.10.tar.gz

Download URL AutoStrEnum-0.0.10.tar.gz
Size 3.5 kB
Tags Source
SHA-256 checksum
How to use checksums
f4f0df05562c9da78a23989c3a772f8c985edd912940b90a323a7928f707b847
BLAKE2b-256 checksum
How to use checksums
edd4e6d63d1af0e9e52b2579736c2b305e0d4022ad0c0d5219641589573a5555
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.8.10

Release files / AutoStrEnum-0.0.10-py2.py3-none-any.whl

Download URL AutoStrEnum-0.0.10-py2.py3-none-any.whl
Size 3.9 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
06f586d7858b82ad9bfb9e4ea81619ba523210de3ef73f4120d5ac93a8fa9d0d
BLAKE2b-256 checksum
How to use checksums
5d907e72316da1485832e0c3c9a804f29e7f954b33b8739c5bcac707e01c9c68
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.8.10

Release history Release notifications | RSS feed

This release

0.0.10 This release

2 release files

0.0.9

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page