AutoStrEnum


NameAutoStrEnum JSON
Version 0.0.10 PyPI version JSON
download
home_pagehttps://github.com/PttCodingMan/AutoStrEnum
Summary# AutoStrEnum
upload_time2022-09-04 09:07:37
maintainer
docs_urlNone
authorCodingMan
requires_python>=3.8
license
keywords enum
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 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

```shell
pip install AutoStrEnum
```

## Demo

```python
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))
```

```shell
$ 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
    }
}
```



            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/PttCodingMan/AutoStrEnum",
    "name": "AutoStrEnum",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": "",
    "keywords": "enum",
    "author": "CodingMan",
    "author_email": "pttcodingman@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/ed/d4/e6d63d1af0e9e52b2579736c2b305e0d4022ad0c0d5219641589573a5555/AutoStrEnum-0.0.10.tar.gz",
    "platform": null,
    "description": "# AutoStrEnum\n\nThis project defines an extended `Enum` class.  \nIt can automatically assign the value to your Enum member, and the value is just the same as the member name!  \nAnd when you print it, you won't see the Enum name in front of the class member.\n\n## Install\n\n```shell\npip install AutoStrEnum\n```\n\n## Demo\n\n```python\nimport json\nimport pickle\nfrom enum import auto\n\nfrom AutoStrEnum import AutoStrEnum, AutoJsonEncoder\n\n\nclass Fruit(AutoStrEnum):\n    BANANA = auto()\n    WATERMELON = auto()\n    DURIAN = auto()\n    KIWI = auto()\n\n\nclass MagicFruit(AutoStrEnum):\n    BANANA = auto()\n    WATERMELON = auto()\n    DURIAN = auto()\n\n\nif __name__ == '__main__':\n    print(Fruit, MagicFruit)\n    print(Fruit.BANANA, Fruit.WATERMELON, Fruit.DURIAN)\n\n    print('should be True:', Fruit.BANANA in Fruit)\n    print('should be True:', Fruit.BANANA is Fruit.BANANA)\n    print('should be True:', Fruit.BANANA == Fruit.BANANA)\n    print('should be True:', isinstance(Fruit.BANANA, Fruit))\n    print('should be False:', isinstance(Fruit.BANANA, str))\n    print('should be False:', isinstance(Fruit.BANANA, MagicFruit))\n    print('should be False:', isinstance(False, Fruit))\n\n    # We also can use as dict key!\n    test_dict = {\n        Fruit: {\n            Fruit.BANANA: 2,\n            Fruit.DURIAN: 10,\n            Fruit.WATERMELON: 0,\n            'Love': Fruit.KIWI\n        }}\n\n    print(test_dict)\n\n    # json dumps is also fine!\n    print('json dumps', json.dumps(test_dict, indent=4, cls=AutoJsonEncoder))\n```\n\n```shell\n$ python demo.py\nFruit MagicFruit\nBANANA WATERMELON DURIAN\nshould be True: True\nshould be True: True\nshould be True: True\nshould be True: True\nshould be False: False\nshould be False: False\nshould be False: False\n{Fruit: {BANANA: 2, DURIAN: 10, WATERMELON: 0, 'Love': KIWI}}\njson dumps {\n    \"Fruit\": {\n        \"Love\": \"KIWI\",\n        \"BANANA\": 2,\n        \"DURIAN\": 10,\n        \"WATERMELON\": 0\n    }\n}\n```\n\n\n",
    "bugtrack_url": null,
    "license": "",
    "summary": "# AutoStrEnum",
    "version": "0.0.10",
    "split_keywords": [
        "enum"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "md5": "e14150a98bd7ac4bbc9b24cc14fad629",
                "sha256": "06f586d7858b82ad9bfb9e4ea81619ba523210de3ef73f4120d5ac93a8fa9d0d"
            },
            "downloads": -1,
            "filename": "AutoStrEnum-0.0.10-py2.py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "e14150a98bd7ac4bbc9b24cc14fad629",
            "packagetype": "bdist_wheel",
            "python_version": "py2.py3",
            "requires_python": ">=3.8",
            "size": 3903,
            "upload_time": "2022-09-04T09:07:36",
            "upload_time_iso_8601": "2022-09-04T09:07:36.080969Z",
            "url": "https://files.pythonhosted.org/packages/5d/90/7e72316da1485832e0c3c9a804f29e7f954b33b8739c5bcac707e01c9c68/AutoStrEnum-0.0.10-py2.py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "md5": "a79ea2adfa46b254cdb3c4d2d1ceb64a",
                "sha256": "f4f0df05562c9da78a23989c3a772f8c985edd912940b90a323a7928f707b847"
            },
            "downloads": -1,
            "filename": "AutoStrEnum-0.0.10.tar.gz",
            "has_sig": false,
            "md5_digest": "a79ea2adfa46b254cdb3c4d2d1ceb64a",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 3517,
            "upload_time": "2022-09-04T09:07:37",
            "upload_time_iso_8601": "2022-09-04T09:07:37.901530Z",
            "url": "https://files.pythonhosted.org/packages/ed/d4/e6d63d1af0e9e52b2579736c2b305e0d4022ad0c0d5219641589573a5555/AutoStrEnum-0.0.10.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2022-09-04 09:07:37",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "PttCodingMan",
    "github_project": "AutoStrEnum",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [],
    "lcname": "autostrenum"
}
        
Elapsed time: 0.05661s