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

Uploaded Source

Built Distribution

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

Uploaded Python 2 Python 3

File details

Details for the file AutoStrEnum-0.0.10.tar.gz.

File metadata

  • Download URL: AutoStrEnum-0.0.10.tar.gz
  • Upload date:
  • Size: 3.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.10

File hashes

Hashes for AutoStrEnum-0.0.10.tar.gz
Algorithm Hash digest
SHA256 f4f0df05562c9da78a23989c3a772f8c985edd912940b90a323a7928f707b847
MD5 a79ea2adfa46b254cdb3c4d2d1ceb64a
BLAKE2b-256 edd4e6d63d1af0e9e52b2579736c2b305e0d4022ad0c0d5219641589573a5555

See more details on using hashes here.

File details

Details for the file AutoStrEnum-0.0.10-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for AutoStrEnum-0.0.10-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 06f586d7858b82ad9bfb9e4ea81619ba523210de3ef73f4120d5ac93a8fa9d0d
MD5 e14150a98bd7ac4bbc9b24cc14fad629
BLAKE2b-256 5d907e72316da1485832e0c3c9a804f29e7f954b33b8739c5bcac707e01c9c68

See more details on using hashes here.

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