extended-enum


Nameextended-enum JSON
Version 1.1.0 PyPI version JSON
download
home_pagehttps://github.com/ilichev-andrey/python-extended-enum
SummaryExtends the capabilities of the standard Enum.
upload_time2024-03-29 18:48:14
maintainerNone
docs_urlNone
authorIlichev Andrey
requires_python<4.0.0,>=3.8.1
licenseApache-2.0
keywords enum extended extended-enum
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # extended-enum

## Introduction

Package that expands the capabilities of the standard `Enum`.

There are times when you want to have constants that also carry additional information.
This functionality can be implemented in different ways, one of them is mapping, but there is a better approach - using `ExtendedEnum`.
`ExtendedEnum` - allows you to store dataclass as a value.
This allows you to store the value and additional information in one object and no longer need to use any auxiliary data containers.

It is important to note that the functionality of the standard `Enum` is preserved. `ExtendedEnum` is just an add-on.

## Install

Installation does not require any additional dependencies.
`ExtendedEnum` was specifically designed to be a lightweight package that uses only standard Python functionality.

```shell
pip install extended-enum
```

## Features

- You can store a value and additional information inside an `Enum` member.
  Initially, the `ValueWithDescription` class is available, which additionally stores a description.
  You can create a custom class `SomeExtendedEnumValue`.

```python
from extended_enum import ExtendedEnum, BaseExtendedEnumValue, ValueWithDescription, EnumField

class DetailedEnum(ExtendedEnum):
    CONST1 = EnumField(BaseExtendedEnumValue(value='const1'))
    CONST2 = EnumField(ValueWithDescription(value='const2', description='some description 2'))
    CONST3 = EnumField(ValueWithDescription(value='const3', description='some description 3'))
```

```python
from dataclasses import dataclass, field
from extended_enum import ExtendedEnum, BaseExtendedEnumValue, EnumField
from typing import Optional

@dataclass(frozen=True)
class SomeExtendedEnumValue(BaseExtendedEnumValue):
    display_name: str = field(compare=False)
    description: Optional[str] = field(default=None, compare=False)

class DetailedEnum(ExtendedEnum):
    CONST1 = EnumField(SomeExtendedEnumValue(value='const1', display_name='ONE'))
    CONST2 = EnumField(SomeExtendedEnumValue(value='const2', display_name='TWO', description='some description 2'))
```

