A simple alternative to Python 3 enums.
Xenum offers a simple alternative to Python 3 enums that’s especially useful for (de)serialization. When you would like to model your enum values so that they survive jumps to and from JSON, databases, and other sources cleanly as strings, Xenum will allow you to do so extremely easily.
Installation is simple. With python3-pip, do the following:
$ sudo pip install -e .
Or, to install the latest version available on PyPI:
$ sudo pip install xenum
Just create a basic class with attributes and annotate it with the @xenum attribute. This will convert all of the class attributes into Xenum instances and allow you to easily convert them to strings via str(MyEnum.A) and from strings via MyEnum.by_name("MyEnum.A").
from xenum import xenum, ctor @xenum class Actions: INSERT = ctor('insert') UPDATE = ctor('update') DELETE = ctor('delete') def __init__(self, name): self.name = name assert Actions.INSERT == Actions.by_name('Actions.INSERT') assert Actions.INSERT().name == 'insert'
Checkout test.py in the git repo for more usage examples.