- The following types can be used as internal values: str, int, uuid.UUID [[ref: SimpleValueType](extended_enum/__init__.py#L7)]

```python
from uuid import UUID
from extended_enum import ExtendedEnum, EnumField

class MixedEnum(ExtendedEnum):
    CONST1 = EnumField('const1')
    CONST2 = EnumField(2)
    CONST3 = EnumField(UUID('79ff3431-3e98-4bec-9a4c-63ede2580f83'))
```

- Additionally created attributes:
  * `extended_value` - Get the expanded value of an enumeration member.
  * `get_values` - Get a list of values of an enumeration.
  * `get_extended_values` - Get a list of values (in expanded form) of an enumeration.
  * `get_members` - Get the members of the enumeration.
  * `get_simple_value_member` - Get a mapping of enumeration members to simple values.

```pycon
>>> from extended_enum import ExtendedEnum, BaseExtendedEnumValue, ValueWithDescription, EnumField
>>> class DetailedEnum(ExtendedEnum):
...     CONST1 = EnumField(ValueWithDescription(value='const1'))
...     CONST2 = EnumField(ValueWithDescription(value='const2', description='some description 2'))
...     CONST3 = EnumField(ValueWithDescription(value='const3', description='some description 3'))
>>> DetailedEnum.CONST2.value
'const2'
>>>
>>> DetailedEnum.CONST2.extended_value
ValueWithDescription(value='const2', description='some description 2')
>>>
>>> DetailedEnum.get_values()
('const1', 'const2', 'const3')
>>>
>>> DetailedEnum.get_extended_values()
(ValueWithDescription(value='const1', description=None), 
 ValueWithDescription(value='const2', description='some description 2'), 
 ValueWithDescription(value='const3', description='some description 3'))
>>>
>>> DetailedEnum.get_members()
mappingproxy({
    'CONST1': <DetailedEnum.CONST1: ValueWithDescription(value='const1', description=None)>, 
    'CONST2': <DetailedEnum.CONST2: ValueWithDescription(value='const2', description='some description 2')>, 
    'CONST3': <DetailedEnum.CONST3: ValueWithDescription(value='const3', description='some description 3')>
})
>>> DetailedEnum.get_simple_value_member()
{
    'const1': <DetailedEnum.CONST1: ValueWithDescription(value='const1', description=None)>, 
    'const2': <DetailedEnum.CONST2: ValueWithDescription(value='const2', description='some description 2')>, 
    'const3': <DetailedEnum.CONST3: ValueWithDescription(value='const3', description='some description 3')>
}
```

- You can make unique enumerations using `enum.unique` in the same way as with a standard `Enum`.

```pycon
>>> from enum import unique
>>> from extended_enum import ExtendedEnum, EnumField
>>> @unique
... class Mistake(ExtendedEnum):
...     ONE = EnumField(1)
...     TWO = EnumField(2)
...     THREE = EnumField(2)
...     FOUR = EnumField('four')
...     FIVE = EnumField('five')
...     SIX = EnumField('four')
...     SEVEN = EnumField(UUID('1a882a33-f0e2-4b9f-a880-30db10c2c7dc'))
...     EIGHT = EnumField(UUID('1a882a33-f0e2-4b9f-a880-30db10c2c7dc'))
...     NINE = EnumField(UUID('f0602460-77fb-4980-9900-4e3f50093b78'))
... 
ValueError: duplicate values found in <enum 'Mistake'>: THREE -> TWO, SIX -> FOUR, EIGHT -> SEVEN
>>> 
>>> # Or without decorator
>>> unique(Mistake)
ValueError: duplicate values found in <enum 'Mistake'>: THREE -> TWO, SIX -> FOUR, EIGHT -> SEVEN
```

- You can make a nice display in automatic documentation, for example in `/redoc`. Below is an example for FastAPI

<img width="1433" alt="extended-enum_in_fastapi" src="https://github.com/ilichev-andrey/python-extended-enum/assets/24242890/f58640a8-7122-4f3c-8e93-f9ef96a65186">

```python
from typing import Literal

import uvicorn
from fastapi import FastAPI
from pydantic import Field, BaseModel

from extended_enum import ExtendedEnum, EnumField, ValueWithDescription
from extended_enum.tools import format_to_markdown

app = FastAPI()


class CompressedFileExtension(ExtendedEnum):
    LZ = EnumField(ValueWithDescription(
        value='.lz',
        description='Employs the Lempel–Ziv–Markov chain algorithm (LZMA)'
    ))
    IZO = EnumField(ValueWithDescription(
        value='.izo',
        description='Lossless data compression algorithm that is focused on decompression speed'
    ))
    IZMA = EnumField(ValueWithDescription(
        value='.izma',
        description='Uses a dictionary compression scheme and features a high compression ratio while still maintaining decompression speed'
    ))
    ZIP = EnumField('.zip')


FileExtension2Type = Literal[CompressedFileExtension.IZMA, CompressedFileExtension.ZIP]


class SomeRequestBody(BaseModel):
    file_path: str
    file_extension1: CompressedFileExtension = Field(
        default=CompressedFileExtension.ZIP,
        description='The following file extensions are supported.\n'
                    '{0}'.format(format_to_markdown(CompressedFileExtension))
    )
    file_extension2: FileExtension2Type = Field(
        description='The following file extensions are supported.\n'
                    '{0}'.format(format_to_markdown(FileExtension2Type))
    )


@app.get('/')
async def example(body: SomeRequestBody):
    return {}


if __name__ == '__main__':
    uvicorn.run(app=app, host='localhost', port=8000, workers=1)
```

## Usage

### Quick Start

`ExtendedEnum` is very easy to use.
Switching from standard `Enum` is not difficult.

Let's look at an example.
You have an enum that denotes file extensions declared like this:

```python
from enum import Enum

class FilenameExtension(Enum):
    LZ = '.lz'
    IZO = '.izo'
    IZMA = '.izma'
    ZIP = '.zip'
```

Let's clarify these incomprehensible character sets.
We will additionally need an `EnumField` wrapper for each value and a data 
class - `ValueWithDescription` to store additional information.
Let's add a description for 3 members, a `.zip` will be left without a description because everyone knows it.

```python
from extended_enum import ExtendedEnum, EnumField, ValueWithDescription

class CompressedFileExtension(ExtendedEnum):
    LZ = EnumField(ValueWithDescription(
        value='.lz',
        description='Employs the Lempel–Ziv–Markov chain algorithm (LZMA)'
    ))
    IZO = EnumField(ValueWithDescription(
        value='.izo',
        description='Lossless data compression algorithm that is focused '
                    'on decompression speed'
    ))
    IZMA = EnumField(ValueWithDescription(
        value='.izma',
        description='Uses a dictionary compression scheme and features '
                    'a high compression ratio while still maintaining '
                    'decompression speed'
    ))
    ZIP = EnumField('.zip')
```

That's it, we have completed the transition.
Let's see what is stored inside the class.

```pycon
>>> from pprint import pprint
>>> pprint(list(CompressedFileExtension))
[
    <CompressedFileExtension.LZ: ValueWithDescription(
        value='.lz',
        description='Employs the Lempel–Ziv–Markov chain algorithm (LZMA)'
    )>,
    <CompressedFileExtension.IZO: ValueWithDescription(
        value='.izo', 
        description='Lossless data compression algorithm that is focused on decompression speed'
    )>,
    <CompressedFileExtension.IZMA: ValueWithDescription(
        value='.izma', 
        description='Uses a dictionary compression scheme and features a high compression ratio while still maintaining decompression speed'
    )>,
    <CompressedFileExtension.ZIP: BaseExtendedEnumValue(value='.zip')>
]
```

Serializing values is also not difficult.

```pycon
>>> import json
>>> from enum import Enum
>>> from typing import Any
>>> def dump_enum(value: Any) -> str:
...    if isinstance(value, Enum):
...        return value.value
...    raise TypeError()
>>> json.dumps(
...     {
...         "file_extension1": CompressedFileExtension.IZO, 
...         "file_extension2": CompressedFileExtension.ZIP
...     },
...     default=dump_enum
... )
'{"file_extension1": ".izo", "file_extension2": ".zip"}'
>>> CompressedFileExtension('.lz')
<CompressedFileExtension.LZ: ValueWithDescription(
    value='.lz', 
    description='Employs the Lempel–Ziv–Markov chain algorithm (LZMA)'
)>
>>> CompressedFileExtension('.unknown')
ValueError: '.unknown' is not a valid CompressedFileExtension
```

Easily works with `orjson`.

```pycon
>>> import orjson
>>> orjson.dumps(
...     {
...         "file_extension1": CompressedFileExtension.IZO, 
...         "file_extension2": CompressedFileExtension.ZIP
...     }
... )
b'{"file_extension1":".izo","file_extension2":".zip"}'
```

Also works great with `Pydantic v1`.

```pycon
>>> from extended_enum import ExtendedEnum, EnumField, ValueWithDescription
>>> from pydantic import BaseModel
>>> class SomeModel(BaseModel):
...     file_path: str
...     file_extension: CompressedFileExtension
>>> obj = SomeModel(
...    file_path='/path/to/compressed_file.lz',
...    file_extension=CompressedFileExtension.LZ
...)
>>> obj.json()
'{"file_path": "/path/to/compressed_file.lz", "file_extension": ".lz"}'
>>> 
>>> SomeModel.parse_raw('{"file_path": "/path/to/compressed_file.lz", "file_extension": ".lz"}')
SomeModel(
    file_path='/path/to/compressed_file.lz', 
    file_extension=<CompressedFileExtension.LZ: ValueWithDescription(
        value='.lz', 
        description='Employs the Lempel–Ziv–Markov chain algorithm (LZMA)'
    )>
)
>>> 
>>> import orjson
>>> data = orjson.loads('{"file_path": "/path/to/compressed_file.lz", "file_extension": ".lz"}')
>>> SomeModel(**data)
SomeModel(
    file_path='/path/to/compressed_file.lz', 
    file_extension=<CompressedFileExtension.LZ: ValueWithDescription(
        value='.lz', 
        description='Employs the Lempel–Ziv–Markov chain algorithm (LZMA)'
    )>
)
```

Also works great with `Pydantic v2` with minor differences from the previous example.

```pycon
>>> obj.model_dump_json()
'{"file_path":"/path/to/compressed_file.lz","file_extension":".lz"}'
>>> 
>>> SomeModel.model_validate_json('{"file_path": "/path/to/compressed_file.lz", "file_extension": ".lz"}')
SomeModel(
    file_path='/path/to/compressed_file.lz', 
    file_extension=<CompressedFileExtension.LZ: ValueWithDescription(
        value='.lz', 
        description='Employs the Lempel–Ziv–Markov chain algorithm (LZMA)'
    )>
)
>>> 
>>> import orjson
>>> data = orjson.loads('{"file_path": "/path/to/compressed_file.lz", "file_extension": ".lz"}')
>>> SomeModel(**data)
SomeModel(
    file_path='/path/to/compressed_file.lz', 
    file_extension=<CompressedFileExtension.LZ: ValueWithDescription(
        value='.lz', 
        description='Employs the Lempel–Ziv–Markov chain algorithm (LZMA)'
    )>
)
```


## License

This project is licensed under the [Apache-2.0](https://github.com/ilichev-andrey/python-extended-enum/blob/master/LICENSE) License.


            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/ilichev-andrey/python-extended-enum",
    "name": "extended-enum",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<4.0.0,>=3.8.1",
    "maintainer_email": null,
    "keywords": "enum, extended, extended-enum",
    "author": "Ilichev Andrey",
    "author_email": "ilichev.andrey.y@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/f7/bf/2c65b61af2e4bacbc4db84f1a97d87d9fc89caf971677c6b32d5138aba7c/extended_enum-1.1.0.tar.gz",
    "platform": null,
    "description": "# extended-enum\n\n## Introduction\n\nPackage that expands the capabilities of the standard `Enum`.\n\nThere are times when you want to have constants that also carry additional information.\nThis functionality can be implemented in different ways, one of them is mapping, but there is a better approach - using `ExtendedEnum`.\n`ExtendedEnum` - allows you to store dataclass as a value.\nThis allows you to store the value and additional information in one object and no longer need to use any auxiliary data containers.\n\nIt is important to note that the functionality of the standard `Enum` is preserved. `ExtendedEnum` is just an add-on.\n\n## Install\n\nInstallation does not require any additional dependencies.\n`ExtendedEnum` was specifically designed to be a lightweight package that uses only standard Python functionality.\n\n```shell\npip install extended-enum\n```\n\n## Features\n\n- You can store a value and additional information inside an `Enum` member.\n  Initially, the `ValueWithDescription` class is available, which additionally stores a description.\n  You can create a custom class `SomeExtendedEnumValue`.\n\n```python\nfrom extended_enum import ExtendedEnum, BaseExtendedEnumValue, ValueWithDescription, EnumField\n\nclass DetailedEnum(ExtendedEnum):\n    CONST1 = EnumField(BaseExtendedEnumValue(value='const1'))\n    CONST2 = EnumField(ValueWithDescription(value='const2', description='some description 2'))\n    CONST3 = EnumField(ValueWithDescription(value='const3', description='some description 3'))\n```\n\n```python\nfrom dataclasses import dataclass, field\nfrom extended_enum import ExtendedEnum, BaseExtendedEnumValue, EnumField\nfrom typing import Optional\n\n@dataclass(frozen=True)\nclass SomeExtendedEnumValue(BaseExtendedEnumValue):\n    display_name: str = field(compare=False)\n    description: Optional[str] = field(default=None, compare=False)\n\nclass DetailedEnum(ExtendedEnum):\n    CONST1 = EnumField(SomeExtendedEnumValue(value='const1', display_name='ONE'))\n    CONST2 = EnumField(SomeExtendedEnumValue(value='const2', display_name='TWO', description='some description 2'))\n```\n\n- The following types can be used as internal values: str, int, uuid.UUID [[ref: SimpleValueType](extended_enum/__init__.py#L7)]\n\n```python\nfrom uuid import UUID\nfrom extended_enum import ExtendedEnum, EnumField\n\nclass MixedEnum(ExtendedEnum):\n    CONST1 = EnumField('const1')\n    CONST2 = EnumField(2)\n    CONST3 = EnumField(UUID('79ff3431-3e98-4bec-9a4c-63ede2580f83'))\n```\n\n- Additionally created attributes:\n  * `extended_value` - Get the expanded value of an enumeration member.\n  * `get_values` - Get a list of values of an enumeration.\n  * `get_extended_values` - Get a list of values (in expanded form) of an enumeration.\n  * `get_members` - Get the members of the enumeration.\n  * `get_simple_value_member` - Get a mapping of enumeration members to simple values.\n\n```pycon\n>>> from extended_enum import ExtendedEnum, BaseExtendedEnumValue, ValueWithDescription, EnumField\n>>> class DetailedEnum(ExtendedEnum):\n...     CONST1 = EnumField(ValueWithDescription(value='const1'))\n...     CONST2 = EnumField(ValueWithDescription(value='const2', description='some description 2'))\n...     CONST3 = EnumField(ValueWithDescription(value='const3', description='some description 3'))\n>>> DetailedEnum.CONST2.value\n'const2'\n>>>\n>>> DetailedEnum.CONST2.extended_value\nValueWithDescription(value='const2', description='some description 2')\n>>>\n>>> DetailedEnum.get_values()\n('const1', 'const2', 'const3')\n>>>\n>>> DetailedEnum.get_extended_values()\n(ValueWithDescription(value='const1', description=None), \n ValueWithDescription(value='const2', description='some description 2'), \n ValueWithDescription(value='const3', description='some description 3'))\n>>>\n>>> DetailedEnum.get_members()\nmappingproxy({\n    'CONST1': <DetailedEnum.CONST1: ValueWithDescription(value='const1', description=None)>, \n    'CONST2': <DetailedEnum.CONST2: ValueWithDescription(value='const2', description='some description 2')>, \n    'CONST3': <DetailedEnum.CONST3: ValueWithDescription(value='const3', description='some description 3')>\n})\n>>> DetailedEnum.get_simple_value_member()\n{\n    'const1': <DetailedEnum.CONST1: ValueWithDescription(value='const1', description=None)>, \n    'const2': <DetailedEnum.CONST2: ValueWithDescription(value='const2', description='some description 2')>, \n    'const3': <DetailedEnum.CONST3: ValueWithDescription(value='const3', description='some description 3')>\n}\n```\n\n- You can make unique enumerations using `enum.unique` in the same way as with a standard `Enum`.\n\n```pycon\n>>> from enum import unique\n>>> from extended_enum import ExtendedEnum, EnumField\n>>> @unique\n... class Mistake(ExtendedEnum):\n...     ONE = EnumField(1)\n...     TWO = EnumField(2)\n...     THREE = EnumField(2)\n...     FOUR = EnumField('four')\n...     FIVE = EnumField('five')\n...     SIX = EnumField('four')\n...     SEVEN = EnumField(UUID('1a882a33-f0e2-4b9f-a880-30db10c2c7dc'))\n...     EIGHT = EnumField(UUID('1a882a33-f0e2-4b9f-a880-30db10c2c7dc'))\n...     NINE = EnumField(UUID('f0602460-77fb-4980-9900-4e3f50093b78'))\n... \nValueError: duplicate values found in <enum 'Mistake'>: THREE -> TWO, SIX -> FOUR, EIGHT -> SEVEN\n>>> \n>>> # Or without decorator\n>>> unique(Mistake)\nValueError: duplicate values found in <enum 'Mistake'>: THREE -> TWO, SIX -> FOUR, EIGHT -> SEVEN\n```\n\n- You can make a nice display in automatic documentation, for example in `/redoc`. Below is an example for FastAPI\n\n<img width=\"1433\" alt=\"extended-enum_in_fastapi\" src=\"https://github.com/ilichev-andrey/python-extended-enum/assets/24242890/f58640a8-7122-4f3c-8e93-f9ef96a65186\">\n\n```python\nfrom typing import Literal\n\nimport uvicorn\nfrom fastapi import FastAPI\nfrom pydantic import Field, BaseModel\n\nfrom extended_enum import ExtendedEnum, EnumField, ValueWithDescription\nfrom extended_enum.tools import format_to_markdown\n\napp = FastAPI()\n\n\nclass CompressedFileExtension(ExtendedEnum):\n    LZ = EnumField(ValueWithDescription(\n        value='.lz',\n        description='Employs the Lempel\u2013Ziv\u2013Markov chain algorithm (LZMA)'\n    ))\n    IZO = EnumField(ValueWithDescription(\n        value='.izo',\n        description='Lossless data compression algorithm that is focused on decompression speed'\n    ))\n    IZMA = EnumField(ValueWithDescription(\n        value='.izma',\n        description='Uses a dictionary compression scheme and features a high compression ratio while still maintaining decompression speed'\n    ))\n    ZIP = EnumField('.zip')\n\n\nFileExtension2Type = Literal[CompressedFileExtension.IZMA, CompressedFileExtension.ZIP]\n\n\nclass SomeRequestBody(BaseModel):\n    file_path: str\n    file_extension1: CompressedFileExtension = Field(\n        default=CompressedFileExtension.ZIP,\n        description='The following file extensions are supported.\\n'\n                    '{0}'.format(format_to_markdown(CompressedFileExtension))\n    )\n    file_extension2: FileExtension2Type = Field(\n        description='The following file extensions are supported.\\n'\n                    '{0}'.format(format_to_markdown(FileExtension2Type))\n    )\n\n\n@app.get('/')\nasync def example(body: SomeRequestBody):\n    return {}\n\n\nif __name__ == '__main__':\n    uvicorn.run(app=app, host='localhost', port=8000, workers=1)\n```\n\n## Usage\n\n### Quick Start\n\n`ExtendedEnum` is very easy to use.\nSwitching from standard `Enum` is not difficult.\n\nLet's look at an example.\nYou have an enum that denotes file extensions declared like this:\n\n```python\nfrom enum import Enum\n\nclass FilenameExtension(Enum):\n    LZ = '.lz'\n    IZO = '.izo'\n    IZMA = '.izma'\n    ZIP = '.zip'\n```\n\nLet's clarify these incomprehensible character sets.\nWe will additionally need an `EnumField` wrapper for each value and a data \nclass - `ValueWithDescription` to store additional information.\nLet's add a description for 3 members, a `.zip` will be left without a description because everyone knows it.\n\n```python\nfrom extended_enum import ExtendedEnum, EnumField, ValueWithDescription\n\nclass CompressedFileExtension(ExtendedEnum):\n    LZ = EnumField(ValueWithDescription(\n        value='.lz',\n        description='Employs the Lempel\u2013Ziv\u2013Markov chain algorithm (LZMA)'\n    ))\n    IZO = EnumField(ValueWithDescription(\n        value='.izo',\n        description='Lossless data compression algorithm that is focused '\n                    'on decompression speed'\n    ))\n    IZMA = EnumField(ValueWithDescription(\n        value='.izma',\n        description='Uses a dictionary compression scheme and features '\n                    'a high compression ratio while still maintaining '\n                    'decompression speed'\n    ))\n    ZIP = EnumField('.zip')\n```\n\nThat's it, we have completed the transition.\nLet's see what is stored inside the class.\n\n```pycon\n>>> from pprint import pprint\n>>> pprint(list(CompressedFileExtension))\n[\n    <CompressedFileExtension.LZ: ValueWithDescription(\n        value='.lz',\n        description='Employs the Lempel\u2013Ziv\u2013Markov chain algorithm (LZMA)'\n    )>,\n    <CompressedFileExtension.IZO: ValueWithDescription(\n        value='.izo', \n        description='Lossless data compression algorithm that is focused on decompression speed'\n    )>,\n    <CompressedFileExtension.IZMA: ValueWithDescription(\n        value='.izma', \n        description='Uses a dictionary compression scheme and features a high compression ratio while still maintaining decompression speed'\n    )>,\n    <CompressedFileExtension.ZIP: BaseExtendedEnumValue(value='.zip')>\n]\n```\n\nSerializing values is also not difficult.\n\n```pycon\n>>> import json\n>>> from enum import Enum\n>>> from typing import Any\n>>> def dump_enum(value: Any) -> str:\n...    if isinstance(value, Enum):\n...        return value.value\n...    raise TypeError()\n>>> json.dumps(\n...     {\n...         \"file_extension1\": CompressedFileExtension.IZO, \n...         \"file_extension2\": CompressedFileExtension.ZIP\n...     },\n...     default=dump_enum\n... )\n'{\"file_extension1\": \".izo\", \"file_extension2\": \".zip\"}'\n>>> CompressedFileExtension('.lz')\n<CompressedFileExtension.LZ: ValueWithDescription(\n    value='.lz', \n    description='Employs the Lempel\u2013Ziv\u2013Markov chain algorithm (LZMA)'\n)>\n>>> CompressedFileExtension('.unknown')\nValueError: '.unknown' is not a valid CompressedFileExtension\n```\n\nEasily works with `orjson`.\n\n```pycon\n>>> import orjson\n>>> orjson.dumps(\n...     {\n...         \"file_extension1\": CompressedFileExtension.IZO, \n...         \"file_extension2\": CompressedFileExtension.ZIP\n...     }\n... )\nb'{\"file_extension1\":\".izo\",\"file_extension2\":\".zip\"}'\n```\n\nAlso works great with `Pydantic v1`.\n\n```pycon\n>>> from extended_enum import ExtendedEnum, EnumField, ValueWithDescription\n>>> from pydantic import BaseModel\n>>> class SomeModel(BaseModel):\n...     file_path: str\n...     file_extension: CompressedFileExtension\n>>> obj = SomeModel(\n...    file_path='/path/to/compressed_file.lz',\n...    file_extension=CompressedFileExtension.LZ\n...)\n>>> obj.json()\n'{\"file_path\": \"/path/to/compressed_file.lz\", \"file_extension\": \".lz\"}'\n>>> \n>>> SomeModel.parse_raw('{\"file_path\": \"/path/to/compressed_file.lz\", \"file_extension\": \".lz\"}')\nSomeModel(\n    file_path='/path/to/compressed_file.lz', \n    file_extension=<CompressedFileExtension.LZ: ValueWithDescription(\n        value='.lz', \n        description='Employs the Lempel\u2013Ziv\u2013Markov chain algorithm (LZMA)'\n    )>\n)\n>>> \n>>> import orjson\n>>> data = orjson.loads('{\"file_path\": \"/path/to/compressed_file.lz\", \"file_extension\": \".lz\"}')\n>>> SomeModel(**data)\nSomeModel(\n    file_path='/path/to/compressed_file.lz', \n    file_extension=<CompressedFileExtension.LZ: ValueWithDescription(\n        value='.lz', \n        description='Employs the Lempel\u2013Ziv\u2013Markov chain algorithm (LZMA)'\n    )>\n)\n```\n\nAlso works great with `Pydantic v2` with minor differences from the previous example.\n\n```pycon\n>>> obj.model_dump_json()\n'{\"file_path\":\"/path/to/compressed_file.lz\",\"file_extension\":\".lz\"}'\n>>> \n>>> SomeModel.model_validate_json('{\"file_path\": \"/path/to/compressed_file.lz\", \"file_extension\": \".lz\"}')\nSomeModel(\n    file_path='/path/to/compressed_file.lz', \n    file_extension=<CompressedFileExtension.LZ: ValueWithDescription(\n        value='.lz', \n        description='Employs the Lempel\u2013Ziv\u2013Markov chain algorithm (LZMA)'\n    )>\n)\n>>> \n>>> import orjson\n>>> data = orjson.loads('{\"file_path\": \"/path/to/compressed_file.lz\", \"file_extension\": \".lz\"}')\n>>> SomeModel(**data)\nSomeModel(\n    file_path='/path/to/compressed_file.lz', \n    file_extension=<CompressedFileExtension.LZ: ValueWithDescription(\n        value='.lz', \n        description='Employs the Lempel\u2013Ziv\u2013Markov chain algorithm (LZMA)'\n    )>\n)\n```\n\n\n## License\n\nThis project is licensed under the [Apache-2.0](https://github.com/ilichev-andrey/python-extended-enum/blob/master/LICENSE) License.\n\n",
    "bugtrack_url": null,
    "license": "Apache-2.0",
    "summary": "Extends the capabilities of the standard Enum.",
    "version": "1.1.0",
    "project_urls": {
        "Documentation": "https://github.com/ilichev-andrey/python-extended-enum",
        "Homepage": "https://github.com/ilichev-andrey/python-extended-enum",
        "Repository": "https://github.com/ilichev-andrey/python-extended-enum",
        "Tracker": "https://github.com/ilichev-andrey/python-extended-enum/issues"
    },
    "split_keywords": [
        "enum",
        " extended",
        " extended-enum"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "abd697c84de4535a9ca28eb3e3879341d0b4140b2ea5fc9c833ef061888187f7",
                "md5": "80cfa26fd4339e26791eea19b03cfb76",
                "sha256": "38e602fbd69fb0aef69c0e1363eff066ce0fa46e69f92c2fe3ae0d98072d08a4"
            },
            "downloads": -1,
            "filename": "extended_enum-1.1.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "80cfa26fd4339e26791eea19b03cfb76",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4.0.0,>=3.8.1",
            "size": 18366,
            "upload_time": "2024-03-29T18:48:12",
            "upload_time_iso_8601": "2024-03-29T18:48:12.772906Z",
            "url": "https://files.pythonhosted.org/packages/ab/d6/97c84de4535a9ca28eb3e3879341d0b4140b2ea5fc9c833ef061888187f7/extended_enum-1.1.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "f7bf2c65b61af2e4bacbc4db84f1a97d87d9fc89caf971677c6b32d5138aba7c",
                "md5": "82fbbbe5804c8aae65e6936cdc34751d",
                "sha256": "d720b56447a975b0eefbd0bd5071d709ac2ab6589f407caf35088e950f6ebb81"
            },
            "downloads": -1,
            "filename": "extended_enum-1.1.0.tar.gz",
            "has_sig": false,
            "md5_digest": "82fbbbe5804c8aae65e6936cdc34751d",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4.0.0,>=3.8.1",
            "size": 10512,
            "upload_time": "2024-03-29T18:48:14",
            "upload_time_iso_8601": "2024-03-29T18:48:14.426577Z",
            "url": "https://files.pythonhosted.org/packages/f7/bf/2c65b61af2e4bacbc4db84f1a97d87d9fc89caf971677c6b32d5138aba7c/extended_enum-1.1.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-03-29 18:48:14",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "ilichev-andrey",
    "github_project": "python-extended-enum",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "extended-enum"
}
        
Elapsed time: 0.23696s